Wannafly挑战赛19
Posted aguin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Wannafly挑战赛19相关的知识,希望对你有一定的参考价值。
随便on
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1e5 + 10; 4 int q[maxn], f[maxn], b[maxn], vis[maxn]; 5 stack<int> F, B; 6 vector<int> ans; 7 8 int main() { 9 int N, Q; 10 scanf("%d", &N); 11 for(int i = 1; i <= N; ++i) scanf("%d", q + i); 12 scanf("%d", &Q); 13 while(Q--) { 14 int x; 15 char s[11]; 16 scanf("%s %d", s, &x); 17 if(s[0] == ‘F‘) f[x] = 1, b[x] = 0, F.push(x); 18 else f[x] = 0, b[x] = 1, B.push(x); 19 } 20 while(!F.empty()) { 21 int x = F.top(); F.pop(); 22 if(!f[x] || vis[x]) continue; 23 vis[x] = 1, ans.push_back(x); 24 } 25 for(int i = 1; i <= N; ++i) { 26 if(b[q[i]] || vis[q[i]]) continue; 27 vis[q[i]] = 1, ans.push_back(q[i]); 28 } 29 while(!B.empty()) { 30 int x = B.top(); B.pop(); 31 if(!b[x] || vis[x]) continue; 32 vis[x] = 1, F.push(x); 33 } 34 while(!F.empty()) { 35 int x = F.top(); F.pop(); 36 ans.push_back(x); 37 } 38 for(int i = 0; i < N; ++i) printf("%d%c", ans[i], i == N - 1 ? ‘ ‘ : ‘ ‘); 39 return 0; 40 }
单调队列
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 LL M[505][505], sum[505][505], zero[505][505]; 5 deque<int> dq; 6 7 int main() { 8 int R, C, X, Y, Z; 9 scanf("%d %d %d %d %d", &R, &C, &X, &Y, &Z); 10 for(int i = 1; i <= R; ++i) { 11 for(int j = 1; j <= C; ++j) { 12 scanf("%lld", M[i] + j); 13 sum[i][j] = sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1] + M[i][j]; 14 zero[i][j] = zero[i-1][j] + zero[i][j-1] - zero[i-1][j-1] + (M[i][j] == 0 ? 1 : 0); 15 } 16 } 17 LL ans = 0; 18 for(int U = 1; U <= R; ++U) { 19 for(int D = U; D <= min(R, U + X - 1); ++D) { 20 dq.clear(); 21 dq.push_back(0); 22 for(int i = 1; i <= C; ++i) { 23 while(!dq.empty() && (i - dq.front() > Y || (zero[D][i] - zero[U-1][i]) - (zero[D][dq.front()] - zero[U-1][dq.front()]) > Z)) dq.pop_front(); 24 LL x = sum[D][i] - sum[U-1][i]; 25 if(!dq.empty()) ans = max(ans, x - sum[D][dq.front()] + sum[U-1][dq.front()]); 26 while(!dq.empty() && x <= sum[D][dq.back()] - sum[U-1][dq.back()]) dq.pop_back(); 27 dq.push_back(i); 28 } 29 } 30 } 31 printf("%lld ", ans); 32 return 0; 33 }
枚举色集容斥
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 5e4 + 10; 4 typedef long long LL; 5 const LL mod = 1e9 + 7; 6 vector<int> G[maxn]; 7 int A[maxn], vis[maxn]; 8 LL cnt, ans[2222]; 9 10 void dfs(int x, int msk) { 11 vis[x] = 1, cnt++; 12 for(int i = 0; i < G[x].size(); ++i) { 13 int to = G[x][i]; 14 if(vis[to] || !((1 << (A[to] - 1)) & msk)) continue; 15 dfs(to, msk); 16 } 17 } 18 19 int main() { 20 int N, K; 21 scanf("%d %d", &N, &K); 22 for(int i = 1; i <= N; ++i) scanf("%d", A + i); 23 for(int i = 1; i < N; ++i) { 24 int u, v; 25 scanf("%d %d", &u, &v); 26 G[u].push_back(v); 27 G[v].push_back(u); 28 } 29 for(int i = 0; i < (1 << K); ++i) { 30 for(int j = 1; j <= N; ++j) vis[j] = 0; 31 for(int j = 1; j <= N; ++j) { 32 if(!vis[j] && ((1 << (A[j] - 1)) & i)) { 33 cnt = 0, dfs(j, i); 34 ans[i] = (ans[i] + cnt * (cnt - 1) / 2 + cnt) % mod; 35 } 36 } 37 } 38 for(int j = 0; j < K; ++j) { 39 for (int i = 0; i < (1 << K); ++i) { 40 if (i & (1 << j)) ans[i] = (ans[i] + mod - ans[i ^ (1 << j)]) % mod; 41 } 42 } 43 LL ret = 0; 44 for(int i = 0; i < (1 << K); ++i) { 45 LL base = 1; 46 for(int j = 0; j < K; ++j) { 47 if(i & (1 << j)) base = base * 131 % mod; 48 } 49 ret = (ret + ans[i] * base) % mod; 50 } 51 printf("%lld ", ret); 52 return 0; 53 }
马拉车前后缀预处理完枚举回文中心
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1e5 + 10; 4 int A[26], B[26], p[maxn<<1]; 5 typedef long long LL; 6 const LL INF = 1e18; 7 LL L[maxn], R[maxn], SL[maxn], SR[maxn]; 8 char s[maxn], ss[maxn<<1]; 9 10 int main() { 11 scanf("%s", s + 1); 12 int l = strlen(s + 1); 13 for (int i = 0; i < 26; ++i) scanf("%d %d", A + i, B + i); 14 LL m = 0, sum = 0; 15 for (int i = 1; i <= l; ++i) { 16 sum += A[s[i] - ‘a‘] - B[s[i] - ‘a‘]; 17 L[i] = sum - m; 18 SL[i] = SL[i - 1] + A[s[i] - ‘a‘]; 19 m = min(m, sum); 20 } 21 m = 0, sum = 0; 22 for (int i = l; i >= 1; --i) { 23 sum += A[s[i] - ‘a‘] - B[s[i] - ‘a‘]; 24 R[i] = sum - m; 25 SR[i] = SR[i + 1] + A[s[i] - ‘a‘]; 26 m = min(m, sum); 27 } 28 int len = 2 * l + 1; 29 ss[0] = ‘$‘; 30 for (int i = 1; i <= len; i++) { 31 if (i % 2) ss[i] = ‘#‘; 32 else ss[i] = s[i / 2]; 33 } 34 ss[len + 1] = ‘^‘; 35 int mx = 0, id; 36 for (int i = 1; i <= len; i++) { 37 if (mx > i) p[i] = min(p[2 * id - i], mx - i); 38 else p[i] = 1; 39 while (ss[i + p[i]] == ss[i - p[i]]) p[i]++; 40 if (p[i] + i > mx) { 41 mx = p[i] + i; 42 id = i; 43 } 44 } 45 LL ans = INF; 46 for (int i = 1; i <= len; ++i) { 47 if (ss[i] == ‘#‘) { 48 if (p[i] == 1) continue; 49 int lb = i / 2 - p[i] / 2 + 1, rb = i / 2 + p[i] / 2; 50 ans = min(ans, SL[lb-1] + SR[rb+1] - max(L[lb-1], R[rb+1])); 51 } else { 52 int lb = i / 2 - p[i] / 2 + 1, rb = i / 2 + p[i] / 2 - 1; 53 ans = min(ans, SL[lb-1] + SR[rb+1] - max(L[lb-1], R[rb+1])); 54 } 55 } 56 printf("%lld ", ans); 57 return 0; 58 }
预处理bfs转移要o1 想不到建图
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 // SPFA_min_cost_flow 5 const int INF = 1e9; 6 const int maxn = 1e6; 7 int dist[555], vis[555]; 8 int pv[555], pe[555]; 9 int cnt, h[555]; 10 11 struct edge { 12 int to, pre, cap, cost; 13 } e[maxn<<1]; 14 15 void init() { //Don‘t forget 16 cnt = 0; 17 memset(h, -1, sizeof(h)); 18 } 19 20 void add(int from, int to, int cap, int cost) { 21 e[cnt].pre = h[from]; 22 e[cnt].to = to; 23 e[cnt].cap = cap; 24 e[cnt].cost = cost; 25 h[from] = cnt; 26 cnt++; 27 } 28 29 void ad(int from, int to, int cap, int cost) { 30 add(from, to, cap, cost); 31 add(to, from, 0, -cost); 32 } 33 34 int min_cost_flow(int s, int t) { 35 int ret = 0; 36 while (1) { 37 memset(vis, 0, sizeof(vis)); 38 for (int i = 0; i < 555; i++) dist[i] = INF; 39 dist[s] = 0; 40 queue<int> q; 41 q.push(s); 42 while (!q.empty()) { 43 int v = q.front(); 44 q.pop(); 45 vis[v] = 0; 46 for (int i = h[v]; i >= 0; i = e[i].pre) { 47 int to = e[i].to, cap = e[i].cap, cost = e[i].cost; 48 if (cap > 0 && dist[to] > dist[v] + cost) { 49 pv[to] = v, pe[to] = i; 50 dist[to] = dist[v] + cost; 51 if (!vis[to]) q.push(to); 52 vis[to] = 1; 53 } 54 } 55 } 56 if (dist[t] >= 0) return ret; // modify here 57 int d = INF; 58 for (int v = t; v != s; v = pv[v]) 59 d = min(d, e[pe[v]].cap); 60 ret += d * dist[t]; 61 for (int v = t; v != s; v = pv[v]) { 62 e[pe[v]].cap -= d; 63 e[pe[v] ^ 1].cap += d; 64 } 65 } 66 } 67 68 char SA[55][22], SB[55][22]; 69 int da[55], db[55], ma[55], mb[55]; 70 vector<int> G[55], D[55]; 71 72 queue<int> q; 73 int dis[1<<16], base[22]; 74 75 int get(char * s, int la) { 76 int ret = 0; 77 for (int i = 0; i < la; ++i) ret += (s[i] - ‘0‘) * base[i]; 78 return ret; 79 } 80 81 vector<int> rev[17]; 82 int main() { 83 for (int i = 0; i < 22; ++i) base[i] = 1 << i; 84 for (int i = 2; i <= 16; ++i) { 85 for (int j = 0; j < (1 << i); ++j) { 86 int o = 0; 87 for (int k = 0; k < i; ++k) 88 if (j & (1 << k)) o += (1 << (i - k - 1)); 89 rev[i].push_back(o); 90 } 91 } 92 int N, M, ans = 0; 93 scanf("%d", &N); 94 for (int i = 1; i <= N; ++i) scanf("%s", SA[i]); 95 scanf("%d", &M); 96 for (int i = 1; i <= M; ++i) scanf("%s", SB[i]); 97 for (int i = 1; i <= N; ++i) scanf("%d", da + i), ans += da[i]; 98 for (int i = 1; i <= M; ++i) scanf("%d", db + i), ans += db[i]; 99 for (int i = 1; i <= N; ++i) scanf("%d", ma + i); 100 for (int i = 1; i <= M; ++i) scanf("%d", mb + i); 101 for (int i = 1; i <= N; ++i) { 102 int la = strlen(SA[i]); 103 for (int j = 0; j < (1 << la); ++j) dis[j] = INF; 104 int st = get(SA[i], la); 105 dis[st] = 0; 106 q.push(st); 107 while (!q.empty()) { 108 int x = q.front(); 109 q.pop(); 110 for (int L = 0; L < la; ++L) { 111 for (int R = L + 1; R < la; ++R) { 112 int low = x & (base[L] - 1); 113 int high = (x >> (R + 1)) << (R + 1); 114 int mid = rev[R - L + 1][((x - high) >> L)] << L; 115 int t = low + mid + high; 116 if (dis[t] == INF) dis[t] = dis[x] + 1, q.push(t); 117 } 118 } 119 } 120 for (int j = 1; j <= M; ++j) 121 if (strlen(SB[j]) == la && dis[get(SB[j], la)] != INF) 122 G[i].push_back(j), D[i].push_back(dis[get(SB[j], la)] * min(ma[i], mb[j]) - da[i] - db[j]); 123 } 124 init(); 125 int S = N + M + 1, T = S + 1; 126 for (int i = 1; i <= N; ++i) ad(S, i, 1, 0); 127 for (int i = 1; i <= M; ++i) ad(N + i, T, 1, 0); 128 for (int i = 1; i <= N; ++i) 129 for (int j = 0; j < G[i].size(); ++j) 130 ad(i, N + G[i][j], 1, D[i][j]); 131 printf("%d ", ans + min_cost_flow(S, T)); 132 return 0; 133 }
哈希莫队
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 3e4 + 10; 4 char s[maxn]; 5 6 int sz, ql[maxn], qr[maxn], id[maxn]; 7 bool cmp(int i, int j) { 8 if(ql[i] / sz != ql[j] / sz) return ql[i] / sz < ql[j] / sz; 9 return qr[i] < qr[j]; 10 } 11 12 int sum[26]; 13 typedef long long LL; 14 const LL base = 131, mod = 1e9 + 7; 15 vector<LL> v; 16 LL c[maxn], ans[maxn]; 17 18 int cnt[maxn]; 19 int main() { 20 int K, Q; 21 scanf("%d %s %d", &K, s + 1, &Q); 22 for(int i = 1; i <= Q; ++i) scanf("%d %d", ql + i, qr + i), id[i] = i; 23 sz = sqrt(Q + 0.5), sort(id + 1, id + 1 + Q, cmp); 24 int l = strlen(s + 1); 25 for(int j = 0; j < 26; ++j) c[0] = (c[0] * base + sum[j]) % mod; 26 v.push_back(c[0]); 27 for(int i = 1; i <= l; ++i) { 28 sum[s[i] - ‘a‘] = (sum[s[i] - ‘a‘] + 1) % K; 29 for(int j = 0; j < 26; ++j) c[i] = (c[i] * base + sum[j]) % mod; 30 v.push_back(c[i]); 31 } 32 sort(v.begin(), v.end()); 33 v.erase(unique(v.begin(), v.end()), v.end()); 34 for(int i = 0; i <= l; ++i) c[i] = lower_bound(v.begin(), v.end(), c[i]) - v.begin() + 1; 35 LL tmp = 0; 36 int L = 1, R = 0; 37 cnt[c[0]]++; 38 for(int i = 1; i <= Q; ++i) { 39 int x = id[i]; 40 while (L < ql[x]) { 41 cnt[c[L-1]]--; 42 tmp -= cnt[c[L-1]]; 43 ++L; 44 } 45 while (L > ql[x]) { 46 --L; 47 tmp += cnt[c[L-1]]; 48 cnt[c[L-1]]++; 49 } 50 while (R < qr[x]) { 51 ++R; 52 tmp += cnt[c[R]]; 53 cnt[c[R]]++; 54 } 55 while (R > qr[x]) { 56 cnt[c[R]]--; 57 tmp -= cnt[c[R]]; 58 --R; 59 } 60 ans[x] = tmp; 61 } 62 for(int i = 1; i <= Q; ++i) printf("%lld ", ans[i]); 63 return 0; 64 }
以上是关于Wannafly挑战赛19的主要内容,如果未能解决你的问题,请参考以下文章