A. Fraction

暴力遍历1-1000,取组成的真分数比值最大且分子分母gcd为1时更新答案

代码:

  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <bitset>
  6. #include <string>
  7. #include <stack>
  8. #include <cmath>
  9. #include <queue>
  10. #include <set>
  11. #include <map>
  12. using namespace std;
  13. #define INF 0x3f3f3f3f
  14. #define LC(x) (x<<1)
  15. #define RC(x) ((x<<1)+1)
  16. #define MID(x,y) ((x+y)>>1)
  17. #define fin(name) freopen(name,"r",stdin)
  18. #define fout(name) freopen(name,"w",stdout)
  19. #define CLR(arr,val) memset(arr,val,sizeof(arr))
  20. #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
  21. typedef pair<int, int> pii;
  22. typedef long long LL;
  23. const double PI = acos(-1.0);
  24.  
  25. int main(void)
  26. {
  27. int n;
  28. while (~scanf("%d", &n))
  29. {
  30. double mm = 0;
  31. int aa, bb;
  32. for (int a = 1; a <= n; ++a)
  33. {
  34. for (int b = a + 1; b <= n; ++b)
  35. {
  36. if (a + b == n && a < b && (a * 1.0 / b) > mm && __gcd(a, b) == 1)
  37. {
  38. aa = a; bb = b;
  39. mm = a * 1.0 / b;
  40. }
  41. }
  42. }
  43. printf("%d %d\n", aa, bb);
  44. }
  45. return 0;
  46. }

B. Maxim Buys an Apartment

最好的情况是尽量放中间,每一个房子都可以造成两个good位置,这样一共有n/3个位置,如果k<=n/3则答案就是k*3,否则把剩余的位置先放到对答案没影响的,再放到对答案影响为1的地方。

代码:

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <bitset>
  7. #include <string>
  8. #include <stack>
  9. #include <cmath>
  10. #include <queue>
  11. #include <set>
  12. #include <map>
  13. using namespace std;
  14. #define INF 0x3f3f3f3f
  15. #define LC(x) (x<<1)
  16. #define RC(x) ((x<<1)+1)
  17. #define MID(x,y) ((x+y)>>1)
  18. #define fin(name) freopen(name,"r",stdin)
  19. #define fout(name) freopen(name,"w",stdout)
  20. #define CLR(arr,val) memset(arr,val,sizeof(arr))
  21. #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
  22. typedef pair<int, int> pii;
  23. typedef long long LL;
  24. const double PI = acos(-1.0);
  25.  
  26. int main(void)
  27. {
  28. LL n, k;
  29. while (cin >> n >> k)
  30. {
  31. if (k == 0 || k == n)
  32. puts("0 0");
  33. else
  34. {
  35. if (n == 2)
  36. puts("1 1");
  37. else
  38. {
  39. if (k <= (n / 3))
  40. printf("1 %I64d\n", k * 2);
  41. else
  42. {
  43. LL need = n / 3;
  44. LL res = k - need;
  45. printf("1 %I64d\n", need * 2 - (res - (n - need * 3)));
  46. }
  47. }
  48. }
  49. }
  50. return 0;
  51. }

C. Planning

贪心枚举当前时间k,让1-k时间内花费最多的航班最优降落即可,弱比我想到的是线段树,而大牛都是set……

