暴力 A - Orchestra

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner cin = new Scanner (System.in);
  7. int r = cin.nextInt ();
  8. int c = cin.nextInt ();
  9. int n = cin.nextInt ();
  10. int k = cin.nextInt ();
  11. int[][] a = new int[12][12];
  12. for (int i=0; i<n; ++i) {
  13. int x = cin.nextInt ();
  14. int y = cin.nextInt ();
  15. a[x][y] = 1;
  16. }
  17. int ans = 0;
  18. for (int sx=1; sx<=r; ++sx) {
  19. for (int lx=0; sx+lx<=r; ++lx) {
  20. for (int sy=1; sy<=c; ++sy) {
  21. for (int ly=0; sy+ly<=c; ++ly) {
  22. int cnt = 0;
  23. for (int i=sx; i<=sx+lx; ++i) {
  24. for (int j=sy; j<=sy+ly; ++j) {
  25. if (a[i][j] == 1) cnt++;
  26. if (cnt >= k) break;
  27. }
  28. }
  29. if (cnt >= k) ans++;
  30. }
  31. }
  32. }
  33. }
  34. System.out.println (ans);
  35. }
  36. }

找规律 B - Island Puzzle

相对位置不变,0的位置任意,当做不存在.查看每个点到对应的点是否能一起移动到.

  1. #include <bits/stdc++.h>
  2.  
  3. const int N = 2e5 + 5;
  4. int a[N], b[N];
  5. int n;
  6.  
  7. int main() {
  8. scanf ("%d", &n);
  9. int tot1 = 0, tot2 = 0;
  10. for (int x, i=0; i<n; ++i) {
  11. scanf ("%d", &x);
  12. if (x != 0) a[tot1++] = x;
  13. }
  14. for (int x, i=0; i<n; ++i) {
  15. scanf ("%d", &x);
  16. if (x != 0) b[tot2++] = x;
  17. }
  18. int s = 0;
  19. for (; s<tot2; ++s) {
  20. if (b[s] == a[0]) break;
  21. }
  22. bool flag = true;
  23. for (int i=0; i<tot1; ++i) {
  24. if (b[(i+s)%(n-1)] != a[i]) {
  25. flag = false; break;
  26. }
  27. }
  28. if (flag) puts ("YES");
  29. else puts ("NO");
  30.  
  31. return 0;
  32. }

数位DP C - XOR Equation

题意:求多少对(a, b)满足a + b == s && a ^ b == x

分析:s和x拆分成二进制,dp[i][4][0/1]第一维是二进制长度,第二维是a和b二进制下第i位的方案(01,10,00,11),第三维是是否第i位会进位,递推一下能够在log(N)解决.

  1. #include <bits/stdc++.h>
  2.  
  3. typedef long long ll;
  4. int s[41], x[41];
  5. ll dp[41][4][2];
  6.  
  7. void updata(int id) {
  8. if (s[id] == x[id]) {
  9. if (s[id] == 0) { //0 0
  10. if (id == 0) {
  11. dp[id][2][0] = dp[id][3][1] = 1;
  12. }
  13. else {
  14. for (int j=0; j<4; ++j) {
  15. dp[id][2][0] += dp[id-1][j][0];
  16. dp[id][3][1] += dp[id-1][j][0];
  17. }
  18. }
  19. }
  20. else { //1 1
  21. if (id == 0) {
  22. dp[id][0][0] = dp[id][1][0] = 1;
  23. }
  24. else {
  25. for (int j=0; j<4; ++j) {
  26. dp[id][0][0] += dp[id-1][j][0];
  27. dp[id][1][0] += dp[id-1][j][0];
  28. }
  29. }
  30. }
  31. }
  32. else {
  33. if (s[id] == 0) { //0 1
  34. for (int j=0; j<4; ++j) {
  35. dp[id][0][1] += dp[id-1][j][1];
  36. dp[id][1][1] += dp[id-1][j][1];
  37. }
  38. }
  39. else { //1 0
  40. for (int j=0; j<4; ++j) {
  41. dp[id][2][0] += dp[id-1][j][1];
  42. dp[id][3][1] += dp[id-1][j][1];
  43. }
  44. }
  45. }
  46. }
  47.  
  48. int main() {
  49. ll S, X; std::cin >> S >> X;
  50. if (X == 0) {
  51. puts ("1"); return 0;
  52. }
  53. bool same = false;
  54. if (S == X) same = true;
  55. int n = 0, m = 0;
  56. while (S) {
  57. if (S & 1) s[n++] = 1;
  58. else s[n++] = 0;
  59. S >>= 1;
  60. }
  61. while (X) {
  62. if (X & 1) x[m++] = 1;
  63. else x[m++] = 0;
  64. X >>= 1;
  65. }
  66. int len = std::max (n, m);
  67. for (int i=0; i<len; ++i) {
  68. updata (i);
  69. }
  70. ll ans = 0;
  71. for (int i=0; i<4; ++i) {
  72. ans += dp[len-1][i][0];
  73. }
  74. if (same && ans >= 2) ans -= 2;
  75. std::cout << ans << '\n';
  76.  
  77. return 0;
  78. }

