Mishka and Contest
#pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JUDGE #include "stdafx.h" #else #include<bits/stdc++.h> #endif using namespace std; typedef long long lint; typedef vector<int> VI; typedef pair<int, int> PII; typedef queue<int> QI; typedef map<int, int> MII; void makedata() { freopen("input.txt", "w", stdout); fclose(stdout); } int a[200]; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif //makedata(); std::ios::sync_with_stdio(0), cin.tie(0); int n, k; cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; int l = n, r = -1; for (int i = 0; i < n; i++) { if (a[i] > k) { l = i; break; } } for (int i = n - 1; i >= 0; i--) { if (a[i] > k) { r = i; break; } } if (l > r) cout << n << endl; else cout << (n - (r - l + 1)) << endl; return 0; }
Reversing Encryption
splay树,模拟也可以
#pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JUDGE #include "stdafx.h" #else #include<bits/stdc++.h> #endif using namespace std; typedef long long lint; typedef vector<int> VI; typedef pair<int, int> PII; typedef queue<int> QI; typedef map<int, int> MII; void makedata() { freopen("input.txt", "w", stdout); fclose(stdout); } class SplayNode { public: SplayNode *child[2]; char value; int size; bool flip; SplayNode(char c) : value(c), size(1), flip(false) { child[0] = child[1] = NULL; } int getPosition()const { return child[0] ? child[0]->size + 1 : 1; } void maintain() { size = 1; if (child[0]) { size += child[0]->size; } if (child[1]) { size += child[1]->size; } } void pushDown() { if (flip) { swap(child[0], child[1]); for (int i = 0; i < 2; i++) { if (child[i]) { child[i]->flip ^= 1; } } flip = false; } } }; class SplayTree { public: SplayNode *root; void init(char *a, int n); void build(SplayNode *&node, char *begin, char *end); void rotate(SplayNode *&node, int direction); void splay(SplayNode *&node, int position); void reverse(int begin, int end); void traverse(SplayNode *u); void traverse(); }; void SplayTree::init(char *a, int n) { build(root, a, a + n - 1); } void SplayTree::build(SplayNode *&node, char *begin, char *end) { if (begin > end) { return; } char *middle = begin + ((end - begin) >> 1); node = new SplayNode(*middle); build(node->child[0], begin, middle - 1); build(node->child[1], middle + 1, end); node->maintain(); } void SplayTree::rotate(SplayNode *&node, int direction) { SplayNode *child = node->child[direction ^ 1]; node->child[direction ^ 1] = child->child[direction]; child->child[direction] = node; node->maintain(); child->maintain(); node = child; } void SplayTree::splay(SplayNode *&node, int position) { node->pushDown(); if (node->getPosition() != position) { int d = node->getPosition() < position; SplayNode *node2 = node->child[d]; position -= d ? node->getPosition() : 0; node2->pushDown(); if (node2->getPosition() != position) { int d2 = node2->getPosition() < position; position -= d2 ? node2->getPosition() : 0; splay(node2->child[d2], position); if (d == d2) { rotate(node, d ^ 1); } else { rotate(node->child[d], d); } } rotate(node, d ^ 1); } } void SplayTree::reverse(int begin, int end) { splay(root, begin); splay(root->child[1], end - begin + 2); root->child[1]->child[0]->flip ^= 1; } void SplayTree::traverse(SplayNode *u) { if (!u) { return; } u->pushDown(); traverse(u->child[0]); if (u->value) { printf("%c", u->value); } traverse(u->child[1]); } void SplayTree::traverse() { traverse(root); } SplayTree st; char a[200]; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif //makedata(); std::ios::sync_with_stdio(0), cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; st.init(a, n + 2); for (int i = 1; i <= n; i++) { if (n % i == 0) st.reverse(1, i); } st.traverse(); return 0; }
Alphabetic Removals
#pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JUDGE #include "stdafx.h" #else #include<bits/stdc++.h> #endif using namespace std; typedef long long lint; typedef vector<int> VI; typedef pair<int, int> PII; typedef queue<int> QI; typedef map<int, int> MII; void makedata() { freopen("input.txt", "w", stdout); fclose(stdout); } string s; VI v[26], u; bool f[500000]; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif //makedata(); std::ios::sync_with_stdio(0), cin.tie(0); int n, k; cin >> n >> k; cin >> s; for (int i = 0; i < n; i++) v[s[i] - 'a'].push_back(i); for (int i = 0; i < 26; i++) { for (int j = 0; j < v[i].size(); j++) { u.push_back(v[i][j]); } } memset(f, true, sizeof(f)); for (int i = 0; i < k; i++) f[u[i]] = false; for (int i = 0; i < n; i++) { if (f[i]) cout << s[i]; } return 0; }
Equalize the Remainders
贪心
#pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JUDGE #include "stdafx.h" #else #include<bits/stdc++.h> #endif using namespace std; typedef long long lint; typedef vector<int> VI; typedef pair<int, int> PII; typedef queue<int> QI; typedef map<int, int> MII; void makedata() { freopen("input.txt", "w", stdout); fclose(stdout); } int cnt[250000], nex[250000]; int n, m, x, r, p, q, t; VI v; int getNext(int r) { if (cnt[nex[r]] < q) return nex[r]; else { nex[r] = getNext(nex[r]); return nex[r]; } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif //makedata(); std::ios::sync_with_stdio(0), cin.tie(0); lint ans = 0; cin >> n >> m; q = n / m; memset(cnt, 0, sizeof(cnt)); for (int i = 0; i < m; i++) nex[i] = i + 1; nex[m - 1] = 0; for (int i = 0; i < n; i++) { cin >> x; r = x % m; if (cnt[r] < q) p = r; else p = getNext(r); cnt[p]++; ans += (p + m - r) % m; v.push_back(x + (p + m - r) % m); } cout << ans << endl; for (int i = 0; i < n; i++) cout << v[i] << ' '; return 0; }
Reachability from the Capital
染色,色块数减一
#pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JUDGE #include "stdafx.h" #else #include<bits/stdc++.h> #endif using namespace std; typedef long long lint; typedef vector<int> VI; typedef pair<int, int> PII; typedef queue<int> QI; typedef map<int, int> MII; void makedata() { freopen("input.txt", "w", stdout); fclose(stdout); } VI G[5500]; int color[5500]; bool f[5500]; void dfs(int x, int c) { for (int i = 0; i < G[x].size(); i++) { int v = G[x][i]; if (color[v] != c) { color[v] = c; dfs(v, c); } } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif //makedata(); std::ios::sync_with_stdio(0), cin.tie(0); int n, m, s, u, v; cin >> n >> m >> s; for (int i = 0; i < m; i++) { cin >> u >> v; if (v == s) continue; G[u].push_back(v); } memset(color, 0, sizeof(color)); for (int i = 1; i <= n; i++) { if (color[i] == 0) { color[i] = i; dfs(i, i); } } memset(f, false, sizeof(f)); int ans = 0; for (int i = 1; i <= n; i++) { if (!f[color[i]]) ans++; f[color[i]] = true; } cout << ans - 1 << endl; return 0; }
Cards and Joy
分成若干个子问题,动态规划
#pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JUDGE #include "stdafx.h" #else #include<bits/stdc++.h> #endif using namespace std; typedef long long lint; typedef vector<int> VI; typedef pair<int, int> PII; typedef queue<int> QI; typedef map<int, int> MII; void makedata() { freopen("input.txt", "w", stdout); fclose(stdout); } int f[600][6000]; int p[150000], h[6000], c[150000], k; int dp(int n, int m) { int rtn = 0; for (int i = 0; i <= k; i++) f[0][i] = 0; for (int i = 0; i <= n; i++) f[i][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m && j <= i * k; j++) { f[i][j] = 0; for (int q = 0; q <= k && q <= j; q++) { f[i][j] = max(f[i][j], f[i - 1][j - q] + h[q]); } rtn = max(rtn, f[i][j]); } } return rtn; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif //makedata(); std::ios::sync_with_stdio(0), cin.tie(0); int n, x; cin >> n >> k; for (int i = 0; i < n * k; i++) { cin >> x; c[x]++; } for (int i = 0; i < n; i++) { cin >> x; p[x]++; } for (int i = 1; i <= k; i++) cin >> h[i]; int ans = 0; for (int i = 0; i <= 100000; i++) { if (p[i] == 0 || c[i] == 0) continue; else ans += dp(p[i], c[i]); } cout << ans << endl; return 0; }