代码:

  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <bitset>
  6. #include <string>
  7. #include <stack>
  8. #include <cmath>
  9. #include <queue>
  10. #include <set>
  11. #include <map>
  12. using namespace std;
  13. #define INF 0x3f3f3f3f
  14. #define LC(x) (x<<1)
  15. #define RC(x) ((x<<1)+1)
  16. #define MID(x,y) ((x+y)>>1)
  17. #define fin(name) freopen(name,"r",stdin)
  18. #define fout(name) freopen(name,"w",stdout)
  19. #define CLR(arr,val) memset(arr,val,sizeof(arr))
  20. #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
  21. typedef pair<int, int> pii;
  22. typedef long long LL;
  23. const double PI = acos(-1.0);
  24. const int N = 6e5 + 7;
  25. struct seg
  26. {
  27. int l, mid, r;
  28. int v, id;
  29. } T[N << 2];
  30. int arr[N];
  31. int Time[N];
  32.  
  33. inline void pushup(int k)
  34. {
  35. if (T[LC(k)].v > T[RC(k)].v)
  36. {
  37. T[k].v = T[LC(k)].v;
  38. T[k].id = T[LC(k)].id;
  39. }
  40. else
  41. {
  42. T[k].v = T[RC(k)].v;
  43. T[k].id = T[RC(k)].id;
  44. }
  45. }
  46. void build(int k, int l, int r)
  47. {
  48. T[k].l = l;
  49. T[k].r = r;
  50. T[k].mid = MID(l, r);
  51. if (l == r)
  52. {
  53. T[k].v = arr[l];
  54. T[k].id = l;
  55. }
  56. else
  57. {
  58. build(LC(k), l, T[k].mid);
  59. build(RC(k), T[k].mid + 1, r);
  60. pushup(k);
  61. }
  62. }
  63. void update(int k, int x)
  64. {
  65. if (T[k].l == T[k].r)
  66. T[k].v = -INF;
  67. else
  68. {
  69. if (x <= T[k].mid)
  70. update(LC(k), x);
  71. else
  72. update(RC(k), x);
  73. pushup(k);
  74. }
  75. }
  76. int query(int k, int l, int r)
  77. {
  78. if (l <= T[k].l && T[k].r <= r)
  79. return T[k].id;
  80. else
  81. {
  82. if (r <= T[k].mid)
  83. return query(LC(k), l, r);
  84. else if (l > T[k].mid)
  85. return query(RC(k), l, r);
  86. else
  87. {
  88. int ll = query(LC(k), l, r);
  89. int rr = query(RC(k), l, r);
  90. if (arr[ll] > arr[rr])
  91. return ll;
  92. else
  93. return rr;
  94. }
  95. }
  96. }
  97. int main(void)
  98. {
  99. int n, k, i;
  100. while (~scanf("%d%d", &n, &k))
  101. {
  102. for (i = 1; i <= n; ++i)
  103. scanf("%d", &arr[i]);
  104. build(1, 1, n);
  105. LL ans = 0;
  106. for (i = k + 1; i <= k + n; ++i)
  107. {
  108. int pos = query(1, 1, min(i, n));
  109. ans = ans + (LL)(arr[pos]) * (LL)(i - pos);
  110. update(1, pos);
  111. arr[pos] = -INF;
  112. Time[pos] = i;
  113. }
  114. printf("%I64d\n", ans);
  115. for (i = 1; i <= n; ++i)
  116. printf("%d%c", Time[i], " \n"[i == n]);
  117. }
  118. return 0;
  119. }

D. Jury Meeting

前缀+后缀思想,主要用两个二维数组pre和suf和维护当前某地最少花费的cost数组,用pre[i][0]表示当前到第i天为止所有能到达0点的人最少花费,pre[i][1]表示有几个人能到达0点,因此枚举i,把航班天数小于等于i的数据都拿去更新cost,再结合cost更新pre,类似于前缀最小值一样处理一遍,然后返程suf数组做一个与pre相反顺序的后缀最小花费处理,然后枚举到达和离开的区间[l,l+k+1],检查一下区间合法性和区间内是否可以到齐,再更新答案即可