树状数组 D - Factory Repairs

单点更新以及区间求和,不过不知道该点是k天前还是后,树状数组开二维.

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5. static int[][] C;
  6. static int[] A;
  7. static int n;
  8. static void updata(int i, int j, int x) {
  9. while (i <= n) {
  10. C[i][j] += x;
  11. i += i & -i;
  12. }
  13. }
  14. static int query(int i, int j) {
  15. int ret = 0;
  16. while (i > 0) {
  17. ret += C[i][j];
  18. i -= i & -i;
  19. }
  20. return ret;
  21. }
  22. static int min(int a, int b) {
  23. if (a < b) return a;
  24. else return b;
  25. }
  26. public static void main(String[] args) {
  27. Scanner cin = new Scanner (new BufferedInputStream (System.in));
  28. n = cin.nextInt ();
  29. int k = cin.nextInt ();
  30. int a = cin.nextInt ();
  31. int b = cin.nextInt ();
  32. int q = cin.nextInt ();
  33. C = new int[n+5][2];
  34. A = new int[n+5];
  35. for (int i=0; i<q; ++i) {
  36. int op = cin.nextInt ();
  37. if (op == 1) {
  38. int d = cin.nextInt ();
  39. int e = cin.nextInt ();
  40. int tmp = A[d]; A[d] += e;
  41. updata (d, 0, min (b, A[d]) - min (b, tmp));
  42. updata (d, 1, min (a, A[d]) - min (a, tmp));
  43. }
  44. else {
  45. int p = cin.nextInt ();
  46. int ans = query (p - 1, 0) + query (n, 1) - query (p+k-1, 1);
  47. System.out.println (ans);
  48. }
  49. }
  50. }
  51. }

贪心+双端队列 E - Package Delivery

题意:m个加油站,每单位油有价格,汽车有油的容量,初始满油,问到终点最少消费多少.

分析: 假设起点也算是一个加油站,免费,且汽车初始空油,那么每一个加油站最多能给汽车充n单位油,那么到下一个加油站要选择之前加油站价格最少的加油,这里用双端队列维护最低价格的加油站.

  1. #include <bits/stdc++.h>
  2.  
  3. typedef long long ll;
  4. const int N = 2e5 + 5;
  5. int dque[N];
  6. std::pair<int, int> station[N];
  7. int d, n, m;
  8.  
  9. int main() {
  10. scanf ("%d%d%d", &d, &n, &m);
  11. for (int i=0; i<m; ++i) {
  12. scanf ("%d%d", &station[i].first, &station[i].second);
  13. }
  14. station[m++] = std::make_pair (0, 0);
  15. station[m++] = std::make_pair (d, 0);
  16. std::sort (station, station+m);
  17. for (int i=0; i<m-1; ++i) {
  18. if (station[i].first + n < station[i+1].first) {
  19. puts ("-1"); return 0;
  20. }
  21. }
  22. int qs = 0, qe = 0, now = 0; ll ans = 0;
  23. for (int i=0; i<m; ++i) {
  24. int x = station[i].first, p = station[i].second;
  25. while (qs < qe && station[dque[qs]].first + n < x) {
  26. ans += 1ll * (station[dque[qs]].first + n - now) * station[dque[qs]].second;
  27. now = station[dque[qs]].first + n;
  28. qs++;
  29. }
  30. ans += 1ll * (x - now) * station[dque[qs]].second;
  31. now = x;
  32. while (qs < qe && station[dque[qe-1]].second > p) {
  33. qe--;
  34. }
  35. dque[qe++] = i;
  36. }
  37. std::cout << ans << '\n';
  38.  
  39. return 0;
  40. }

