[比赛链接]

http://codeforces.com/contest/922

[题解]

Problem A. Cloning Toys

         [算法]

当y = 0 ,   不可以

当y = 1 , x不为0时 , 不可以

当 y - 1 <= x , (x - y + 1)为偶数时 , 可以

时间复杂度 : O(1)

[代码]

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
  5. template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
  6. template <typename T> inline void read(T &x)
  7. {
  8. T f = ; x = ;
  9. char c = getchar();
  10. for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
  11. for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
  12. x *= f;
  13. }
  14.  
  15. int main()
  16. {
  17.  
  18. int x , y;
  19. read(x); read(y);
  20. if (y == ) printf("No\n");
  21. else if (y == && x != ) printf("No\n");
  22. else if (y - <= x && (x - y + ) % == ) printf("Yes\n");
  23. else printf("No\n");
  24.  
  25. return ;
  26.  
  27. }

Problem B. Magic Forest

              [算法]

枚举三角形的两边 , 计算第三边 , 判断是否能构成三角形即可

时间复杂度 : O(N^2)

[代码]

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n;
  5.  
  6. template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
  7. template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
  8. template <typename T> inline void read(T &x)
  9. {
  10. T f = ; x = ;
  11. char c = getchar();
  12. for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
  13. for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
  14. x *= f;
  15. }
  16. inline bool ok(int x,int y,int z)
  17. {
  18. return x > && y > && z > && x <= n && y <= n && z <= n && x + y > z && x + z > y && y + z > x;
  19. }
  20.  
  21. int main()
  22. {
  23.  
  24. int answer = ;
  25. read(n);
  26. for (int i = ; i <= n; i++)
  27. {
  28. for (int j = ; j <= n; j++)
  29. {
  30. int k = i ^ j;
  31. if (ok(i,j,k)) answer++;
  32. }
  33. }
  34. answer /= ;
  35. printf("%d\n",answer);
  36.  
  37. return ;
  38.  
  39. }

Problem C. Cave Painting

                [算法]

一个数除以1余数只可能为0

一个数除以2余数只可能为0,1

....

一个数除以n余数只可能为0,1,2,...n - 1

因此 , 我们只需判断对于i <= k , n除以i余数是否余数(i - 1)

时间复杂度 : O(K)

[代码]

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4.  
  5. template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
  6. template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
  7. template <typename T> inline void read(T &x)
  8. {
  9. T f = ; x = ;
  10. char c = getchar();
  11. for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
  12. for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
  13. x *= f;
  14. }
  15.  
  16. int main()
  17. {
  18.  
  19. LL n , k;
  20. read(n); read(k);
  21. for (LL i = ; i <= k; i++)
  22. {
  23. if (n % i != i - )
  24. {
  25. printf("No\n");
  26. return ;
  27. }
  28. }
  29. printf("Yes\n");
  30.  
  31. return ;
  32.  
  33. }

Problem D. Robot Vacuum Cleaner

                  [算法]

显然 , 答案只与字符串中"s"和"h"的个数有关

若a.s * b.h > a.h * b.s , a比b优

调用std :: sort即可 , 时间复杂度 : O(NlogN)

[代码]

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const int MAXN = 1e5 + ;
  5.  
  6. struct info
  7. {
  8. int s , h;
  9. } a[MAXN];
  10.  
  11. int n;
  12.  
  13. inline bool cmp(info a,info b)
  14. {
  15. return 1ll * a.s * b.h > 1ll * b.s * a.h;
  16. }
  17.  
  18. int main()
  19. {
  20.  
  21. cin >> n;
  22. LL ans = ;
  23. for (int i = ; i <= n; i++)
  24. {
  25. string s;
  26. cin >> s;
  27. for (int j = ; j < (int)s.size(); j++)
  28. {
  29. if (s[j] == 'h')
  30. {
  31. a[i].h++;
  32. ans += a[i].s;
  33. } else a[i].s++;
  34. }
  35. }
  36. sort(a + ,a + n + ,cmp);
  37. int pre = ;
  38. for (int i = ; i <= n; i++)
  39. {
  40. ans += 1ll * pre * a[i].h;
  41. pre += a[i].s;
  42. }
  43. printf("%I64d\n",ans);
  44.  
  45. return ;
  46.  
  47. }

Problem E. Birds

                      [算法]

注意 .,考虑使用动态规划

用f[i][j]表示在前i只小鸟中选j只 , 最多还剩多少能量 , 转移比较显然 , 不再赘述

