随机打乱数组,并从打乱后的数组选出一个子段,判断是否满足其平均值为全局平均值。其实就相当于随机选数。
可以稳定 AC,跑得飞快。
是这题的数据太难造了吗。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
inline ll read() {...}
char buf[50];
inline void write(ll x) {...}
const int MAX_N = 1e3 + 10;
int S, n, tot, a[MAX_N], sum[MAX_N];
bool check() {
for(int i = 1; i <= n; i++)
sum[i] = sum[i - 1] + a[i];
for(int l = 1; l <= n; l++)
for(int r = l; r <= n; r++) {
if(l == 1 && r == n) continue;
if(sum[r] - sum[l - 1] == (r - l + 1) * S) return true;
}
return false;
}
int main() {
int q = read();
while(q--) {
n = read(), S = 0;
for(int i = 1; i <= n; i++)
a[i] = read(), S += a[i];
if(S % n != 0) puts("No");
else {
S /= n;
bool flag = 0;
for(int k = 1; k <= 100; k++) {
random_shuffle(a + 1, a + n + 1);
if(check()) {
flag = 1;
break;
}
}
puts(flag ? "Yes" : "No");
}
}
return 0;
}