URAL 1252 ——Sorting the Tombstones——————【gcd的应用】
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
input | output |
---|---|
5 |
1 |
#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的应用】的更多相关文章
- ural 1252. Sorting the Tombstones
1252. Sorting the Tombstones Time limit: 1.0 secondMemory limit: 64 MB There is time to throw stones ...
- URAL(timus) 1280 Topological Sorting(模拟)
Topological Sorting Time limit: 1.0 secondMemory limit: 64 MB Michael wants to win the world champio ...
- ural 1249. Ancient Necropolis
1249. Ancient Necropolis Time limit: 5.0 secondMemory limit: 4 MB Aerophotography data provide a bit ...
- URAL 1139 City Blocks(数论)
The blocks in the city of Fishburg are of square form. N avenues running south to north and Mstreets ...
- 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 ...
- IOS多线程 总结 -------------核心代码(GCD)
//NSObject //在子线程中执行代码 // 参数1: 执行的方法 (最多有一个参数,没有返回值) //参数2: 传递给方法的参数 [self performSelectorInBackgrou ...
- block ,GCD(转)
原文:http://blog.sina.com.cn/s/blog_45e2b66c01010dhd.html 1.GCD之dispatch queue http://www.cnblogs.com/ ...
- URAL ——1249——————【想法题】
Ancient Necropolis Time Limit:5000MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u ...
- Ural State University Internal Contest October'2000 Junior Session
POJ 上的一套水题,哈哈~~~,最后一题很恶心,不想写了~~~ Rope Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7 ...
随机推荐
- mvc 高并发解决方案之一---存储过程
MVC用户访问多线程,一般的lock是无法造成单例的. 存储过程既是一种解决方案,先来看看存储过程优缺点: A. 存储过程允许标准组件式编程 存储过程创建后可以在程序中被多次调用执行,而不必重新编写该 ...
- Highsoft.Highcharts 5.0.6439.38401 key
Highcharts .NET allows developers to make charts using Highcharts API with the Microsoft .NET Framew ...
- Linux虚拟机安装 nginx (nginx1.9.9)
1.安装基础环境包(如果已安装,可更新) yum -y :自动选择y yum -y install openssl* yum -y install libjpeg libjpeg-devel libp ...
- Eclipse下的Hadoop应用开发准备
window下开发的准备: A.在windows的某个目录下解压一个hadoop的安装包 B.将安装包下的lib和bin目录用对应windows版本平台编译的本地库替换 C.在window系统中配置H ...
- hexo博客的相关配置
获得更多资料欢迎进入我的网站或者 csdn或者博客园 前面两节讲解了hexo博客的搭建以及jacman主题的配置,这节主要讲解博客的一些相关配置.比如404页面,图床,自定义页面,个人网站绑定(重要的 ...
- winform跨线程问题(有参数和无参数)
1.invoke是同步线程 using System; using System.Collections.Generic; using System.ComponentModel; using Sys ...
- mac下对NTFS格式的磁盘进行读写操作
mac对NTFS格式的分区读写有很大的限制,网上看到很多相关的文章,都表明了一个信息:需要购买类似NTFS for mac这样的软件才能实现对NTFS格式的分区读写的权限,其实不然,mac自带的hdi ...
- 条目六《当心C++编译器中最烦人的分析机制》
当心C++编译器中最烦人的分析机制 C++是较为底层的面相对象语言,在底层的语法规则分析中,有很多隐藏的分析机制. C++中的普遍规律相符,即尽可能地解释为函数声明. 把形式参数的声明用括号括起来是非 ...
- hdp 2.06 安装备忘
1,官方安装说明文档 http://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.0.6.0-Win/bk_installing_hdp_for_windo ...
- php 缓冲函数
php.ini中有两个关键参数会影响到php的缓存输出控制: output_buffering :on/off 或者整数 .设置为 on 时,将在所有脚本中使用输出缓存控制,不限制缓存的大小.而设置为 ...