http://acm.hdu.edu.cn/showproblem.php?pid=6486
要让所有数都俩俩差值为0,让N-1一个数-1,对于差值的影响,就是没有-1的那个数进行+1,最大操作次数为mx-1,现在要让每个数与最大值的差值为0,那么需要的操作次数就是sum(mx - a[i]),然后sum(mx - a[i]) <= mx-1,就表示可以,否则不可以
#include <stdio.h> #include <cstring> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <iostream> #include <map> #include <set> #define go(i, l, r) for(int i = (l), i##end = (int)(r); i <= i##end; ++i) #define god(i, r, l) for(int i = (r), i##end = (int)(l); i >= i##end; --i) #define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define debug_in freopen("in.txt","r",stdin) #define debug_out freopen("out.txt","w",stdout); #define pb push_back #define all(x) x.begin(),x.end() #define fs first #define sc second using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll,ll> pii; const ll maxn = 1e6+10; const ll maxM = 1e6+10; const ll inf_int = 1e8; const ll inf_ll = 1e17; template<class T>void read(T &x){ T s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();} while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); x = s*w; } template<class H, class... T> void read(H& h, T&... t) { read(h); read(t...); } void pt(){ cout<<'\n';} template<class H, class ... T> void pt(H h,T... t){ cout<<" "<<h; pt(t...);} //-------------------------------------------- ll T,N; ll a[maxn],mx; int solve(){ ll sum = 0; go(i,1,N) sum += mx - a[i]; if(sum <= mx) return sum; return -1; } int main() { // debug_in; // debug_out; read(T); while(T--){ read(N); mx = -1; for(int i = 1;i<=N;i++) { read(a[i]); mx = max(mx,a[i]); } cout<<solve()<<'\n'; } return 0; }