贪心问题。
从左到右去移动,这里把负的看成是从右向左得到的。
那么就可以得出最大步数就是每个需要移动的和总步数之间的最大值。
#include<bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int,int> pii; const int N = 1e4 + 5; const int M = 3e5 + 5; const LL Mod = 1e9 + 7; #define pi acos(-1) #define INF 1e10 #define dbg(ax) cout << "now this num is " << ax << endl; namespace FASTIO{ inline LL read(){ LL x = 0,f = 1;char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();} while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();} return x*f; } } using namespace FASTIO; int a[N]; int main() { int n; while(cin >> n){ int sum = 0; for(int i = 1;i <= n;++i) a[i] = read(),sum += a[i]; if(sum % n != 0) printf("-1 "); else{ sum /= n; int mx = 0,move = 0; for(int i = 1;i <= n;++i){ move += a[i] - sum; mx = max(mx,max(a[i] - sum,abs(move))); } printf("%d ",mx); } } system("pause"); return 0; }