A. Subset Mex
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) int n; int a[110], cnt[110]; inline void solve(int T) { cin >> n; int ans = 0; rep(i, 0, 105) cnt[i] = 0; rep(i, 1, n) cin >> a[i], cnt[a[i]]++; rep(i, 0, 105) if(cnt[i]) { cnt[i]--; } else { ans += i; break; } rep(i, 0, 105) if(cnt[i]) { cnt[i]--; } else { ans += i; break; } cout << ans << endl; } int main() { // ios_base::sync_with_stdio(0); // cin.tie(0); // cout.tie(0); // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); int T = 1; cin >> T; rep(i, 1, T) solve(i); // system("pause"); }
B. Maximum Product
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) ll n; ll a[100010]; inline void solve(int T) { cin >> n; rep(i, 1, n) cin >> a[i]; sort(a + 1, a + n + 1); ll ans = a[1] * a[2] * a[3] * a[4] * a[5]; ans = max(ans, a[1] * a[2] * a[3] * a[4] * a[n]); ans = max(ans, a[1] * a[2] * a[3] * a[n - 1] * a[n]); ans = max(ans, a[1] * a[2] * a[n - 2] * a[n - 1] * a[n]); ans = max(ans, a[1] * a[n - 3] * a[n - 2] * a[n - 1] * a[n]); ans = max(ans, a[n - 4] * a[n - 3] * a[n - 2] * a[n - 1] * a[n]); cout << ans<< endl; } int main() { // ios_base::sync_with_stdio(0); // cin.tie(0); // cout.tie(0); // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); int T = 1; cin >> T; rep(i, 1, T) solve(i); // system("pause"); }
C. Link Cut Centroids
主要是找重心,如果有两个就把其中一个作为根,找到最大的连通块取一个点连向根
解释的不是很清楚,具体看代码
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) int n; vector<int> son[100010]; int sz[100010], max_part[100010]; int vis[100010]; int ma, cnt, point; void dfs(int u, int fa) { sz[u] = 1; max_part[u] = 0; for(vector<int>::iterator it = son[u].begin(); it != son[u].end(); it++) { int v = *it; if(v == fa) continue; dfs(v, u); sz[u] += sz[v]; max_part[u] = max(max_part[u], sz[v]); } max_part[u] = max(max_part[u], n - sz[u]); if(max_part[u] < ma) { cnt = 1; ma = max_part[u]; point = u; } else if(max_part[u] == ma) cnt++; } bool cmp(int ff, int gg) {return sz[ff] > sz[gg];} inline void solve(int T) { cin >> n; rep(i, 1, n) son[i].clear(); ma = n * 2; cnt = point = 0; int x, y; rep(i, 1, n - 1) { cin >> x >> y; son[x].push_back(y); son[y].push_back(x); } dfs(1, 0); // cout << max_part[point] << " " << point << " " << cnt << endl; if(cnt == 1) cout << x << " " << y << endl << x << " " << y << endl; else { dfs(point, 0); int u = point, v; while(son[u].size() > 1) { sort(son[u].begin(), son[u].end(), cmp); v = u; if(u == point) u = son[u][0]; else u = son[u][1]; } cout << u << " " << v << endl << u << " " << point << endl; } } int main() { // ios_base::sync_with_stdio(0); // cin.tie(0); // cout.tie(0); // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); int T = 1; cin >> T; rep(i, 1, T) solve(i); // system("pause"); }