zoukankan      html  css  js  c++  java
  • 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
    */
    

      

     
     
  • 相关阅读:
    获取最近一周
    git设置个人信息
    ajax的content-download时间过慢问题的解决与思考
    element UI table中字符太多
    git 合并代码冲突最终解决办法
    thinkphp swoole 的使用
    vue elemnet 二进制文件上传
    Python+Selenium+Chrome 笔记(2)Selenium的Hello World
    chrome 自动测试插件
    php-fpm 错误日志 与 php 错误日志的用法
  • 原文地址:https://www.cnblogs.com/chengsheng/p/5057432.html
Copyright © 2011-2022 走看看