A. The Child and Homework

注意仔细读题,WA了好多次,=_=

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. const int maxn = ;
  7.  
  8. char s[][maxn];
  9. int l[], r[];
  10.  
  11. bool cmp(int a, int b) { return l[a] < l[b]; }
  12.  
  13. int main()
  14. {
  15. //freopen("in.txt", "r", stdin);
  16.  
  17. for(int i = ; i < ; i++) r[i] = i;
  18. for(int i = ; i < ; i++) scanf("%s", s[i]);
  19. for(int i = ; i < ; i++) { l[i] = strlen(s[i]); l[i] -= ; }
  20. sort(r, r + , cmp);
  21.  
  22. int ans = -;
  23. if(l[r[]] * <= l[r[]]) ans = r[];
  24. if(l[r[]] >= l[r[]] * ) { if(ans == -) ans = r[]; else ans = ; }
  25. if(ans == -) ans = ;
  26.  
  27. printf("%c\n", 'A' + ans);
  28.  
  29. return ;
  30. }

代码君

B. The Child and Set (贪心)

把那些lowbit都算出来,然后从大到小排个序,依次往里面填就好了。

  1. #include <cstdio>
  2. #include <algorithm>
  3. using std::sort;
  4. const int maxn = + ;
  5. int lowbit[maxn], r[maxn], ans[maxn], cnt;
  6.  
  7. bool cmp(int a, int b) { return lowbit[a] > lowbit[b]; }
  8.  
  9. int main()
  10. {
  11. int s, n;
  12. scanf("%d%d", &s, &n);
  13.  
  14. for(int i = ; i <= n; i++) { r[i] = i; lowbit[i] = i & (-i); }
  15. sort(r + , r + + n, cmp);
  16. int t = ;
  17. for(int i = ; i <= n; i++)
  18. {
  19. if(t == s) break;
  20. if(lowbit[r[i]] + t <= s) { t += lowbit[r[i]]; ans[cnt++] = r[i]; }
  21. }
  22.  
  23. if(t != s) { puts("-1"); return ; }
  24. printf("%d\n%d", cnt, ans[]);
  25. for(int i = ; i < cnt; i++) printf(" %d", ans[i]);
  26. puts("");
  27.  
  28. return ;
  29. }

代码君

C. The Child and Toy (贪心)

最优的删点方式就是从最大的点开始删起。

不妨从边的角度考虑更为方便,最终没有任意两个点事连通的,所以每条边都会被删去。考虑边(u, v),它被删去的时候贡献的一定是uv中较小的权值。

所以算法就是,读进每条边累加它权值较小的那个端点的权值即可。

  1. #include <cstdio>
  2. #include <algorithm>
  3. using std::min;
  4.  
  5. const int maxn = + ;
  6. const int maxm = + ;
  7.  
  8. int a[maxn], u[maxm], v[maxm];
  9.  
  10. int main()
  11. {
  12. int n, m, ans = ;
  13. scanf("%d%d", &n, &m);
  14. for(int i = ; i <= n; i++) scanf("%d", &a[i]);
  15. for(int i = ; i < m; i++) { scanf("%d%d", &u[i], &v[i]); ans += min(a[u[i]], a[v[i]]); }
  16. printf("%d\n", ans);
  17.  
  18. return ;
  19. }

代码君

D. The Child and Zoo (贪心 含秩并查集 最大生成树)

  给每条边赋一个权值,为两端顶点权值较小的那个,然后将这些边从大到小排序。

  一开始图是空的,每加进一条权值为w的边可能会将两个连通分量连通起来,那么对于分别位于这两个连通分量中的(u, v),f(u, v) = w,而这样的f(u, v)一共有 p * q个,其中pq分别为这两个连通分量的秩。

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. const int maxn = + ;
  6. int a[maxn], u[maxn], v[maxn], w[maxn], r[maxn], cnt[maxn];
  7.  
  8. int pa[maxn];
  9. int findset(int x) { return x == pa[x] ? x : pa[x] = findset(pa[x]); }
  10.  
  11. bool cmp(int a, int b) { return w[a] > w[b]; }
  12.  
  13. int main()
  14. {
  15. //freopen("in.txt", "r", stdin);
  16.  
  17. int n, m;
  18. scanf("%d%d", &n, &m);
  19. for(int i = ; i <= n; i++) scanf("%d", &a[i]);
  20. for(int i = ; i <= m; i++) { scanf("%d%d", &u[i], &v[i]); w[i] = min(a[u[i]], a[v[i]]); }
  21.  
  22. for(int i = ; i <= n; i++) { pa[i] = i; cnt[i] = ; }
  23. for(int i = ; i <= m; i++) r[i] = i;
  24. sort(r + , r + + m, cmp);
  25.  
  26. double ans = 0.0;
  27. int c = n;
  28. for(int i = ; i <= m && c > ; i++)
  29. {
  30. int px = findset(u[r[i]]);
  31. int py = findset(v[r[i]]);
  32. if(px != py)
  33. {
  34. ans += (double) cnt[px] * cnt[py] * w[r[i]];
  35. pa[px] = py; cnt[py] += cnt[px];
  36. c--;
  37. }
  38. }
  39.  
  40. printf("%.6f\n", ans * / (n * 1.0 * (n - )));
  41.  
  42. return ;
  43. }