代码;

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <bitset>
  7. #include <string>
  8. #include <stack>
  9. #include <cmath>
  10. #include <queue>
  11. #include <set>
  12. #include <map>
  13. using namespace std;
  14. #define INF 0x3f3f3f3f
  15. #define LC(x) (x<<1)
  16. #define RC(x) ((x<<1)+1)
  17. #define MID(x,y) ((x+y)>>1)
  18. #define fin(name) freopen(name,"r",stdin)
  19. #define fout(name) freopen(name,"w",stdout)
  20. #define CLR(arr,val) memset(arr,val,sizeof(arr))
  21. #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
  22. typedef pair<int, int> pii;
  23. typedef long long LL;
  24. const double PI = acos(-1.0);
  25. const int N = 1000010;
  26. struct info
  27. {
  28. int d, s, t, c;
  29. };
  30. info A[N], B[N];
  31. LL pre[N][2], suf[N][2], cost[N];
  32.  
  33. void init()
  34. {
  35. CLR(pre, 0);
  36. CLR(suf, 0);
  37. }
  38. int main(void)
  39. {
  40. int n, m, k, i;
  41. while (~scanf("%d%d%d", &n, &m, &k))
  42. {
  43. init();
  44. int cnta = 0, cntb = 0;
  45. int maxday = 0;
  46.  
  47. for (i = 0; i < m; ++i)
  48. {
  49. int d, s, t, c;
  50. scanf("%d%d%d%d", &d, &s, &t, &c);
  51. if (t == 0)
  52. A[cnta++] = (info) {d, s, t, c};
  53. else
  54. B[cntb++] = (info) {d, s, t, c};
  55. maxday = max(maxday, d);
  56. }
  57. sort(A, A + cnta, [](info a, info b) {return a.d < b.d;});
  58. sort(B, B + cntb, [](info a, info b) {return a.d > b.d;});
  59.  
  60. LL ps = 0, pc = 0;
  61. int cur = 0;
  62. CLR(cost, 0);
  63. for (i = 1; i <= maxday; ++i)
  64. {
  65. while (A[cur].d <= i && cur < cnta)
  66. {
  67. if (!cost[A[cur].s])
  68. {
  69. cost[A[cur].s] = A[cur].c;
  70. ps += A[cur].c;
  71. ++pc;
  72. }
  73. else if (cost[A[cur].s] > A[cur].c)
  74. {
  75. ps -= cost[A[cur].s];
  76. ps += A[cur].c;
  77. cost[A[cur].s] = A[cur].c;
  78. }
  79. ++cur;
  80. }
  81. pre[i][0] = ps;
  82. pre[i][1] = pc;
  83. }
  84. cur = 0;
  85. ps = 0;
  86. pc = 0;
  87. CLR(cost, 0);
  88. for (i = maxday; i >= 1; --i)
  89. {
  90. while (B[cur].d >= i && cur < cntb)
  91. {
  92. if (!cost[B[cur].t])
  93. {
  94. cost[B[cur].t] = B[cur].c;
  95. ps += B[cur].c;
  96. ++pc;
  97. }
  98. else if (cost[B[cur].t] > B[cur].c)
  99. {
  100. ps -= cost[B[cur].t];
  101. ps += B[cur].c;
  102. cost[B[cur].t] = B[cur].c;
  103. }
  104. ++cur;
  105. }
  106. suf[i][0] = ps;
  107. suf[i][1] = pc;
  108. }
  109. LL ans = 0x3f3f3f3f3f3f3f3f;
  110. for (i = 1; i <= maxday; ++i)
  111. {
  112. if (i + k + 1 <= maxday && pre[i][1] >= n && suf[i + k + 1][1] >= n && pre[i][0] + suf[i + k + 1][0] < ans)
  113. ans = pre[i][0] + suf[i + k + 1][0];
  114. }
  115. printf("%I64d\n", ans == 0x3f3f3f3f3f3f3f3f ? -1 : ans);
  116. }
  117. return 0;
  118. }

