Wannafly挑战赛15
Posted Algorithms Crush Me
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Wannafly挑战赛15相关的知识,希望对你有一定的参考价值。
贪心
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1e5 + 10; 4 int a[maxn], b[maxn], c[maxn], id[maxn]; 5 multiset<int> S; 6 7 bool cmp(int i, int j){ 8 return b[i] > b[j]; 9 } 10 11 int main(){ 12 int n, m, p = 0; 13 scanf("%d %d", &n, &m); 14 for(int i = 1; i <= n; ++i) scanf("%d", a + i); 15 for(int i = 1; i <= m; ++i){ 16 scanf("%d %d", b + i, c + i); 17 id[i] = i; 18 } 19 sort(a + 1, a + 1 + n); 20 sort(id + 1, id + 1 + m, cmp); 21 long long ans = 0; 22 for(int i = n; i >= 1; i--){ 23 while(p < m && b[id[p + 1]] >= a[i]){ 24 ++p; 25 S.insert(c[id[p]]); 26 } 27 if(S.empty()){ans = -1; break;} 28 ans += *S.begin(); 29 S.erase(S.begin()); 30 } 31 cout << ans << endl; 32 return 0; 33 }
贪心
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1e5 + 10; 4 int cnt[6]; 5 6 int main(){ 7 int n, x; 8 scanf("%d", &n); 9 for(int i = 1; i <= n; ++i){ 10 scanf("%d", &x); 11 cnt[x]++; 12 } 13 int ans = cnt[5]; 14 while(cnt[1] + cnt[2] + cnt[3] + cnt[4]){ 15 int r = 5; 16 ans++; 17 while(r >= 4 && cnt[4]) r -= 4, cnt[4]--; 18 while(r >= 3 && cnt[3]) r -= 3, cnt[3]--; 19 while(r >= 2 && cnt[2]) r -= 2, cnt[2]--; 20 while(r >= 1 && cnt[1]) r -= 1, cnt[1]--; 21 } 22 cout << ans << endl; 23 return 0; 24 }
出题人骗我
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 5 int main(){ 6 LL n, q, x; 7 scanf("%lld %lld", &n, &q); 8 while(q--){ 9 scanf("%lld", &x); 10 LL y = n, ans = 1; 11 while(1){ 12 if(x % 2) {ans += x / 2; break;} 13 x /= 2; 14 if(y % 2) x++; 15 ans += y / 2; 16 y -= y / 2; 17 } 18 printf("%lld\n", ans); 19 } 20 return 0; 21 }
随便计数
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1e5 + 10; 4 typedef long long LL; 5 6 int c[10][maxn]; 7 int lowbit(int s) { 8 return s & (-s); 9 } 10 void modify(int o, int x, int v) { 11 for (int i = x; i < maxn; i += lowbit(i)) c[o][i] += v; 12 return; 13 } 14 int query(int o, int x) { 15 int ret = 0; 16 for (int i = x; i > 0; i -= lowbit(i)) ret += c[o][i]; 17 return ret; 18 } 19 20 char s[maxn]; 21 int main(){ 22 int q, L, R; 23 scanf("%s %d %d %d", s + 1, &q, &L, &R); 24 int l = strlen(s + 1); 25 LL ans = 0; 26 for(int i = 1; i <= l; ++i){ 27 int x = s[i] - ‘0‘; 28 modify(x, i, 1); 29 for(int j = x + 1; j <= 9; ++j) ans += query(j, i - L + 1) - query(j, i - R); 30 } 31 while(q--){ 32 int i, x; 33 scanf("%d %d", &i, &x); 34 int y = s[i] - ‘0‘; 35 modify(y, i, -1); 36 for(int j = 0; j <= 9; ++j) { 37 if (j < x) ans += query(j, min(l, i + R - 1)) - query(j, min(l, i + L - 2)); 38 else if (j > x) ans += query(j, i - L + 1) - query(j, i - R); 39 if (j < y) ans -= query(j, min(l, i + R - 1)) - query(j, min(l, i + L - 2)); 40 else if (j > y) ans -= query(j, i - L + 1) - query(j, i - R); 41 } 42 modify(x, i, 1); 43 s[i] = ‘0‘ + x; 44 printf("%lld\n", ans); 45 } 46 return 0; 47 }
显然斜着扫
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 1e6 + 10; 5 int x[maxn], y[maxn], id[maxn], P, Q; 6 7 bool cmp(int i, int j){ 8 return (double) x[i] * P + (double) y[i] * Q < (double) x[j] * P + (double) y[j] * Q; 9 } 10 11 double cal(int i){ 12 double k1 = x[id[i]+1] == x[id[i]] ? 1e18 : 1.0 * (y[id[i]+1] - y[id[i]]) / (x[id[i]+1] - x[id[i]]); 13 double k2 = Q == 0 ? 1e18 : 1.0 * P / Q; 14 return fabs(k1 - k2); 15 } 16 17 LL gcd(LL a, LL b){ 18 if(a == 0) return b; 19 if(b == 0) return a; 20 return a % b ? gcd(b, a % b) : b; 21 } 22 23 int main(){ 24 int n; 25 scanf("%d %d %d", &n, &P, &Q); 26 for(int i = 1; i <= n; ++i){ 27 scanf("%d %d", x + i, y + i); 28 id[i] = i; 29 } 30 sort(id + 1, id + 1 + n, cmp); 31 int p = 1; 32 double m = cal(1); 33 for(int i = 2; i < n; ++i) 34 if(cal(i) < m) m = cal(i), p = i; 35 LL dy = y[id[p]+1] - y[id[p]], dx = x[id[p]+1] - x[id[p]]; 36 LL g = gcd(abs(dx), abs(dy)); 37 if(dx < 0 && dy < 0) dx = -dx, dy = -dy; 38 dx /= g, dy /= g; 39 printf("%lld/%lld\n", dy, dx); 40 return 0; 41 }
优秀分块
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 111111; 4 int sg[maxn], cnt[maxn], b[maxn]; 5 vector<int> G[maxn]; 6 7 struct query { 8 int l, r; 9 } q[maxn]; 10 int block; 11 12 bool cmp(query A, query B) { 13 int a = A.l / block, b = B.l / block; 14 if (a != b) return a < b; 15 return A.r < B.r; 16 } 17 18 inline void Add(int x) { 19 cnt[x]++; 20 if (cnt[x] == 1) b[x / block]++; 21 } 22 23 inline void Minus(int x) { 24 cnt[x]--; 25 if (cnt[x] == 0) b[x / block]--; 26 } 27 28 int get() { 29 for (int j = 0; ; ++j) 30 if (b[j] != block) 31 for (int k = j * block; ; ++k) 32 if (!cnt[k]) return k; 33 } 34 35 int main() { 36 int n, m, k; 37 scanf("%d %d %d", &n, &m, &k); 38 assert(1 <= n && n <= 100000 && 1 <= m && m <= 200000 && 1 <= k && k <= 100000); 39 block = sqrt(n + 0.5); 40 for (int i = 1; i <= m; ++i) { 41 int u, v; 42 scanf("%d %d", &u, &v); 43 assert(1 <= v && v < u && u <= n); 44 G[u].push_back(v); 45 } 46 for (int i = 1; i <= n; ++i) { 47 for (int j = 0; j < G[i].size(); ++j) Minus(sg[G[i][j]]); 48 sg[i] = get(); 49 for (int j = 0; j < G[i].size(); ++j) Add(sg[G[i][j]]); 50 Add(sg[i]); 51 } 52 for (int i = 1; i <= k; ++i) scanf("%d %d", &q[i].l, &q[i].r); 53 sort(q + 1, q + 1 + k, cmp); 54 memset(cnt, 0, sizeof(cnt)); 55 memset(b, 0, sizeof(b)); 56 int r = 0, l = 1, ans = 0; 57 for (int i = 1; i <= k; ++i) { 58 while (l < q[i].l) Minus(sg[l++]); 59 while (l > q[i].l) Add(sg[--l]); 60 while (r < q[i].r) Add(sg[++r]); 61 while (r > q[i].r) Minus(sg[r--]); 62 ans ^= get(); 63 } 64 puts(ans ? "Alice" : "Bob"); 65 return 0; 66 }
以上是关于Wannafly挑战赛15的主要内容,如果未能解决你的问题,请参考以下文章