CodeForces Round #547 Div.3
A. Game 23
#include <bits/stdc++.h> using namespace std; int N, M; int main() { int ans = 0; int cnt = 0; scanf("%d%d", &N, &M); if(N == M) ans = 0; else if(N > M) ans = -1; else { if(M % N) ans = -1; else { int t = M / N; while(t % 3 == 0) { t /= 3; cnt ++; } while(t % 2 == 0) { t /= 2; cnt ++; } if(t == 1) ans = cnt; else ans = -1; } } printf("%d\n", ans); return 0; }
B. Maximal Continuous Rest
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int N; int a[maxn]; int main() { scanf("%d", &N); int maxx = 0, cnt = 0; for(int i = 1; i <= N; i ++) { scanf("%d", &a[i]); if(a[i] == 1) { cnt ++; } else { cnt = 0; } maxx = max(cnt, maxx); } int num1 = 0; for(int i = 1; i <= N; i ++) { if(a[i] == 1) num1 ++; else break; } for(int i = N; i >= 1; i --) { if(a[i] == 1) num1 ++; else break; } printf("%d\n", max(maxx, num1)); return 0; }
C. Polycarp Restores Permutation
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 2e5 + 10; int N; long long a[maxn]; map<long long, int> vis; vector<long long> ans; int main() { scanf("%d", &N); for(int i = 1; i < N; i ++) scanf("%I64d", &a[i]); ans.push_back(1); long long minn = 1; for(int i = 1; i < N; i ++) { ans.push_back(a[i] + ans[i - 1]); minn = min(minn, a[i] + ans[i - 1]); } bool flag = true; long long sum; if(minn == 1) sum = 0; sum = 1 - minn; for(int i = 0; i < ans.size(); i ++) { ans[i] += sum; if(!vis[ans[i]] && ans[i] <= N) vis[ans[i]] ++; else { flag = false; break; } } if(!flag) printf("-1\n"); else { for(int i = 0; i < ans.size(); i ++) printf("%I64d%s", ans[i], i != ans.size() - 1 ? " " : "\n"); } return 0; }
D. Colored Boots
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int N; string s, t; int sz = 0, szz = 0; int vis[30], is[maxn], it[maxn]; map<char, int> mp; map<char, int> pos, pot; struct Node{ char c; int num; int loc; }node[maxn], mode[maxn]; vector<Node> v; bool cmp(const Node &a, const Node &b) { if(a.c != b.c) return a.c < b.c; else return a.loc < b.loc; } int main() { scanf("%d", &N); cin >> s >> t; int cnt = 0, cntt = 0; memset(vis, 0, sizeof(vis)); for(int i = 0; i < N; i ++) { if(s[i] >= 'a' && s[i] <= 'z') mp[s[i]] ++; if(s[i] == '?') is[sz ++] = i; } for(int i = 0; i < N; i ++) { if(mp[t[i]]) { vis[t[i] - 'a'] ++; cnt ++; node[cnt].loc = i; node[cnt].c = t[i]; mp[t[i]] --; } else { if(t[i] >= 'a' && t[i] <= 'z'){ if(sz < 1) continue; cnt ++; node[cnt].c = t[i]; node[cnt].loc = i; cntt ++; mode[cntt].c = t[i]; mode[cntt].loc = is[sz - 1]; sz --; } else it[szz ++] = i; } } for(int i = 0; i < N; i ++) { if(s[i] >= 'a' && s[i] <= 'z') { if(vis[s[i] - 'a']) { cntt ++; vis[s[i] - 'a'] --; mode[cntt].loc = i; mode[cntt].c = s[i]; } else { if(szz >= 1) { cntt ++; mode[cntt].c = s[i]; mode[cntt].loc = i; cnt ++; node[cnt].c = s[i]; node[cnt].loc = it[szz - 1]; szz --; } } } } if(sz) { for(int i = 0; i < sz; i ++) { cnt ++; node[cnt].loc = it[i]; node[cnt].c = 'h'; cntt ++; mode[cntt].loc = is[i]; mode[cntt].c = 'h'; } } sort(node + 1, node + 1 + cnt, cmp); sort(mode + 1, mode + 1 + cntt, cmp); printf("%d\n", cnt); for(int i = 1; i <= cnt; i ++) printf("%d %d\n", mode[i].loc + 1, node[i].loc + 1); return 0; }
E. Superhero Battle
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; long long H, N, temp; long long a[maxn]; long long sum, minn = 0; int main() { cin >> H >> N; bool flag = true; for(int i = 0; i < N; i ++) { cin >> a[i]; sum += a[i]; minn = min(minn, sum); if(minn + H <= 0 && flag) { flag = false; temp = i + 1; } } if(!flag) { cout << temp <<endl; return 0; } if(sum >= 0) printf("-1\n"); else { long long ans = 0; if(H + sum < 0) { for(int i = 0; i < N; i ++) { H += a[i]; if(H <= 0) { ans = i; break; } } cout << ans + 1 << endl; } else { long long p = 0; long long l = 0, r = H / (-1 * sum) + 1; while(l < r) { long long mid = (l + r) / 2; long long h = H; h += mid * sum; if(h + minn <= 0) { p = mid; r = mid; } else l = mid + 1; } ans += p * N; H += sum * p; for(int i = 0; i < N; i ++) { H += a[i]; ans ++; if(H <= 0) break; } cout << ans << endl; } } return 0; }
F1. Same Sum Blocks (Easy)
也太暴力了吧 但是 暴力出奇迹
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 1e7 + 10; int N; int a[maxn], sum[maxn]; struct Node { int num; vector<pair<int, int> > v; int vis[110]; }; map<int, Node> mp; vector<int> ans; int main() { scanf("%d", &N); memset(sum, 0, sizeof(sum)); for(int i = 1; i <= N; i ++) { scanf("%d", &a[i]); if(i == 1) sum[i] = a[i]; else sum[i] = sum[i - 1] + a[i]; } int maxx = -inf, temp = inf; for(int i = 1; i <= N; i ++) { for(int j = 1; j <= i; j ++) { bool flag = true; for(int k = j; k <= i; k ++) { if(mp[sum[i] - sum[j - 1]].vis[k]) { flag = false; break; } } if(flag) { mp[sum[i] - sum[j - 1]].v.push_back({j, i}); mp[sum[i] - sum[j - 1]].num ++; for(int k = j - 1; k <= i; k ++) mp[sum[i] - sum[j - 1]].vis[k] = 1; } if(mp[sum[i] - sum[j - 1]].num > maxx) { temp = sum[i] - sum[j - 1]; maxx = mp[temp].num; } } } printf("%d\n", mp[temp].v.size()); for(int i = 0; i < mp[temp].v.size(); i ++) printf("%d %d\n", mp[temp].v[i].first, mp[temp].v[i].second); return 0; }
F2. Same Sum Blocks (Hard)
贪心排序求最多不相交区间要按照右端点排序 因为直接 copy 了上一题的代码进来改 Node 里面的 vis 忘记删 内存超限三发 枯了 这个也是个小暴力
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 1510; int N; int a[maxn], sum[maxn]; struct Node { int num; vector<pair<int, int> > v, out; }; map<int, Node> mp; vector<int> ans; map<int, int> appear; bool cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; } vector<pair<int, int> > MaxIntersect(vector<pair<int, int> > a) { int n = a.size(); vector<pair<int, int> > anss; anss.push_back({a[0].first, a[0].second}); int en = a[0].second; for(int i = 1; i < n; i ++) { if(a[i].first <= en) continue; en = a[i].second; anss.push_back({a[i].first, a[i].second}); } return anss; } int main() { scanf("%d", &N); memset(sum, 0, sizeof(sum)); for(int i = 1; i <= N; i ++) { scanf("%d", &a[i]); if(i == 1) sum[i] = a[i]; else sum[i] = sum[i - 1] + a[i]; } appear.clear(); for(int i = 1; i <= N; i ++) { for(int j = 1; j <= i; j ++) { if(appear[sum[i] - sum[j - 1]] == 0) ans.push_back(sum[i] - sum[j - 1]); appear[sum[i] - sum[j - 1]] = 1; mp[sum[i] - sum[j - 1]].v.push_back({j, i}); mp[sum[i] - sum[j - 1]].num ++; } } int maxx = 0, temp = 0; for(int i = 0; i < ans.size(); i ++) { sort(mp[ans[i]].v.begin(), mp[ans[i]].v.end(), cmp); mp[ans[i]].out = MaxIntersect(mp[ans[i]].v); if(mp[ans[i]].out.size() > maxx) { maxx = mp[ans[i]].out.size(); temp = ans[i]; } } printf("%d\n", mp[temp].out.size()); for(int i = 0; i < mp[temp].out.size(); i ++) printf("%d %d\n", mp[temp].out[i].first, mp[temp].out[i].second); return 0; } /* 100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 */