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 ...
随机推荐
- 笔记本小键盘提示 C#
穷人家的孩子,买了个笔记本愣是没有小键盘提示灯. 牛的是人家给了一个大写提示灯. 更牛的是他妈给了音量关闭打开的提示灯,还他妈是橙色的!!!!!! 没办法 弄了小程序 来判断是否打开小键盘了. 本来是 ...
- Google浏览器历史版和下载地址
Google浏览器历史版本下载地址https://www.slimjet.com/chrome/google-chrome-old-version.php google webdriver下载地址分享 ...
- Django 实现购物车功能
购物车思路:使用 session 功能识别不同浏览器用户,使得用户不管是否登录了网站,均能够把想要购买的产品放在某个地方,之后随时可以显示或修改要购买的产品,等确定了之后再下订单,购物车可以用来暂存商 ...
- 使用webpack开发ES6程序的正确姿势
1.cnpm install babel-loader babel-core babel-preset-es2015 -D 2.cnpm install babel-plugin-transform- ...
- 50个php程序性能优化的方法,赶紧收藏吧!
1. 用单引号代替双引号来包含字符串,这样做会更快一些.因为 PHP 会在双引号包围的 字符串中搜寻变量,单引号则不会,注意:只有 echo 能这么做,它是一种可以把多个字符 串当作参数的“函数”(译 ...
- CBoard 汉化以及元数据存储配置
汉化配置如图位置: 元数据存储配置为Mysql数据库的位置 版权声明:本文为博主原创文章,需要转载请注明出处. [置顶]Cboard 系列随笔
- js事件处理程序return false ,preventDefault,returnValue
面试题目中,经常会被问到如何阻止默认行为. 以下是<javascript权威指南>书中的内容,详情可以去看书. 能够取消事件默认操作的方法有三种 1.属性注册的事件处理程序的返回值fals ...
- HTML 培训教程
HTML培训教程 1. HTML概述 1.1. 什么是 HTML 文件? n ...
- STM32基础分析——PWM配置
在使用STM32F103产生固定频率.固定占空比的PWM波时,虽然有官方以及众多开发板提供的例程,但是关于有点问题并没有说的很清晰,并且<STM32F10X参考手册>的中文翻译可能容易造成 ...
- 域名dns 查询
腾讯云 DNS 使用不同套餐版本的解析分别为不同集群,每种集群对应的 DNS 地址是不同的: 解析套餐版本 DNS 地址 备注 免费 DNS 地址 f1g1ns1.dnspod.net/f1g1ns2 ...