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

Sorting the Tombstones

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice URAL 1252

Description

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

input output
5
30
21
56
40
17
1

题目大意:给你n个数,让你求中间隔K个数可以交换两边的数时,让这n个数有序,问这个K最大是多少。  如样例:K = 1,即 56 和 17可以交换位置,21 和 40可以交换位置。

解题思路:我们可以设每个数的起始位置是idx,有序时应在的位置是dst。那么 dst = idx + K*x。 K是要求的值,x表示某个整数。那么要让所有的数都能交换到达有序的位置,那么,dst[i] = idx[i] + K[i] * x[i]。那么我们要求的K,就是所有的GCD(K[i]*x[i] , ans)。 还要注意的是,顺序包括两种,递增和递减,结果取两种的最大值。

#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
*/

  

时间: 2024-07-28 19:13:19

URAL 1252 ——Sorting the Tombstones——————【gcd的应用】的相关文章

URAL(timus) 1280 Topological Sorting(模拟)

Topological Sorting Time limit: 1.0 secondMemory limit: 64 MB Michael wants to win the world championship in programming and decided to study N subjects (for convenience we will number these subjects from 1 to N). Michael has worked out a study plan

ural 1249. Ancient Necropolis

1249. Ancient Necropolis Time limit: 5.0 secondMemory limit: 4 MB Aerophotography data provide a bitmap picture of a hard-to-reach region. According to the suggestions of scientists, this region is a cemetery of an extinct civilization. Indeed, the p

URAL 1907. Coffee and Buns(数论推导+容斥原理)

1907. Coffee and Buns Time limit: 1.0 second Memory limit: 64 MB Planet Ataraxia is known for its education centers. The people who are expected to take high social positions in future are brought up in conditions of continuous training and supervisi

URAL 2003. Simple Magic(数学啊 )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2003 2003. Simple Magic Time limit: 1.0 second Memory limit: 64 MB Do you think that magic is simple? That some hand-waving and muttering incomprehensible blubber is enough to conjure wonderful garden

Ural 1903 Unidentified Ships 组合数 + 乘法逆元

一开始题意没读懂,英语是硬伤,其实是这道题目真的有点饶人,后来补题,看懂了意思,从n个数中挑出t个,然后第k个必须要在,挑出的t个数要排序成不下降的顺序,然后 原本那个第k个数在这个跳出的t个数当中要在第x的位置 分析:直接找出比第k个数小的数的个数,还有比第k个数大的数的个数,当然啦还有可能存在与第k个数相等的数的个数,唉呀,一开始漏了相等的情况,没看题目案例,真是作死啊,后来全弄好了,那不就是在比它小的里面挑x-1个数字,当然也可以从相等的里面挑了补,然后在比它大的  里面挑t-x个数 当然

iphone ios 如何使用gcd,block

iphone ios 如何使用gcd,block 转自:http://blog.sina.com.cn/s/blog_45e2b66c01010dhd.html 1.GCD之dispatch queue http://www.cnblogs.com/scorpiozj/archive/2011/07/25/2116459.html 2.iOS中GCD的魔力 http://blog.csdn.net/favormm/article/details/6453260 3.官方 ,内容真的很多 http

URAL 1204. Idempotents 扩展欧几里德

题目来源:URAL 1204. Idempotents 题意:输入n(n = p*q p,q是质数) 并且x*x=x(mod n) 求x 思路: x*x=x(mod n)  -> x*x+k*n=x -> x*(x-1)/n = k 所以 0 和 1 是一组解 因为n = p*q 且x*(x-1)%(p*q)== 0 x < n 因为x*x%n == x 模n之后才是x 1.x有p因子x-1有q因子 x%p == 0且(x-1)%q == 0 a*p == x且b*q == x-1 得到

upc 3028 Card Hand Sorting

Card Hand Sorting 时间限制: 1 Sec  内存限制: 64 MB提交: 66  解决: 23[提交] [状态] [讨论版] [命题人:外部导入] 题目描述 When dealt cards in the card game Plump it is a good idea to start by sorting the cards in hand by suit and rank. The different suits should be grouped and the ra

dutacm.club 1094: 等差区间(RMQ区间最大、最小值,区间GCD)

1094: 等差区间 Time Limit:5000/3000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)Total Submissions:655   Accepted:54 [Submit][Status][Discuss] Description 已知一个长度为 n 的数组 a[1],a[2],-,a[n],我们进行 q 次询问,每次询问区间 a[l],a[l+1],-,a[r?1],a[r] ,数字从小到大