zoukankan      html  css  js  c++  java
  • [模拟]Decrease (Judge ver.)

    题目描述

    We have a sequence of length N consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes N−1 or smaller. (The operation is the same as the one in Problem D.)
    Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by N, and increase each of the other elements by 1.
    It can be proved that the largest element in the sequence becomes N−1 or smaller after a finite number of operations.
    You are given the sequence ai. Find the number of times we will perform the above operation.

    Constraints
    2≤N≤50
    0≤ai≤1016+1000
     

    输入

    Input is given from Standard Input in the following format:
    N
    a1 a2 ... aN

    输出

    Print the number of times the operation will be performed.

    样例输入

    4
    3 3 3 3
    

    样例输出

    0
    
    思路:等价模拟;虽然题目描述的是每次取最大的减n,其余加1,直到所有数都小于n,其实这个操作等价于每次取最大的直接减到n以下,其余加上1*减n的次数;
    AC代码:
    #include <iostream>
    #include<cstdio>
    #include<algorithm>
    typedef long long ll;
    using namespace std;
    
    ll a[55];
    
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
        ll ans=0;
        for(;;){
            ll maxn=a[1];int ind=1;
            for(int i=2;i<=n;i++) if(a[i]>maxn) maxn=a[i],ind=i;
            if(maxn<=n-1) break;
            for(int i=1;i<=n;i++){
                if(i==ind) a[i]=maxn%n;
                else a[i]+=maxn/n;
            }
            ans+=(maxn/n);
        }
        printf("%lld
    ",ans);
        return 0;
    }

    直接模拟不可行时,可尝试一种等价的方式模拟

    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    Vue.js + Flask + echarts + MySQL
    一次挖矿处理
    Docker三剑客之Docker Swarm
    Malleable-C2-Profiles配置
    「持续更新中」最近浏览的网站心得总结
    有关虚拟内存的一点思考
    Qt---ToolBox自由伸展
    mindmaster导出markdown文档
    svg代码嵌入秀米的方法
    css3渐变透明度的写法
  • 原文地址:https://www.cnblogs.com/lllxq/p/9281278.html
Copyright © 2011-2022 走看看