CF1931B题解

思路

我们首先要计算出 mid=ainmid=\frac{\sum a_i}n,因为这是我们的目标。由于 ii 必须 <j<j,所以我们可以从前往后贪心。如果 ai>mida_i>mid,那就把多余的 aimida_i-mid 单位的水倒到下一桶 ai+1a_{i+1} 里;如果 ai<mida_i<mid,那说明把前面所有多余的水都给它都不够,这时候无解。

代码

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int t, n, a[200005];
ll sum, x;
int main () {
	ios::sync_with_stdio (0);
	cin.tie (0);
	cout.tie (0);
	cin >> t;
	while (t --) {
		cin >> n;
		x = sum = 0;
		for (int i = 0; i < n; ++ i)
			cin >> a[i], sum += a[i];
		sum /= n;
		for (int i = 0; i < n; ++ i) {
			a[i] += x;
			if (a[i] < sum) {
				cout << "NO\n";
				goto there;
			}
			x = a[i] - sum;
		}
		cout << "YES\n";
there:
		;
	}
	return 0;
}
posted @ 2024-02-20 09:04  Vitamin_B  阅读(8)  评论(0)    收藏  举报  来源