AtCoder Grand Contest 012 题解
A - AtCoder Group Contest
排序一下,贪心取就好了。
1 //waz 2 #include <bits/stdc++.h> 3 4 using namespace std; 5 6 #define mp make_pair 7 #define pb push_back 8 #define fi first 9 #define se second 10 #define ALL(x) (x).begin(), (x).end() 11 #define SZ(x) ((int)((x).size())) 12 13 typedef pair<int, int> PII; 14 typedef vector<int> VI; 15 typedef long long int64; 16 typedef unsigned int uint; 17 typedef unsigned long long uint64; 18 19 #define gi(x) ((x) = F()) 20 #define gii(x, y) (gi(x), gi(y)) 21 #define giii(x, y, z) (gii(x, y), gi(z)) 22 23 int F() 24 { 25 char ch; 26 int x, a; 27 while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-'); 28 if (ch == '-') ch = getchar(), a = -1; 29 else a = 1; 30 x = ch - '0'; 31 while (ch = getchar(), ch >= '0' && ch <= '9') 32 x = (x << 1) + (x << 3) + ch - '0'; 33 return a * x; 34 } 35 36 const int N = 300010; 37 38 int n, a[N]; 39 40 int main() 41 { 42 gi(n); 43 for (int i = 1; i <= 3 * n; ++i) gi(a[i]); 44 sort(a + 1, a + 3 * n + 1); 45 int cnt = 0; 46 long long ans = 0; 47 for (int i = 3 * n - 1; i; i -= 2) 48 { 49 ans += a[i]; 50 ++cnt; 51 if (cnt == n) break; 52 } 53 printf("%lld\n", ans); 54 return 0; 55 }
B - Splatter Painting
d很小,倒着暴力即可,每个点只会被最多遍历10次。
1 //waz 2 #include <bits/stdc++.h> 3 4 using namespace std; 5 6 #define mp make_pair 7 #define pb push_back 8 #define fi first 9 #define se second 10 #define ALL(x) (x).begin(), (x).end() 11 #define SZ(x) ((int)((x).size())) 12 13 typedef pair<int, int> PII; 14 typedef vector<int> VI; 15 typedef long long int64; 16 typedef unsigned int uint; 17 typedef unsigned long long uint64; 18 19 #define gi(x) ((x) = F()) 20 #define gii(x, y) (gi(x), gi(y)) 21 #define giii(x, y, z) (gii(x, y), gi(z)) 22 23 int F() 24 { 25 char ch; 26 int x, a; 27 while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-'); 28 if (ch == '-') ch = getchar(), a = -1; 29 else a = 1; 30 x = ch - '0'; 31 while (ch = getchar(), ch >= '0' && ch <= '9') 32 x = (x << 1) + (x << 3) + ch - '0'; 33 return a * x; 34 } 35 36 const int N = 1e5 + 10; 37 38 int n, m; 39 40 VI edge[N]; 41 42 int mxd[N], co[N]; 43 44 void dfs(int u, int d, int c) 45 { 46 if (d <= mxd[u]) return; 47 if (!co[u]) co[u] = c; 48 mxd[u] = d; 49 for (auto v : edge[u]) dfs(v, d - 1, c); 50 } 51 52 int v[N], d[N], c[N]; 53 54 int main() 55 { 56 gii(n, m); 57 for (int i = 1; i <= n; ++i) mxd[i] = -1; 58 for (int i = 1; i <= m; ++i) 59 { 60 int u, v; 61 gii(u, v); 62 edge[u].pb(v); 63 edge[v].pb(u); 64 } 65 int q; 66 gi(q); 67 for (int i = 1; i <= q; ++i) 68 giii(v[i], d[i], c[i]); 69 for (int i = q; i; --i) 70 dfs(v[i], d[i], c[i]); 71 for (int i = 1; i <= n; ++i) 72 printf("%d\n", co[i]); 73 }
C - Tautonym Puzzle
倍增。
//waz #include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back #define fi first #define se second #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)((x).size())) typedef pair<int, int> PII; typedef vector<int> VI; typedef long long int64; typedef unsigned int uint; typedef unsigned long long uint64; #define gi(x) ((x) = F()) #define gii(x, y) (gi(x), gi(y)) #define giii(x, y, z) (gii(x, y), gi(z)) int F() { char ch; int x, a; while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-'); if (ch == '-') ch = getchar(), a = -1; else a = 1; x = ch - '0'; while (ch = getchar(), ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + ch - '0'; return a * x; } long long n; deque<int> l, r; int tot; void solve(long long n) { if (n == 1) return; solve(n >> 1); l.push_front(++tot); r.push_front(tot); if (n & 1) { l.push_front(++tot); r.push_back(tot); } } int main() { scanf("%lld", &n); solve(n + 1); printf("%d\n", l.size() + r.size()); for (auto x : l) printf("%d ", x); for (auto x : r) printf("%d ", x); return 0; }
D - Colorful Balls
我们找到那些可以自由移动的球,算一下方案数就好了。
1 //waz 2 #include <bits/stdc++.h> 3 4 using namespace std; 5 6 #define mp make_pair 7 #define pb push_back 8 #define fi first 9 #define se second 10 #define ALL(x) (x).begin(), (x).end() 11 #define SZ(x) ((int)((x).size())) 12 13 typedef pair<int, int> PII; 14 typedef vector<int> VI; 15 typedef long long int64; 16 typedef unsigned int uint; 17 typedef unsigned long long uint64; 18 19 #define gi(x) ((x) = F()) 20 #define gii(x, y) (gi(x), gi(y)) 21 #define giii(x, y, z) (gii(x, y), gi(z)) 22 23 int F() 24 { 25 char ch; 26 int x, a; 27 while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-'); 28 if (ch == '-') ch = getchar(), a = -1; 29 else a = 1; 30 x = ch - '0'; 31 while (ch = getchar(), ch >= '0' && ch <= '9') 32 x = (x << 1) + (x << 3) + ch - '0'; 33 return a * x; 34 } 35 36 const int N = 2e5 + 10; 37 38 const int mod = 1e9 + 7; 39 40 int n, x, y, c[N], w[N], mi[N], cnt[N]; 41 42 int fac[N], ifac[N]; 43 44 int C(int n, int m) 45 { 46 if (n < m || m < 0) return 0; 47 return 1LL * fac[n] * ifac[n - m] % mod * ifac[m] % mod; 48 } 49 50 int main() 51 { 52 giii(n, x, y); 53 for (int i = 1; i <= n; ++i) 54 { 55 gii(c[i], w[i]); 56 if (!mi[c[i]]) mi[c[i]] = i; 57 else if (w[mi[c[i]]] > w[i]) mi[c[i]] = i; 58 ++cnt[c[i]]; 59 } 60 fac[0] = 1; 61 for (int i = 1; i <= n; ++i) fac[i] = 1LL * fac[i - 1] * i % mod; 62 ifac[0] = ifac[1] = 1; 63 for (int i = 2; i <= n; ++i) ifac[i] = 1LL * (mod - mod / i) * ifac[mod % i] % mod; 64 for (int i = 2; i <= n; ++i) ifac[i] = 1LL * ifac[i - 1] * ifac[i] % mod; 65 int cho = 0; 66 for (int i = 1; i <= n; ++i) 67 { 68 if (!mi[i]) continue; 69 if (!mi[cho]) cho = i; 70 else 71 { 72 if (w[mi[cho]] > w[mi[i]]) 73 cho = i; 74 } 75 } 76 int t = 1e9; 77 for (int i = 1; i <= n; ++i) 78 if (mi[i] && cho != i) t = min(t, w[mi[i]]); 79 for (int i = 1; i <= n; ++i) 80 { 81 if (cho != c[i]) 82 { 83 if (i == mi[c[i]]) continue; 84 if (w[i] + w[mi[c[i]]] <= x) continue; 85 if (w[i] + w[mi[cho]] <= y) continue; 86 } 87 else 88 { 89 if (i == mi[c[i]]) continue; 90 if (w[i] + w[mi[c[i]]] <= x) continue; 91 if (w[i] + t <= y) continue; 92 } 93 //cerr << i << endl; 94 --cnt[c[i]]; 95 } 96 /*for (int i = 1; i <= n; ++i) 97 cerr << cnt[i] << endl;*/ 98 int num = 0; 99 //cerr << w[mi[2]] << endl; 100 for (int i = 1; i <= n; ++i) 101 { 102 if (mi[i] && w[mi[i]] + w[mi[cho]] <= y) num += cnt[i]; 103 } 104 //cerr << num << endl; 105 int ans = 1; 106 for (int i = 1; i <= n; ++i) 107 { 108 if (mi[i] && w[mi[i]] + w[mi[cho]] <= y) 109 { 110 //cerr << cnt[i] << endl; 111 ans = 1LL * ans * C(num, cnt[i]) % mod; 112 num -= cnt[i]; 113 } 114 } 115 printf("%d\n", ans); 116 return 0; 117 }
E - Camel and Oases
状压DP,算出不用v的f和g,然后判断一下每个区间的合法性即可。
1 //waz 2 #include <bits/stdc++.h> 3 4 using namespace std; 5 6 #define mp make_pair 7 #define pb push_back 8 #define fi first 9 #define se second 10 #define ALL(x) (x).begin(), (x).end() 11 #define SZ(x) ((int)((x).size())) 12 13 typedef pair<int, int> PII; 14 typedef vector<int> VI; 15 typedef long long int64; 16 typedef unsigned int uint; 17 typedef unsigned long long uint64; 18 19 #define gi(x) ((x) = F()) 20 #define gii(x, y) (gi(x), gi(y)) 21 #define giii(x, y, z) (gii(x, y), gi(z)) 22 23 int F() 24 { 25 char ch; 26 int x, a; 27 while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-'); 28 if (ch == '-') ch = getchar(), a = -1; 29 else a = 1; 30 x = ch - '0'; 31 while (ch = getchar(), ch >= '0' && ch <= '9') 32 x = (x << 1) + (x << 3) + ch - '0'; 33 return a * x; 34 } 35 36 const int N = 1 << 20; 37 38 int n, v, x[200010]; 39 40 int l[22][200010], r[22][200010]; 41 42 int val[22], cnt, f[N], g[N]; 43 44 vector<PII> t; 45 46 int main() 47 { 48 gii(n, v); 49 for (int i = 1; i <= n; ++i) gi(x[i]); 50 for (int i = v; ; i = i >> 1) 51 { 52 val[++cnt] = i; 53 for (int j = 1; j <= n; ++j) 54 { 55 int k = j; 56 while (k < n && x[k + 1] - x[k] <= i) ++k; 57 for (int p = j; p <= k; ++p) l[cnt][p] = j, r[cnt][p] = k; 58 if (cnt == 1) t.pb(mp(j, k)); 59 j = k; 60 } 61 if (!i) break; 62 } 63 int all = cnt - 1; 64 for (int i = 0; i < (1 << all); ++i) g[i] = n + 1; 65 for (int i = 1; i < (1 << all); ++i) 66 { 67 for (int j = 0; j < all; ++j) 68 if (i & (1 << j)) 69 { 70 int t = f[i ^ (1 << j)]; 71 f[i] = max(f[i], r[j + 2][t + 1]); 72 t = g[i ^ (1 << j)]; 73 g[i] = min(g[i], l[j + 2][t - 1]); 74 } 75 } 76 for (auto v : t) 77 { 78 bool flag = 0; 79 int s = (1 << all) - 1; 80 for (int i = 0; i < (1 << all); ++i) 81 { 82 if (f[i] >= v.fi - 1 && v.se + 1 >= g[s ^ i]) 83 flag = 1; 84 } 85 for (int i = v.fi; i <= v.se; ++i) 86 puts(flag ? "Possible" : "Impossible"); 87 } 88 }
F - Prefix Median
不会,咕着qwq(最近好像已经咕了3题了)