Codeforces 433 Div.2(A、B、C、D)的更多相关文章

  1. CodeForces 122G Lucky Array(一脸懵逼的树状数组)

    Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal re ...

  2. CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)

    The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...

  3. Codeforces Round #258 (Div. 2)(A,B,C,D)

    题目链接 A. Game With Sticks time limit per test:1 secondmemory limit per test:256 megabytesinput:standa ...

  4. codeforces 792CDivide by Three(两种方法:模拟、动态规划

    传送门:https://codeforces.com/problemset/problem/792/C 题意:给你一个字符串,要求让你删除最少个数的元素,使得最终答案是没有前导0并且是3的倍数. 题解 ...

  5. CodeForces 710CMagic Odd Square(经典-奇数个奇数&偶数个偶数)

    题目链接:http://codeforces.com/problemset/problem/710/C 题目大意:输入一个奇数n,则生成n*n矩阵,要求矩阵的行.列还有斜着,所有元素之和为奇数. 解题 ...

  6. Codeforces Gym100502G:Outing(缩点+有依赖的树形背包)

    http://codeforces.com/gym/100502/attachments 题意:有n个点,容量为tol,接下来n个关系,表示选了第i个点,那么第xi个点就必须被选.问最多可以选多少个点 ...

  7. Codeforces 1368H - Breadboard Capacity(最小割+线段树维护矩阵乘法)

    Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 首先看到这种从某一种颜色 ...

  8. Codeforces 917D - Stranger Trees(矩阵树定理/推式子+组合意义)

    Codeforces 题目传送门 & 洛谷题目传送门 刚好看到 wjz 在做这题,心想这题之前好像省选前做过,当时觉得是道挺不错的题,为啥没写题解呢?于是就过来补了,由此可见我真是个大鸽子(( ...

  9. Codeforces 772D - Varying Kibibits(高维差分+二项式定理维护 k 次方和)

    Codeforces 题目传送门 & 洛谷题目传送门 首先很容易注意到一件事,那就是对于所有 \(f(S)\) 可能成为 \(x\) 的集合 \(S\),必定有 \(\forall y\in ...

随机推荐

  1. 基于PHP的微信公众平台开发(TOKEN验证,消息回复)

    微信公众平台开发 实现步骤: 第一步:填写服务器配置 登录微信公众平台官网后,在公众平台后台管理页面 - 开发者中心页,点击“修改配置”按钮,填写服务器地址(URL).Token和EncodingAE ...

  2. IDEA搭建SSM出现的一些错误

    下面是我这几天整合SpringMVC+Spring+MyBatis框架遇到的一些问题 ,在这里总结一下: 1:HTTP Status 500 - Request processing failed; ...

  3. css样式 body的font-size 为什么用625%

    浏览器的默认高度?一般为16px. 为什么用62.5%作为body的默认样式?16px62.5%=10px.* 那么为什么一般多是 16px  *625% = 100px; <响应式Web设计实 ...

  4. Redis ---------- 持久化(AOF)操作

    每小时做一次快照持久化 8:00 快照持久化 9:00 快照持久化 10:00  快照持久化  上帝想玩弄人类,突然停电,55万个key丢失了 11:00 快照持久化 解决方案: 8:00-9:00在 ...

  5. 小游戏banner广告流量量主指引

    小程序导航 https://wq.xmaht.top

  6. R-biomaRt使用-代码备份

    目标:使用R脚本从ensembl上下载transcript数据 简单粗暴,直接上代码.biomaRt的介绍晚一点更新. # this file helps extract information fr ...

  7. Small Talk Matters【闲谈很重要】

    Small Talk Matters We' ve all been there: in a lift, in line at the bank or on an airplane, 我们都有过这样的 ...

  8. 如何查询进程中占用CPU的线程

    top -c             命令查找进程PID top -Hp PID          找进程中的线程号 echo %x 线程号   将线程转换成16进制 jstack PID |grep ...

  9. RHCE考试

    RHCSA_PDF版传送门:https://files.cnblogs.com/files/zhangjianghua/RHCSA%E8%AF%95%E9%A2%98.pdf RHCE_PDF版传送门 ...

  10. NPM安装vue-cli,并创建vue+webpack项目模板

    1.安装npm npm 是node.js 的包管理工具, 安装流程地址:https://docs.npmjs.com/cli/install  估计会非常慢,我们可以使用淘宝NPM镜像下载安装:htt ...