时间复杂度 : O(NV)

[代码]

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const LL MAXN = 1e3 + ;
  5. const LL MAXS = 1e4 + ;
  6. const LL inf = 1e18;
  7.  
  8. LL n , W , B , X;
  9. LL c[MAXN],cost[MAXN];
  10. LL dp[MAXN][MAXS];
  11.  
  12. template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
  13. template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
  14. template <typename T> inline void read(T &x)
  15. {
  16. T f = ; x = ;
  17. char c = getchar();
  18. for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
  19. for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
  20. x *= f;
  21. }
  22. int main()
  23. {
  24.  
  25. read(n); read(W); read(B); read(X);
  26. for (LL i = ; i <= n; i++) read(c[i]);
  27. for (LL i = ; i <= n; i++) read(cost[i]);
  28. for (LL i = ; i <= n; i++)
  29. {
  30. for (LL j = ; j < MAXS; j++)
  31. {
  32. dp[i][j] = -inf;
  33. }
  34. }
  35. LL cnt = c[];
  36. for (LL i = ; i <= c[]; i++)
  37. {
  38. if (W - 1LL * i * cost[] >= )
  39. dp[][i] = min(W - 1LL * i * cost[] + X,W + i * B);
  40. else break;
  41. }
  42. for (LL i = ; i <= n; i++)
  43. {
  44. cnt += c[i];
  45. for (LL j = ; j <= cnt; j++)
  46. {
  47. for (LL k = ; k <= min(j,c[i]); k++)
  48. {
  49. if (dp[i - ][j - k] == -inf) continue;
  50. if (dp[i - ][j - k] - 1LL * cost[i] * k >= )
  51. chkmax(dp[i][j],min(1LL * W + 1LL * j * B,dp[i - ][j - k] + 1LL * X - 1LL * cost[i] * k));
  52. }
  53. }
  54. }
  55. LL ans = ;
  56. for (LL i = ; i <= cnt; i++)
  57. {
  58. if (dp[n][i] >= )
  59. ans = i;
  60. }
  61. printf("%I64d\n",ans);
  62.  
  63. return ;
  64.  
  65. }

Problem F.  Divisbility

[算法]

用F(i)表示选取1-i ,  符合条件的二元组的个数

显然 , 当k > F(n)时 ,  答案为No

否则 , 若k < F(n / 2) , 则不断将n降至n / 2

最后 , 我们取出n  / 2 + 1至n , 然后贪心删除一些数 , 即可

时间复杂度 : O(NlogN)

[代码]

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 3e5 + ;
  4. typedef long long LL;
  5.  
  6. int n , k;
  7. int d[MAXN];
  8. vector< int > a[MAXN];
  9. bool flg[MAXN];
  10.  
  11. template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
  12. template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
  13. template <typename T> inline void read(T &x)
  14. {
  15. T f = ; x = ;
  16. char c = getchar();
  17. for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
  18. for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
  19. x *= f;
  20. }
  21. inline LL f(int n)
  22. {
  23. LL ret = ;
  24. for (int i = ; i <= n; i++) ret += 1LL * n / i - ;
  25. return ret;
  26. }
  27.  
  28. int main()
  29. {
  30.  
  31. read(n); read(k);
  32. if (k > f(n))
  33. {
  34. printf("No\n");
  35. return ;
  36. }
  37. while (f(n >> ) >= k) n >>= ;
  38. for (int i = ; i <= n; i++)
  39. {
  40. for (int j = * i; j <= n; j += i)
  41. {
  42. ++d[j];
  43. }
  44. }
  45. for (int i = n / + ; i <= n; i++) a[d[i]].push_back(i);
  46. memset(flg,true,sizeof(flg));
  47. int m = f(n) - k;
  48. for (int i = n; i >= ; i--)
  49. {
  50. for (unsigned j = ; j < (int)a[i].size(); j++)
  51. {
  52. if (m >= i)
  53. {
  54. m -= i;
  55. flg[a[i][j]] = false;
  56. }
  57. }
  58. }
  59. int ans = ;
  60. for (int i = ; i <= n; i++)
  61. if (flg[i]) ans++;
  62. printf("Yes\n%d\n",ans);
  63. for (int i = ; i <= n; i++)
  64. if (flg[i]) printf("%d ",i);
  65. printf("\n");
  66.  
  67. return ;
  68.  
  69. }