代码君

E. The Child and Polygon (区间DP 叉积)

不妨将这些点按照逆时针顺序编号0 ~ n-1。d(i, j)表示多边形 i, i+1 ,..., j, i 的三角剖分的方法数。

则有状态转移方程 d(i, j) = sum{ d(i, k) * d(k, j) | i < k < j 且 线段ik在多边形内部 }

可以通过叉积来判断线段是否在多边形内部,具体就是判断 ji × jk 是否为正。

总时间复杂度为O(n3)

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. typedef long long LL;
  7. const LL MOD = ;
  8.  
  9. const int maxn = + ;
  10. int n;
  11.  
  12. struct Point
  13. {
  14. LL x, y;
  15. Point(LL x = , LL y = ):x(x), y(y) {}
  16. }p[maxn];
  17.  
  18. Point operator - (const Point& A, const Point& B)
  19. { return Point(A.x - B.x, A.y - B.y); }
  20.  
  21. LL Cross(const Point& A, const Point& B)
  22. { return A.x * B.y - A.y * B.x; }
  23.  
  24. LL d[maxn][maxn];
  25.  
  26. LL dp(int L, int R)
  27. {
  28. if(d[L][R] != -) return d[L][R];
  29. LL& ans = d[L][R];
  30. if(R - L == ) return ans = ;
  31. ans = ;
  32. for(int i = L + ; i < R; i++)
  33. if(Cross(p[L] - p[R], p[i] - p[R]) > )
  34. ans = (ans + dp(L, i) * dp(i, R)) % MOD;
  35. return ans;
  36. }
  37.  
  38. int main()
  39. {
  40. //freopen("in.txt", "r", stdin);
  41.  
  42. scanf("%d", &n);
  43. LL x, y;
  44. for(int i = ; i < n; i++)
  45. {
  46. scanf("%I64d%I64d", &x, &y);
  47. p[i] = Point(x, y);
  48. }
  49.  
  50. LL area = ;
  51. for(int i = ; i + < n; i++)
  52. area += Cross(p[i] - p[], p[i+] - p[]);
  53. if(area < ) reverse(p, p + n);
  54.  
  55. memset(d, -, sizeof(d));
  56. printf("%I64d\n", dp(, n - ));
  57.  
  58. return ;
  59. }

代码君

最后看了下Div1的两道题:

D. The Child and Sequence (线段树 单点修改)

动态询问连续子序列的和。操作有 单点修改 和 将 连续区间的每个数都模上x

重点说一下取模的操作,可以维护一个区间的最大值,当区间的最大值小于x的时候,那么便不用取模了。

而且每个数每次取模以后的值至少要减半,所以每个数取模的次数不会太多。

balabala...

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6. typedef long long LL;
  7.  
  8. const int maxn = + ;
  9. int n, m, qL, qR, p, op;
  10. LL v;
  11. LL sumv[maxn << ], maxv[maxn << ];
  12.  
  13. void pushup(int o)
  14. {
  15. sumv[o] = sumv[o<<] + sumv[o<<|];
  16. maxv[o] = max(maxv[o<<], maxv[o<<|]);
  17. }
  18.  
  19. void build(int o, int L, int R)
  20. {
  21. if(L == R) { scanf("%I64d", &sumv[o]); maxv[o] = sumv[o]; return; }
  22. int M = (L + R) / ;
  23. build(o<<, L, M);
  24. build(o<<|, M+, R);
  25. pushup(o);
  26. }
  27.  
  28. void update1(int o, int L, int R)
  29. {
  30. if(L == R) { sumv[o] = maxv[o] = v; return; }
  31. int M = (L + R) / ;
  32. if(p <= M) update1(o<<, L, M);
  33. else update1(o<<|, M+, R);
  34. pushup(o);
  35. }
  36.  
  37. void update2(int o, int L, int R)
  38. {
  39. if(maxv[o] < v) return;
  40. if(L == R) { sumv[o] %= v; maxv[o] %= v; return; }
  41. int M = (L + R) / ;
  42. if(qL <= M) update2(o<<, L, M);
  43. if(qR > M) update2(o<<|, M+, R);
  44. pushup(o);
  45. }
  46.  
  47. LL query(int o, int L, int R)
  48. {
  49. if(qL <= L && qR >= R) return sumv[o];
  50. int M = (L + R) / ;
  51. LL ans = ;
  52. if(qL <= M) ans += query(o<<, L, M);
  53. if(qR > M) ans += query(o<<|, M+, R);
  54. return ans;
  55. }
  56.  
  57. int main()
  58. {
  59. //freopen("in.txt", "r", stdin);
  60.  
  61. cin >> n >> m;
  62. build(, , n);
  63. while( m-- )
  64. {
  65. scanf("%d", &op);
  66. if(op == )
  67. {
  68. scanf("%d%d", &qL, &qR);
  69. printf("%I64d\n", query(, , n));
  70. }
  71. else if(op == )
  72. {
  73. scanf("%d%d%I64d", &qL, &qR, &v);
  74. update2(, , n);
  75. }
  76. else
  77. {
  78. scanf("%d%I64d", &p, &v);
  79. update1(, , n);
  80. }
  81. }
  82.  
  83. return ;
  84. }

