[关闭]
@ljt12138 2017-06-10T17:43:30.000000Z 字数 2396 阅读 748

Codeforces Round #413(Div. 1 + Div. 2)


C. Fountains

首先特判买一个C一个D的情况。
考虑买两个C(或D),先按照价格排个序,枚举一个位置,则另一个可行位置构成一个区间。然后用ST表区间最值即可。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 100005;
  4. struct ft {
  5. int b, c;
  6. friend bool operator < (const ft &a, const ft &b)
  7. { return a.c < b.c; }
  8. } coin[MAXN], dm[MAXN];
  9. int top1 = 0, top2 = 0;
  10. int max_c[MAXN], max_d[MAXN];
  11. int n, c, d;
  12. int f[MAXN][25], lg[MAXN];
  13. int max_val(int i, int j)
  14. {
  15. if (i > j) return 0;
  16. int k = lg[j-i+1];
  17. return max(f[i][k], f[j-(1<<k)+1][k]);
  18. }
  19. int max_val_without(int i, int j)
  20. {
  21. if (i < j) return max_val(1, i);
  22. return max(max_val(1, j-1), max_val(j+1, i));
  23. }
  24. int solve(ft a[MAXN], int top, int M)
  25. {
  26. sort(a+1, a+top+1);
  27. memset(f, 0, sizeof f);
  28. for (int i = 1; i <= top; i++) f[i][0] = a[i].b;
  29. for (int j = 1; j <= 20; j++)
  30. for (int i = 1; i <= top; i++) {
  31. f[i][j] = f[i][j-1];
  32. if (i+(1<<(j-1)) <= top) f[i][j] = max(f[i][j], f[i+(1<<(j-1))][j-1]);
  33. }
  34. int Rt = top, ans = 0;
  35. for (int i = 1; i <= top; i++) {
  36. if (a[i].c > M) break;
  37. while (Rt && (a[Rt].c+a[i].c > M || Rt == i)) Rt--;
  38. if (Rt == 0) break;
  39. ans = max(ans, max_val_without(Rt, i)+a[i].b);
  40. }
  41. //cout << ans << endl;
  42. return ans;
  43. }
  44. int main()
  45. {
  46. scanf("%d %d %d", &n, &c, &d);
  47. int j = 1, t = 0;
  48. for (int i = 1; i <= n; i++) {
  49. while (j*2 <= i) j <<= 1, t++;
  50. lg[i] = t;
  51. //cout << lg[i] << " ";
  52. }
  53. //cout << endl;
  54. for (int i = 1; i <= n; i++) {
  55. int a, b; char ch;
  56. scanf("%d %d %c", &a, &b, &ch);
  57. // cout << a << " " << b << "," << c << endl;
  58. if (ch == 'C') coin[++top1].b = a, coin[top1].c = b, max_c[b] = max(max_c[b], a);
  59. else dm[++top2].b = a, dm[top2].c = b, max_d[b] = max(max_d[b], a);
  60. }
  61. max_d[0] = 0, max_c[0] = 0;
  62. for (int i = 1; i <= c; i++) max_c[i] = max(max_c[i], max_c[i-1]);
  63. for (int i = 1; i <= d; i++) max_d[i] = max(max_d[i], max_d[i-1]);
  64. // case1
  65. int ans = 0;
  66. if (max_d[d] != 0 && max_c[c] != 0)
  67. ans = max(ans, max_d[d]+max_c[c]);
  68. // case2
  69. ans = max(ans, max(solve(coin, top1, c), solve(dm, top2, d)));
  70. cout << ans << endl;
  71. return 0;
  72. }

D. Field expansion

大力爆搜..CF机子也真是快...

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. long long a, b, h, w, n;
  4. long long ai[100005];
  5. long long dat[100005], top = 0;
  6. int tms[100005];
  7. int dfs(int nd, long long h, long long w)
  8. {
  9. //cout << dat[nd] << ' ' << h << ' ' << w << endl;
  10. if ((h >= a && w >= b) ||(h >= b && w >= a)) return 0;
  11. if (nd == 0) return 2333333;
  12. long long th = 1, tw = 1;
  13. int ans = 2333333;
  14. for (int i = 0; i <= tms[dat[nd]]; i++) {
  15. tw = 1;
  16. for (int j = 0; j+i <= tms[dat[nd]]; j++) {
  17. ans = min(ans, dfs(nd-1, h*th, w*tw)+i+j);
  18. if (w*tw >= 100000) break;
  19. tw *= dat[nd];
  20. }
  21. if (h*th >= 100000) break;
  22. th *= dat[nd];
  23. }
  24. return ans;
  25. }
  26. int main()
  27. {
  28. scanf("%I64d%I64d%I64d%I64d%I64d", &a, &b, &h, &w, &n);
  29. for (int i = 1; i <= n; i++) scanf("%I64d", &ai[i]);
  30. sort(ai+1, ai+n+1);
  31. for (int i = n; i >= max(1ll, n-40); i--) {
  32. tms[ai[i]]++;
  33. if (dat[top] != ai[i])
  34. dat[++top] = ai[i];
  35. // cout << i << endl;
  36. }
  37. int ans = dfs(top, h, w);
  38. if (ans < 2333333)
  39. cout << dfs(top, h, w) << endl;
  40. else puts("-1");
  41. return 0;
  42. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注