树形DP F - Preorder Test
题意:选择一条k长的DFS先序遍历路径,使得其中的最小值最大
分析:二分枚举最小值,然后枚举每个点出发的大于最小值的路径长度.求路径长度用到树形DP
首先down[u]表示u的子树下最多能走的路径长度,然后第二次dp[u]表示以u为根节点下最多能走的路径长度,up是u以上完整的最大长度

  1. #include <bits/stdc++.h>
  2.  
  3. const int N = 2e5 + 5;
  4. struct DP {
  5. int sum, mx;
  6. DP operator + (const DP &rhs) const {
  7. return DP {sum + rhs.sum, std::max (mx, rhs.mx)};
  8. }
  9. };
  10. int a[N], total[N], down[N], dp[N];
  11. bool good[N];
  12. std::vector<int> edge[N];
  13. int n, k;
  14.  
  15. void DFS(int u, int fa) {
  16. total[u] = 1;
  17. int sum = 0, mx = 0;
  18. for (auto v: edge[u]) {
  19. if (v == fa) continue;
  20. DFS (v, u);
  21. total[u] += total[v];
  22. if (down[v] == total[v]) {
  23. sum += total[v];
  24. } else {
  25. mx = std::max (mx, down[v]);
  26. }
  27. }
  28. down[u] = sum + mx + 1;
  29. if (!good[u]) down[u] = 0;
  30. }
  31.  
  32. void DFS2(int u, int fa, int up) {    //learn from tourist
  33. std::vector<int> children;
  34. int sum = 0, mx = 0;
  35. for (auto v: edge[u]) {
  36. if (v == fa) continue;
  37. if (down[v] == total[v]) {
  38. sum += down[v];
  39. } else {
  40. mx = std::max (mx, down[v]);
  41. }
  42. children.push_back (v);
  43. }
  44. if (up == n - total[u]) {
  45. sum += up;
  46. } else {
  47. mx = std::max (mx, up);
  48. }
  49. dp[u] = sum + mx + 1;
  50. if (!good[u]) dp[u] = 0;
  51.  
  52. int sz = children.size ();
  53. if (sz == 0) return ;
  54. std::vector<DP> predown (sz + 1);
  55. predown[0] = {0, 0};
  56. for (int i=0; i<sz; ++i) {
  57. int v = children[i];
  58. predown[i+1] = predown[i];
  59. if (down[v] == total[v]) {
  60. predown[i+1].sum += down[v];
  61. } else {
  62. predown[i+1].mx = std::max (predown[i+1].mx, down[v]);
  63. }
  64. }
  65. std::vector<DP> sufdown (sz + 1);
  66. sufdown[sz] = {0, 0};
  67. for (int i=sz-1; i>=0; --i) {
  68. int v = children[i];
  69. sufdown[i] = sufdown[i+1];
  70. if (down[v] == total[v]) {
  71. sufdown[i].sum += down[v];
  72. } else {
  73. sufdown[i].mx = std::max (sufdown[i].mx, down[v]);
  74. }
  75. }
  76. for (int i=0; i<sz; ++i) {
  77. int v = children[i];
  78. DP now = predown[i] + sufdown[i+1];
  79. if (up == n - total[u]) {
  80. now.sum += up;
  81. } else {
  82. now.mx = std::max (now.mx, up);
  83. }
  84. int new_up = now.sum + now.mx + 1;
  85. if (!good[u]) new_up = 0;
  86. DFS2 (v, u, new_up);
  87. }
  88. }
  89.  
  90. bool check(int v) {
  91. for (int i=1; i<=n; ++i) good[i] = (a[i] >= v);
  92. DFS (1, 0);
  93. DFS2 (1, 0, 0);
  94. for (int i=1; i<=n; ++i) {
  95. if (dp[i] >= k) return true;
  96. }
  97. return false;
  98. }
  99.  
  100. int main() {
  101. scanf ("%d%d", &n, &k);
  102. for (int i=1; i<=n; ++i) {
  103. scanf ("%d", a+i);
  104. }
  105. for (int u, v, i=1; i<n; ++i) {
  106. scanf ("%d%d", &u, &v);
  107. edge[u].push_back (v);
  108. edge[v].push_back (u);
  109. }
  110. int low = 0, high = 1000005;
  111. while (low < high) {
  112. int mid = (low + high + 1) >> 1;
  113. if (check (mid)) {
  114. low = mid;
  115. } else {
  116. high = mid - 1;
  117. }
  118. }
  119. printf ("%d\n", low);
  120.  
  121. return 0;
  122. }