代码君

E题看到用什么母函数,多项式求根,FFT,就打算把这道题放一放了,毕竟太弱Q_Q

CodeForces Round #250 Div2的更多相关文章

  1. Codeforces Round #539 div2

    Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...

  2. BZOJ 3625: [Codeforces Round #250]小朋友和二叉树

    3625: [Codeforces Round #250]小朋友和二叉树 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 304  Solved: 13 ...

  3. 【前行】◇第3站◇ Codeforces Round #512 Div2

    [第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...

  4. Codeforces Round#320 Div2 解题报告

    Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...

  5. Codeforces Round #564(div2)

    Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...

  6. Codeforces Round #361 div2

    ProblemA(Codeforces Round 689A): 题意: 给一个手势, 问这个手势是否是唯一. 思路: 暴力, 模拟将这个手势上下左右移动一次看是否还在键盘上即可. 代码: #incl ...

  7. Codeforces Round #626 Div2 D,E

    比赛链接: Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics) D.Present 题意: 给定大 ...

  8. CodeForces Round 192 Div2

    This is the first time I took part in Codeforces Competition.The only felt is that my IQ was contemp ...

  9. Codeforces Round#250 D. The Child and Zoo(并差集)

    题目链接:http://codeforces.com/problemset/problem/437/D 思路:并差集应用,先对所有的边从大到小排序,然后枚举边的时候,如果某条边的两个顶点不在同一个集合 ...

随机推荐

  1. jQuery scroll事件

    scroll事件适用于window对象,但也可滚动iframe框架与CSS overflow属性设置为scroll的元素. $(document).ready(function () { //本人习惯 ...

  2. UITableView多选删除

    设置一个在编辑状态下点击可改变图片的cell FileItemTableCell.h #import <UIKit/UIKit.h> @interface FileItemTableCel ...

  3. ***Jquery下Ajax与PHP数据交换

    一.前台传递字符串变量,后台返回字符串变量(非json格式) Javascript代码: 这里,为了解决Ajax数据传递出现的汉字乱码,在字符串传递之前,使用javascript函数escape()对 ...

  4. poj 3317 Stake Your Claim 极大极小搜索

    思路:为了方便,当c1>c2时将0变为1,1变为0. 空格最多有10个,每个空格有3个状态,如果不状态压缩,会TLE的.所以最多有3^10种情况 代码如下: #include<iostre ...

  5. hdu 4726 Kia's Calculation

    思路:刚开始想复杂了. 看解题报告后才知道这题挺简单的,看来还是要多训练啊!!! 单独处理首位的数字,不能为0.其他的就好处理了,从大到小依次找下去就可以了…… 代码如下: #include<i ...

  6. maven本地仓库.m2文件夹路径讲解

    Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(Depen ...

  7. lintcode:交换链表当中两个节点

    题目 给你一个链表以及两个权值v1和v2,交换链表中权值为v1和v2的这两个节点.保证链表中节点权值各不相同,如果没有找到对应节点,那么什么也不用做. 注意事项 你需要交换两个节点而不是改变节点的权值 ...

  8. 读取本地excel发短信

    package com.cmcc.zysoft.sellmanager.controller; import java.io.File; import java.io.FileInputStream; ...

  9. ps小技巧

    一.加色与减色 电脑显示器和电视是加色法最常见的形式,而在油漆.颜料和彩色滤光片会用减色. 二.怎么把背景变成透明:其实就是抠图. 1.魔术棒+delete,缺点:应用于边界明显的图片,否则容差不好控 ...

  10. CentOS7区域设置

    区域设置的配置文件在/etc/locale.conf,通过localectl命令进行设置: systemd服务在启动的时候读取区域配置文件,完成系统的设置. 命令的几个常用方法如下: 1 查看当前配置 ...