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 ...
随机推荐
- JavaScript中创建自定义对象的方法
本文内容参考JavaScript高级程序设计(第3版)第6章:面向对象的程序设计 ECMA-262中把对象定义为:“无序属性的集合,其属性可以包含基本值.对象或者函数.”我所理解的就是对象就是一个结构 ...
- kali 插耳机没声音
打开终端,然后输入命令:下载pulseaudio音量控制软件: apt install pavucontrol 然后终端输入指令:pavucontrol打开软件,发现在输出设备中有两个输出设备:一个N ...
- 【ExecutorService】概述
初试 今天做一个上传excel,后台异步导入数据功能,使用ExecutorService private final ExecutorService m_longPollingService; pub ...
- SQL 单引号转义
declare @userNum varchar(50),@waterNum varchar(50),@tableName varchar(20),@sql varchar(max) select @ ...
- python期中总结
1.tuple 元组 与列表类似 tup1 = (12, 34.56) tup2 = ('abc', 'xyz') # 以下修改元组元素操作是非法的. # tup1[0] = 100 # 创建一个新的 ...
- mysql 查询库中有几张表
SELECT COUNT(*) TABLES, table_schemaFROM information_schema.TABLESWHERE table_schema = '999*999' 999 ...
- 简单了解树形DP
今天在B站看了一个树形DP教学视频有所收获,做一个小小的总结 AV号和链接在这:av12194537 那么先介绍一下树形DP 树形DP就是在树这个特殊的数据结构上进行的DP.有两种方向:自顶向下和自底 ...
- fluent中UDF环境变量问题的三种解决方法
方法一: 这种方式最简便,首选这种,但是有时会因为不明原因而不好使,我自己电脑刚开始用这种方式是行得通的,但是后来中途装过很多乱七八糟的软件,估计环境变量改乱了,这时候只能用第二种或者第三种方法.先说 ...
- abp + angular 项目 图标字体注意事项
用的字体建议下载到本地,否则部署环境没有网络的话,则图片字体会不正常显示.
- Subsequence(二分)
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...