[题解]AT_arc079_c [ARC079E] Decrease (Judge ver

思路

首先,对于每一次操作,我们可以先找到最大值,然后对其操作。

这样,我们可以得到单次操作时间复杂度 Θ(n) 的代码,因为 n 很小,所以这道题时间复杂度的瓶颈在于操作的数量。

那么,我们想到每一次找到最大值时,直接将其减到小于 n

但是这样可能有一种问题,就是最大值减了一定次数,有可能就不是最大值了。

不难发现,对于本题的减的顺序对答案无影响,因为一个数要减到 n 以下无论顺序需要的次数都一样。

因此,我们直接将最大值减到 n 下次即可。

时间复杂度 Θ(n2) 再多一点常数。

Code

#include <bits/stdc++.h>  
#define int long long  
#define re register  
  
using namespace std;  
  
const int N = 55,inf = 1e18 + 10;  
int n,ans;  
int arr[N];  
  
inline int read(){  
    int r = 0,w = 1;  
    char c = getchar();  
    while (c < '0' || c > '9'){  
        if (c == '-') w = -1;  
        c = getchar();  
    }  
    while (c >= '0' && c <= '9'){  
        r = (r << 3) + (r << 1) + (c ^ 48);  
        c = getchar();  
    }  
    return r * w;  
}  
  
inline int up(int a,int b){  
    if (a % b == 0) return a / b;  
    return a / b + 1;  
}  
  
signed main(){  
    n = read();  
    for (re int i = 1;i <= n;i++) arr[i] = read();  
    while (1){  
        int Max = -inf,id = 0;  
        for (re int i = 1;i <= n;i++){  
            if (Max < arr[i]){  
                Max = arr[i];  
                id = i;  
            }  
        }  
        if (Max < n) break;  
        int cnt = up(Max - (n - 1),n);  
        ans += cnt;  
        arr[id] -= cnt * n;  
        for (re int i = 1;i <= n;i++){  
            if (i == id) continue;  
            arr[i] += cnt;  
        }  
    }  
    printf("%lld",ans);  
    return 0;  
}  

作者:WaterSun

出处:https://www.cnblogs.com/WaterSun/p/18263291

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   WBIKPS  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示