2016-2017 ACM-ICPC CHINA-Final
A. Number Theory Problem(规律、签到)
#include <bits/stdc++.h>using namespace std;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);int T, n, cas = 0;scanf("%d", &T);while (T--) {scanf("%d", &n);printf("Case #%d: %d\n", ++cas, n / 3);}return 0;
}
C. Mr. Panda and Strips
给定一个长度为n,(1≤n≤1000)n, (1 \leq n \leq 1000)n,(1≤n≤1000)的数组,里面元素为ci,(1≤ci≤105)c_i,(1 \leq c_i \leq 10 ^ 5)ci,(1≤ci≤105),
问,在里面选出两端不相交的子串(可为空),拼接使得拼接后的子串里面没有重复元素出现,输出这个子串的最大长度。
#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;int pre[N], suc[N], pos[N], a[N], n;set<int> st1, st2;int maxllen(int p, int l, int llen) {auto it = st2.end();while ((it--) != st2.begin()) {if (pos[a[*it]] < p) {return l - *it;}}return llen;
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);int T, cas = 0;scanf("%d", &T);while (T--) {printf("Case #%d: ", ++cas);scanf("%d", &n);for (int i = 1; i <= n; i++) {scanf("%d", &a[i]);}for (int i = 1; i < N; i++) {pos[i] = 0;}for (int i = 1; i <= n; i++) {pre[i] = pos[a[i]];pos[a[i]] = i;}for (int i = 1; i < N; i++) {pos[i] = n + 1;}for (int i = n; i >= 1; i--) {suc[i] = pos[a[i]];pos[a[i]] = i;}int ans = 1;st1.clear();for (int i = n, rlen = 1; i >= 1; i--, rlen++) {while (suc[i] <= i + rlen - 1) {st1.erase(a[i + rlen - 1]);rlen--;}st1.insert(a[i]);pos[a[i]] = i;st2.clear();for (int j = 1, llen = 1; j < i; j++, llen++) {while (pre[j] >= j - llen + 1) {st2.erase(j - llen + 1);llen--;}if (st1.count(a[j])) {st2.insert(j);}if (!st2.size()) {ans = max(ans, llen + rlen);}else {ans = max(ans, j - *(--st2.end()) + rlen);}for (auto it : st2) {ans = max(ans, maxllen(pos[a[it]], j, llen) + pos[a[it]] - i);}}}printf("%d\n", ans);}return 0;
}
D. Ice Cream Tower(二分、贪心)
二分答案,然后贪心 judge 即可。
#include <bits/stdc++.h>using namespace std;const int N = 3e5 + 10;int n, k;long long a[N], b[N];bool judge(int x) {int num = 1, tot = 1;for (int i = 1; i <= x; i++) {b[i] = a[i];}for (int i = x + 1; i <= n; i++) {if (2 * a[i] <= b[tot]) {b[tot++] = a[i];}if (tot == x + 1) {tot = 1;num++;}}return num >= k;
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);int T, cas = 0;scanf("%d", &T);while (T--) {scanf("%d %d", &n, &k);for (int i = 1; i <= n; i++) {scanf("%lld", &a[i]);}sort(a + 1, a + 1 + n, greater<long long> ());int l = 0, r = n;while (l < r) {int mid = l + r + 1 >> 1;if (judge(mid)) {l = mid;}else {r = mid - 1;}}printf("Case #%d: %d\n", ++cas, l);}return 0;
}
E. Bet(思维、贪心)
设我们总共投入SSS元,则对于买入的比赛,我们必须投xix_ixi元,有xi(1+BiAi)=Sx_i(1 + \frac{B_i}{A_i}) = Sxi(1+AiBi)=S,xi=AiSAi+Bix_i = \frac{A_iS}{A_i + B_i}xi=Ai+BiAiS,要使∑iAiSAi+Bi<S\sum\limits_{i} \frac{A_i S}{A_i + B_i} < Si∑Ai+BiAiS<S
则∑iAiAi+Bi<1\sum\limits_{i} \frac{A_i}{A_i + B_i} < 1i∑Ai+BiAi<1,考虑对AiAi+Bi\frac{A_i}{A_i + B_i}Ai+BiAi排一个序,贪心求解即可。
#include <bits/stdc++.h>using namespace std;struct Res {long double a, b;void read() {scanf("%Lf:%Lf", &a, &b);}bool operator < (const Res &t) const {return a * (t.a + t.b) < t.a * (a + b);}
}a[110];int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);int T, n, cas = 0;scanf("%d", &T);while (T--) {scanf("%d", &n);for (int i = 1; i <= n; i++) {a[i].read();}sort(a + 1, a + 1 + n);int ans = 0;long double cur = 0;for (int i = 1; i <= n; i++) {if (cur + a[i].a / (a[i].a + a[i].b) < 1.0) {cur += a[i].a / (a[i].a + a[i].b);ans++;}}printf("Case #%d: %d\n", ++cas, ans);}return 0;
}
G. Pandaria (Kruskal 重构树、线段树合并)
给定一个有nnn条边的无向连通图,每条边有对应的边权,每个点有一个颜色,
问从一个点出发,经过不超过www的边权,所能到达的点中,颜色出现次数做多且颜色编号最小的是什么颜色。
不超过某个权值所能到达的点,由此我们可以考虑建立升序kruskalkruskalkruskal重构树,然后从某个点倍增往上跳,直到不能跳为止,
这个时候我们所在的点的子树所包含的点就是我们能够到达的点了,
考虑用权值线段树来维护每一个点所代表的子树的信息,更新的时候我们只要往上进行线段树合并就行了。
#include <bits/stdc++.h>using namespace std;const int N = 2e5 + 10;int n, nn, m, Q, ff[N], value[N], color[N], fa[N][21], ans[N];int root[N], ls[N << 5], rs[N << 5], maxn[N << 5], num;vector<int> G[N];struct Res {int u, v, w;void read() {scanf("%d %d %d", &u, &v, &w);}bool operator < (const Res &t) const {return w < t.w;}
}edge[N];void push_up(int rt) {maxn[rt] = max(maxn[ls[rt]], maxn[rs[rt]]);
}void update(int &rt, int l, int r, int x, int v) {if (!rt) {rt = ++num;}if (l == r) {maxn[rt] += v;return ;}int mid = l + r >> 1;if (x <= mid) {update(ls[rt], l, mid, x, v);}else {update(rs[rt], mid + 1, r, x, v);}push_up(rt);
}int merge(int x, int y, int l, int r) {if (x == 0 || y == 0) {return x | y;}if (l == r) {maxn[x] += maxn[y];return x;}int mid = l + r >> 1;ls[x] = merge(ls[x], ls[y], l, mid);rs[x] = merge(rs[x], rs[y], mid + 1, r);push_up(x);return x;
}int query(int rt, int l, int r) {if (l == r) {return l;}int mid = l + r >> 1;if (maxn[ls[rt]] == maxn[rt]) {return query(ls[rt], l, mid);}else {return query(rs[rt], mid + 1, r);}
}void dfs(int rt, int f) {fa[rt][0] = f;for (int i = 1; i <= 20; i++) {fa[rt][i] = fa[fa[rt][i - 1]][i - 1];}if (rt <= n) {update(root[rt], 1, n, color[rt], 1);}for (int to : G[rt]) {if (to == f) {continue;}dfs(to, rt);root[rt] = merge(root[rt], root[to], 1, n);}ans[rt] = query(root[rt], 1, n);
}int find(int rt) {return ff[rt] == rt ? rt : ff[rt] = find(ff[rt]);
}void kruskal() {sort(edge + 1, edge + 1 + m);for (int i = 1; i < 2 * n; i++) {ff[i] = i;}for (int i = 1, cur = 1; i <= m && cur < n; i++) {int u = find(edge[i].u), v = find(edge[i].v);if (u ^ v) {nn++, cur++;ff[u] = ff[v] = nn;G[nn].push_back(u), G[nn].push_back(v);value[nn] = edge[i].w;if (u <= n) {value[u] = edge[i].w;}if (v <= n) {value[v] = edge[i].v;}}}for (int i = 1; i <= num; i++) {ls[i] = rs[i] = maxn[i] = 0;}for (int i = 1; i <= nn; i++) {root[i] = 0;}num = 0;dfs(nn, 0);for (int i = 1; i <= nn; i++) {G[i].clear();}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int T;scanf("%d", &T);for (int cas = 1; cas <= T; cas++) {printf("Case #%d:\n", cas);scanf("%d %d", &n, &m);nn = n;for (int i = 1; i <= n; i++) {scanf("%d", &color[i]);}for (int i = 1; i <= m; i++) {edge[i].read();}kruskal();scanf("%d", &Q);int res = 0;while (Q--) {int u, x;scanf("%d %d", &u, &x);u ^= res, x ^= res;for (int i = 20; i >= 0; i--) {if (fa[u][i] && value[fa[u][i]] <= x) {u = fa[u][i];}}res = ans[u];printf("%d\n", res);}}return 0;
}
H. Great Cells
给定一个n×mn \times mn×m的网格,往里面填数,数字范围为[1,k][1, k][1,k],定义 great cell 是其所在行列的严格最大值,
AgA_gAg,表示如果有ggg个 great cell 的方案,要我们求∑g=0n×m(g+1)Ag\sum\limits_{g = 0} ^{n \times m} (g + 1) A_gg=0∑n×m(g+1)Ag。
∑g=0n×mAg+∑g=0n×mgAg\sum_{g = 0} ^{n \times m} A_g + \sum_{g = 0} ^{n \times m} g A_g\\ g=0∑n×mAg+g=0∑n×mgAg
容易发现前项答案为kn×mk ^{n \times m}kn×m,考虑后项如何求解,
L. World Cup(枚举、签到)
三进制枚举一下每一场比赛的胜负,然后用 map 存一下,然后判断答案即可。
#include <bits/stdc++.h>using namespace std;map<vector<int>, int> mp;const int a[6] = {1, 1, 1, 2, 2, 3}, b[6] = {2, 3, 4, 3, 4, 4};void init() {for (int i = 0; i < 729; i++) {vector<int> vt(5, 0);int cur = i;for (int j = 0; j < 6; j++) {if (cur % 3 == 0) {vt[a[j]]++, vt[b[j]]++;}else if (cur % 3 == 1) {vt[a[j]] += 3;}else {vt[b[j]] += 3;}cur /= 3;}mp[vt]++;}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);init();int T, cas = 0;scanf("%d", &T);while (T--) {vector<int> vt(5, 0);for (int i = 1; i <= 4; i++) {scanf("%d", &vt[i]);}printf("Case #%d: ", ++cas);if (!mp.count(vt)) {puts("Wrong Scoreboard");}else if (mp[vt] == 1) {puts("Yes");}else {puts("No");}}return 0;
}