Sorting the Tombstones

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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
 
 
题目大意:给你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)。 还要注意的是,顺序包括两种,递增和递减,结果取两种的最大值。
 
#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
*/

  

 
 

URAL 1252 ——Sorting the Tombstones——————【gcd的应用】的更多相关文章

  1. ural 1252. Sorting the Tombstones

    1252. Sorting the Tombstones Time limit: 1.0 secondMemory limit: 64 MB There is time to throw stones ...

  2. URAL(timus) 1280 Topological Sorting(模拟)

    Topological Sorting Time limit: 1.0 secondMemory limit: 64 MB Michael wants to win the world champio ...

  3. ural 1249. Ancient Necropolis

    1249. Ancient Necropolis Time limit: 5.0 secondMemory limit: 4 MB Aerophotography data provide a bit ...

  4. URAL 1139 City Blocks(数论)

    The blocks in the city of Fishburg are of square form. N avenues running south to north and Mstreets ...

  5. ural 1091. Tmutarakan Exams 和 codeforces 295 B. Greg and Graph

    ural 1091 题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1091 题意是从1到n的集合里选出k个数,使得这些数满足gcd大于1 ...

  6. IOS多线程 总结 -------------核心代码(GCD)

    //NSObject //在子线程中执行代码 // 参数1: 执行的方法 (最多有一个参数,没有返回值) //参数2: 传递给方法的参数 [self performSelectorInBackgrou ...

  7. block ,GCD(转)

    原文:http://blog.sina.com.cn/s/blog_45e2b66c01010dhd.html 1.GCD之dispatch queue http://www.cnblogs.com/ ...

  8. URAL ——1249——————【想法题】

     Ancient Necropolis Time Limit:5000MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u ...

  9. Ural State University Internal Contest October'2000 Junior Session

    POJ 上的一套水题,哈哈~~~,最后一题很恶心,不想写了~~~ Rope Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7 ...

随机推荐

  1. c# 读取txt文档和写入文档的方法

    StreamReader sr = new StreamReader(path); //path是要读取的文件的完整路径 String str_read = sr.ReadToEnd(); //从开始 ...

  2. python-判断语句介绍

    1.生活中的判断场景 1.1 火车站安检 1.2 上网吧 2.开发中的判断场景 2.1 密码判断 2.2 重要日期判断 if 今天是周六或者周日: 约妹子 if 今天是情人节: 买玫瑰 if 今天发工 ...

  3. Selenium辅助工具

    下载Firefox39.0版本浏览器,安装firebug和FirePath.最新版的Firefox在扩展组件中无法找到firebug,可以使用旧的版本的Firefox浏览器. FirePath插件的使 ...

  4. 「BZOJ 3123」「SDOI 2013」森林「启发式合并」

    题意 你有一个森林,你需要支持两个操作 查询两个结点路径上权值第\(k\)小 两个点之间连一条边 强制在线,结点数\(\leq 8\times 10^4\) 题解 如果可以离线,这就是一个主席树板子题 ...

  5. kali linux之被动信息收集(dns信息收集,区域传输,字典爆破)

    公开可获取的信息,不与目标系统产生交互,避免留下痕迹 下图来自美军方 pdf链接:http://www.fas.org/irp/doddir/army/atp2-22-9.pdf 信息收集内容(可利用 ...

  6. oracle为IN OUT变量或OUT变量赋值时提示“表达式''不能用作赋值目标”

    是因为IN OUT变量和OUT变量是要输出的,不能赋给它常量值,这样它就不能再被赋值而输出了,所以是禁止赋常量值的,比如''也是常量值,也不可以赋给这两种类型的变量,如果不能把存储过程中的其他变量赋给 ...

  7. 【bzoj2500】幸福的道路 树形dp+单调队列

    Description 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一同晨练来享受在一起的时光. 他们画出了晨练路线的草图,眼尖的小T发现可以用树来描绘这个草图. ...

  8. 用python实现按权重对N个数据进行选择

    需求:某公司有N个人,根据每个人的贡献不同,按贡献值给每个人赋予一个权重.设计一种算法实现公平的抽奖. 需求分析:按照权重对数据进行选择. 代码实现: 1 def fun(n,p): 2 " ...

  9. 线性可分SVM完全推导过程

  10. UML之类图详解

    原文链接:https://www.cnblogs.com/xsyblogs/p/3404202.html 我们通过一个示例来了解UML类图的基本语法结构.画UML类图其实有专业的工具,像常用的Visi ...