比赛:
Codeforces Round #819 (Div. 1 + Div. 2) and Grimoire of Code Annual Contest 2022 |
---|
这一场D题目本来可以写出来的,可惜了,然后这场没有算分有些可惜要不然还可以加点分的。。
这个D题还是用并查集比较好些,我试了DFS, BFS的方法,一直是WA,然后也没有找到问题,按理讲是可以的。
刷题:
CF1800分的题目练习:
HDU上面一个题目练习:
这个就是质因数分解然后分配之后就是贪心了。是一道比较好的题目,就是范围可以不用取到sum,这个点要抓住。
//A Simple Stone Game
#include <bits/stdc++.h>
using namespace std;
#define OST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ll long long
#define endl "\n"
const ll mod = 1e9 + 7, inf = 0x3f3f3f3f;
const int N = 1e5 + 10;
vector<int> prime;
int n;
void init() {
vector<int> vis(N, 0);
vis[0] = vis[1] = 1;
for (int i = 2; i <= N - 1; i ++ ) {
if (!vis[i]) {
prime.push_back(i);
}
for (auto x : prime) {
if (x * i >= N) {
break;
}
vis[x * i] = 1;
if (i % x == 0) {
break;
}
}
}
}
vector<int> fun(ll sum) {
vector<int> res;
for (auto x : prime) {
if (sum % x == 0) res.push_back(x);
while (sum % x == 0) {
sum /= x;
}
}
// if (sum > 1) {
// res.push_back(sum);
// }
return res;
}
ll Count(ll x, vector<int> a) {
ll res = 0, sum = 0;
vector<ll> shen;
for (int i = 0; i < n; i ++) {
sum += a[i] % x;
if (a[i] % x) {
shen.push_back(a[i] % x);
}
}
sort(shen.begin(), shen.end());
reverse(shen.begin(), shen.end());
for (auto b : shen) {
if (sum <= 0) break;
res += x - b, sum -= x;
}
return res;
}
void solve() {
cin >> n;
vector<int> a(n);
ll sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
sort(a.begin(), a.end());
// 分解质因数
vector<int> zhiyin = fun(sum);
// cout << sum << endl;
// for (auto x : zhiyin) {
// cout << x << " ";
// }
// cout << endl;
ll ans = inf;
for (auto x : zhiyin) {
if (x >= sum) break;
ans = min(ans, Count(x, a));
ans = min(ans, Count(sum / x, a));
}
if (ans != inf)
cout << ans << endl;
else
cout << sum - a[n - 1] << endl;
}
signed main() {
OST;
int T = 1;
cin >> T;
init();
// for (auto x : prime) {
// cout << x << " ";
// }
while (T--) { solve(); }
return 0;
}
归纳模板:主要就是9.11打名额赛,然后总结一下板子顺便复习一下,把板子打好。主要还是刷刷题目保持手感,希望到时候状态好一点。