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. 股票交易(洛谷U6084)

    题目背景 kkk最近迷上了炒股. 题目描述 kkk炒了N天股,第i天的股价为a[i]元.kkk希望股票每天都上涨1元钱,但是操盘手lzn并不想让kkk赚很多钱导致他亏本,于是a[i]相对a[i-1]就 ...

  2. C++中实现对象的clone()

    在C#中,许多对象自动实现了clone函数,在C++中,要拷贝一个对象,除了自定义一个拷贝构造函数来实现对象复制外,还可以像C#中那样实现一个clone函数,这需要借助编译器实现的一个隐藏拷贝构造函数 ...

  3. FragmentHelper

    package com.icitymobile.anda.util; import java.lang.ref.SoftReference; import java.util.ArrayList; i ...

  4. [转]嵌入式SQC文件编译

      Src Url:http://blog.csdn.net/cws1214/article/details/12996351   A.预编译部分  1.预编译DB2篇 1.1 什么是DB2预编译 在 ...

  5. NYOJ题目75日期计算

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAr4AAAInCAIAAADxjM2pAAAgAElEQVR4nO3dPVLjStsG4G8T5CyE2A

  6. Android之IPC机制

    Android IPC简介 任何一个操作系统都需要有相应的IPC机制,Linux上可以通过命名通道.共享内存.信号量等来进行进程间通信.Android系统不仅可以使用了Binder机制来实现IPC,还 ...

  7. mySQL中如何给某一IP段的用户授权?

    给一个用用户use ip: 192.168.0.1 语句是: grant all on *.* to root@192.168.0.1 identified by 'pass' 来授权 其中:root ...

  8. 有关Oracle数据库

    创建数据库(DCA):http://jingyan.baidu.com/article/cbcede07cf42ef02f40b4dc2.html 创建表(连接数据库,sql创建数据表):http:/ ...

  9. 攻城狮在路上(壹) Hibernate(十一)--- 映射实体关联关系

    本文以Customer和Address类的关系为例说明一对一关联映射:以Category和Item类的关系说明多对多关联关系.一.映射一对一关联: 分两种情况:按照外键映射和按照主键映射.这两种方式的 ...

  10. tornado使用(Mac)

    安装需求 Tornado 在 Python 2.5, 2.6, 2.7 中都经过了测试.要使用 Tornado 的所有功能,你需要安装 PycURL (7.18.2 或更高版本) 以及 simplej ...