URAL 1252 ——Sorting the Tombstones——————【gcd的应用】
2015-12-18 16:27
357 查看
Sorting the Tombstones
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice URAL 1252
Description
There is time to throw stones and there is time to sort stones…
An old desolate cemetery is a long dismal row of nameless tombstones There are N tombstones of various shapes. The weights of all the stones are different. People have decided to make the cemetery look more presentable, sorting the tombstone according to their weight. The local custom allows to transpose stones if there are exactly K other stones between them.
Input
The first input line contains an integer N (1 ≤ N ≤ 130000). Each of the next N lines contains an integer X, the weight of a stone in grams (1 ≤ X ≤ 130000).
Output
The output should contain the single integer — the maximal value of K (0 ≤ K < N), that makes possible the sorting of the stones according to their weights.
Sample Input
题目大意:给你n个数,让你求中间隔K个数可以交换两边的数时,让这n个数有序,问这个K最大是多少。 如样例:K = 1,即 56 和 17可以交换位置,21 和 40可以交换位置。
解题思路:我们可以设每个数的起始位置是idx,有序时应在的位置是dst。那么 dst = idx + K*x。 K是要求的值,x表示某个整数。那么要让所有的数都能交换到达有序的位置,那么,dst[i] = idx[i] + K[i] * x[i]。那么我们要求的K,就是所有的GCD(K[i]*x[i] , ans)。 还要注意的是,顺序包括两种,递增和递减,结果取两种的最大值。
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice URAL 1252
Description
There is time to throw stones and there is time to sort stones…
An old desolate cemetery is a long dismal row of nameless tombstones There are N tombstones of various shapes. The weights of all the stones are different. People have decided to make the cemetery look more presentable, sorting the tombstone according to their weight. The local custom allows to transpose stones if there are exactly K other stones between them.
Input
The first input line contains an integer N (1 ≤ N ≤ 130000). Each of the next N lines contains an integer X, the weight of a stone in grams (1 ≤ X ≤ 130000).
Output
The output should contain the single integer — the maximal value of K (0 ≤ K < N), that makes possible the sorting of the stones according to their weights.
Sample Input
input | output |
---|---|
5 30 21 56 40 17 | 1 |
解题思路:我们可以设每个数的起始位置是idx,有序时应在的位置是dst。那么 dst = idx + K*x。 K是要求的值,x表示某个整数。那么要让所有的数都能交换到达有序的位置,那么,dst[i] = idx[i] + K[i] * x[i]。那么我们要求的K,就是所有的GCD(K[i]*x[i] , ans)。 还要注意的是,顺序包括两种,递增和递减,结果取两种的最大值。
#include<stdio.h>
#include<algorithm>
#include<bits/stdc++.h>
#include<string.h>
#include<bitset>
#include<math.h>
#include<iostream>
using namespace std;
const int maxn = 1e6;
struct Stone{
int wei,idx;
}stones[maxn];
int GCD(int a,int b){
return b == 0? a : GCD(b,a%b);
}
bool cmp1(Stone a,Stone b){
return a.wei < b.wei;
}
bool cmp2(Stone a,Stone b){
return a.wei > b.wei;
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
for(int i = 1; i <= n; i++){
scanf("%d",&stones[i].wei);
stones[i].idx = i;
}
sort(stones+1,stones+1+n,cmp1);
int nn = 0, ans = 0, gcd = 0;
for(int i = 1; i <= n; i++){
int tmp = abs(i - stones[i].idx);
if(tmp){
nn++; gcd = GCD(gcd,tmp);
}
}
if(nn == 0){
ans = n -1;
}
ans = max(ans,gcd-1);
sort(stones+1,stones+1+n,cmp2);
nn = 0, gcd = 0;
for(int i = 1; i <= n; i++){
int tmp = abs(i-stones[i].idx);
if(tmp){
nn++;
gcd = GCD(gcd,tmp);
}
}
if(nn == 0){
ans = n-1;
}
ans = max(ans,gcd-1);
printf("%d\n",ans);
}
return 0;
}
/*
5 30 21 56 40 17*/
相关文章推荐
- Android编程开发规范
- 利用openssl管理证书及SSL编程第2部分:在Windows上编译 openssl
- Spring mvc中的登录界面
- 购物车ListView中CheckBox的逻辑处理
- 利用openssl管理证书及SSL编程第2部分:在Windows上编译 openssl
- TextView/EditText
- 绕过防注入的方法
- 禁止浏览器前进和后退 清除缓存
- tslib-1.4.tar.gz安装和配置
- Andorid Dialog 显示宽度设置
- spring websocket 获取httpsession
- 标准linu休眠和唤醒机制分析(三)
- 磁盘的挂载与卸载 mount umount
- 58、IPv6访问控制列表及ICMPv6简介
- IOS第三方开源库和工具 专辑
- MYSQL 优化常用方法
- 线程通信之无锁队列
- cocos2dx 读取rapidjson及解析
- bzoj 3289: Mato的文件管理 莫队+线段树
- 在 Core Data 中存取 transformable 类型的数据