1252. Sorting the Tombstones

Time limit: 1.0 second
Memory limit: 64 MB
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 output
5
30
21
56
40
17
1
Problem Author: Alexey Lakhtin
Problem Source: Open collegiate programming contest for student teams, Ural State University, March 15, 2003
Difficulty: 417
 
题意:给一个序列n个数,如果只能交换相距为k的两个数,然后能够通过这种交换使原序列有序,那么这个k是符合性质的,求最大的k,输出k-1。
分析:如果一开始就是有序的,答案就是n-1。
否则,考虑一个合法的k,
再考虑一个数到应该去的位置的距离x,
显然有k整除于x,即   k|x
那么这个k就是每个数到应该去的位置的距离的gcd。
注意有序既可以是从小到大,又可以是从大到小。
 
 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = ;
int n, arr[N], order[N];
int ans; inline void Input()
{
scanf("%d", &n);
for(int i = ; i < n; i++) scanf("%d", &arr[i]);
} inline int Gcd(int a, int b)
{
if(b) return Gcd(b, a % b);
else return a;
} inline int Work(int *arr, int *order, int n)
{
int ret = ;
for(int i = ; i < n; i++)
{
int idx = lower_bound(order, order + n, arr[i]) - order;
int delta = abs(i - idx);
ret = Gcd(ret, delta);
}
return ret;
} inline void Solve()
{
ans = ;
for(int i = ; i < n; i++) order[i] = arr[i];
sort(order, order + n);
int t1 = Work(arr, order, n);
for(int i = ; i < n; i++) arr[i] = order[i] = - arr[i];
sort(order, order + n);
int t2 = Work(arr, order, n);
if(!t1 || !t2) ans = n;
else ans = max(t1, t2); printf("%d\n", ans - );
} int main()
{
freopen("a.in", "r", stdin);
Input();
Solve();
return ;
}

ural 1252. Sorting the Tombstones的更多相关文章

  1. URAL 1252 ——Sorting the Tombstones——————【gcd的应用】

    Sorting the Tombstones Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  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 ——1249——————【想法题】

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

  5. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  6. 1306. Sorting Algorithm 2016 12 30

    1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...

  7. 算法:POJ1007 DNA sorting

    这题比较简单,重点应该在如何减少循环次数. package practice; import java.io.BufferedInputStream; import java.util.Map; im ...

  8. U3D sorting layer, sort order, order in layer, layer深入辨析

    1,layer是对游戏中所有物体的分类别划分,如UIlayer, waterlayer, 3DModelLayer, smallAssetsLayer, effectLayer等.将不同类的物体划分到 ...

  9. WebGrid with filtering, paging and sorting 【转】

    WebGrid with filtering, paging and sorting by Jose M. Aguilar on April 24, 2012 in Web Development A ...

随机推荐

  1. 解决java.lang.NoClassDefFoundError: org/apache/log4j/Level

    现象: java.lang.NoClassDefFoundError: org/apache/log4j/Level at org.slf4j.LoggerFactory.getSingleton(L ...

  2. EventBus学习入门

    EventBus Features What makes greenrobot's EventBus unique, are its features: Simple yet powerful: Ev ...

  3. 学习hibernate @Entity该导入哪个包

    1.在@Entity时很容易顺手导入@org.hibernate.annotations.Entity这个包,结果导致了异常.其实应该导入的是@javax.persistence.Entity Alw ...

  4. AFNetworking请求设置请求头

    NSString *url = @"INPUT URL HERE"; AFHTTPRequestOperationManager *manager = [AFHTTPRequest ...

  5. 登录到mysql查看binlog日志

    查看当前第一个binlog文件的内容 show binlog events; 查看指定binlog文件内容 show binlog events in 'mysql-bin.000002'; 查看当前 ...

  6. 以16进制打印出一块内存buff

    如下代码(支持windows与Linux)会以[16进制][每行16字节]打印出一块内存的内容: void PrintBuffer(void* pBuff, unsigned int nLen) { ...

  7. 单机安装Hadoop环境

    目的 这篇文档的目的是帮助你快速完成单机上的Hadoop安装与使用以便你对Hadoop分布式文件系统(HDFS)和Map-Reduce框架有所体会,比如在HDFS上运行示例程序或简单作业等. 先决条件 ...

  8. 利用YaHoo YUI实现Javascript CSS 压缩 分类: C# 2014-07-13 19:07 371人阅读 评论(0) 收藏

    网站优化时,往往需要对js文件,css文件进行压缩,以达到减少网络传输数据,减少网页加载时间:利用YaHoo的YUI可以实现Javascript,CSS,压缩,包括在线的js压缩和程序压缩,发现C#也 ...

  9. hdu 4041 2011北京赛区网络赛B 搜索 ***

    直接在字符串上搜索,注意逗号的处理 #include<cstdio> #include<iostream> #include<algorithm> #include ...

  10. csipsimple,linphone,webrtc比较

    转自: http://www.lxway.com/566299526.htm 最新要做一个移动端视频通话软件,大致看了下现有的开源软件 一) sipdroid1)架构sip协议栈使用JAVA实现,音频 ...