8VC Venture Cup 2016 - Final Round (Div. 2 Edition)的更多相关文章

  1. 8VC Venture Cup 2016 - Final Round (Div. 1 Edition) E - Preorder Test 树形dp

    E - Preorder Test 思路:想到二分答案了之后就不难啦, 对于每个答案用树形dp取check, 如果二分的值是val, dp[ i ]表示 i 这棵子树答案不低于val的可以访问的 最多 ...

  2. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A

    A. Orchestra time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) D. Factory Repairs 树状数组

    D. Factory Repairs 题目连接: http://www.codeforces.com/contest/635/problem/D Description A factory produ ...

  4. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) C. XOR Equation 数学

    C. XOR Equation 题目连接: http://www.codeforces.com/contest/635/problem/C Description Two positive integ ...

  5. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)B. sland Puzzle 水题

    B. sland Puzzle 题目连接: http://www.codeforces.com/contest/635/problem/B Description A remote island ch ...

  6. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A. Orchestra 水题

    A. Orchestra 题目连接: http://www.codeforces.com/contest/635/problem/A Description Paul is at the orches ...

  7. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) E - Nikita and stack 线段树好题

    http://codeforces.com/contest/760/problem/E 题目大意:现在对栈有m个操作,但是顺序是乱的,现在每输入一个操作要求你输出当前的栈顶, 注意,已有操作要按它们的 ...

  8. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) D - Travel Card

    D - Travel Card 思路:dp,类似于单调队列优化. 其实可以写的更简单... #include<bits/stdc++.h> #define LL long long #de ...

  9. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition)A 水 B 二分 C并查集

    A. Petr and a calendar time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

随机推荐

  1. pmap

    .[root@localhost security]# pmap -d : -bash Address Kbytes Mode Offset Device Mapping r-x-- : bash b ...

  2. 1.1-java创建包和类的方法

    1.new-package-命名方法com打头.中间名称.后台要创建的class 2.创建class-    new-class 选择一下主方法 代码示例  编译完保存一下就能输出信息,一直没有保存才 ...

  3. redis 列出所有的键

    > KEYS * (empty list or set)

  4. kvm 网桥

    root@ok Downloads]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 HWADDR=54:EE:75:4E:37: ...

  5. Validform 学习笔记---基础知识整理

    面对表单的验证,自己写大量的js毕竟不是一个明智的做法.不仅仅是代码很长而且不便于梳理.Validform就是一款开源的第三方验证js的控件,通过添加相应的js以及css能够有效的验证表单,维护起来也 ...

  6. android 入门-android Studio git 克隆

    最后是完成 以上是如何从android studio Git 克隆Github的项目

  7. Windows 历史

  8. ASP.NET 5探险(7):使用混合型控制器方便实现单页应用

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:由于在ASP.NET 5中,MVC和WEB API的技术栈合并了,所以开发混合型Con ...

  9. 提升 LaTeX 效率的小工具:Detexify LaTeX handwritten symbol recognition

    Detexify LaTeX handwritten symbol recognition 用 LaTeX 的人找符号的表示方法通常很费事,需要去翻长长的列表.Detexify 是一个省事的小网站,只 ...

  10. Linux文本比较-diff&awk

    最近为了完成工作,需要将两个文件A.old和A进行比较,然后将A中新增加的部分保存到A中,其他部分删除.经过查找相关资料,发现有两种比较好的方法. 1. 使用diff命令 diff old.file ...