[Codeforces Round #461 (Div2)] 题解的更多相关文章

  1. CodeForces round 967 div2 题解(A~E)

    本来准备比完赛就写题解的, 但是一拖拖了一星期, 唉 最后一题没搞懂怎么做,恳请大神指教 欢迎大家在评论区提问. A Mind the Gap 稳定版题面 https://cn.vjudge.net/ ...

  2. Codeforces Round #407 div2 题解【ABCDE】

    Anastasia and pebbles 题意:你有两种框,每个框可以最多装k重量的物品,但是你每个框不能装不一样的物品.现在地面上有n个物品,问你最少多少次,可以把这n个物品全部装回去. 题解:其 ...

  3. Codeforces Round #467(Div2)题解

    凌晨起来打CF,0:05,也是我第一次codeforces 第一题: 我刚开始怀疑自己读错题了,怎么会辣么水. 判除了0的数字种类 #include <cstdio> ; ]; int m ...

  4. Codeforces Round#704 Div2 题解(A,B,C,D,E)

    FST ROUND !!1 A Three swimmers: 直接整除一下向上取整就好了: #include <bits/stdc++.h> using namespace std; t ...

  5. Codeforces Round#687 Div2 题解

    打这场的时候迷迷糊糊的,然后掉分了( A Prison Break: 题面很复杂,但是题意很简单,仅需求出从这个点到四个角的最大的曼哈顿距离即可 #include <bits/stdc++.h& ...

  6. CodeForces Round #516 Div2 题解

    A. Make a triangle! 暴力... 就是给你三个数,你每次可以选一个加1,问最少加多少次能构成三角形 #include <bits/stdc++.h> #define ll ...

  7. Codeforces Round #539 div2

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

  8. CF922 CodeForces Round #461(Div.2)

    CF922 CodeForces Round #461(Div.2) 这场比赛很晚呀 果断滚去睡了 现在来做一下 A CF922 A 翻译: 一开始有一个初始版本的玩具 每次有两种操作: 放一个初始版 ...

  9. Codeforces Round #543 Div1题解(并不全)

    Codeforces Round #543 Div1题解 Codeforces A. Diana and Liana 给定一个长度为\(m\)的序列,你可以从中删去不超过\(m-n*k\)个元素,剩下 ...

随机推荐

  1. CF651B-Beautiful Paintings

    B. Beautiful Paintings time limit per test 1 second memory limit per test 256 megabytes input standa ...

  2. squid重定向(python 代码)

    #!/usr/bin/python2.7 # coding: utf-8 import sys,os,stat import transport def redirect_url(line,concu ...

  3. [HNOI2012] 永无乡 题解

    题意: n个点,有加边操作,询问与某一点处于相同的联通块的点中权值第k大的点 思路: 对所有点建立一棵权值线段树,加边就配合并查集进行线段树合并 反思: 动态开点,权值线段树要用sum[g[x=fin ...

  4. [luoguP2886] [USACO07NOV]牛继电器Cow Relays(矩阵)

    传送门 矩阵快速幂,本质是floyd 把 * 改成 + 即可 注意初始化 因为只有100条边,所以可以离散化 #include <cstdio> #include <cstring& ...

  5. C++中,什么叫类,结构,联合?

    在C++中 class 和 struct 本质上一样 可以互用class的成员默认是private的,struct的成员默认是public的但一般习惯把成员变量隐藏的用class申明, 成员变量可以公 ...

  6. hdu 3371

    #include<stdio.h> #include<stdlib.h> #define N 501 struct node { int x,y,dis; }road[N*N] ...

  7. PHP建立和删除目录

    <?php/*linux中的文件权限filedir 用户 组 其它 rwx rwx rwx 读写执行 6 4 6 读写 读 读写 7 7 7 rw_ r__ rw_ r__ _w_ ___ r ...

  8. 最长链(codevs 1814)

    题目描述 Description 现给出一棵N个结点二叉树,问这棵二叉树中最长链的长度为多少,保证了1号结点为二叉树的根. 输入描述 Input Description 输入的第1行为包含了一个正整数 ...

  9. THUPC2017看题总结

    THUPC2017 看题总结 #2402. 「THUPC 2017」天天爱射击 / Shooting 果题. 求当前子弹能会使多少块木板损坏,发现因为木板会随着子弹数目的增加而更加容易损坏,故此询问具 ...

  10. openstack swift middleware开发

    首先MiddleWare核心代码,这段代码卸载swift的源代码目录下,~/swift/swift/common/middleware下新建deletionpreventing.py: import ...