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


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

    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

    inputoutput
    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
    ",ans);
        }
        return 0;
    }
    
    /*
    5
    30
    21
    56
    40
    17
    */
    

      

     
     
  • 相关阅读:
    kubernetes之配置Pod的Volume存储
    kubernetes之配置Pod的QoS
    kubernetes之定义容器和Pod的CPU资源
    kubernetes之定义容器和Pod的内存资源
    kubernetes之配置namespace中Pod的总数
    kubernetes之配置namespace中CPU最大最小值约束
    kubernetes之配置namespace中内存最大最小值约束
    kubernetes之配置namespace的默认CPU请求和限制
    Kubernetes之配置namespace的默认内存请求和限制
    信步漫谈之Jmeter—引入外部函数加解密报文
  • 原文地址:https://www.cnblogs.com/chengsheng/p/5057432.html
Copyright © 2020-2023  润新知