传送门

A. Happy Birthday, Polycarp!

签到。

Code
  1. /*
  2. * Author: heyuhhh
  3. * Created Time: 2019/12/14 19:07:57
  4. */
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <cstring>
  8. #include <vector>
  9. #include <cmath>
  10. #include <set>
  11. #include <map>
  12. #include <queue>
  13. #include <iomanip>
  14. #define MP make_pair
  15. #define fi first
  16. #define se second
  17. #define sz(x) (int)(x).size()
  18. #define all(x) (x).begin(), (x).end()
  19. #define INF 0x3f3f3f3f
  20. #define Local
  21. #ifdef Local
  22. #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  23. void err() { std::cout << '\n'; }
  24. template<typename T, typename...Args>
  25. void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  26. #else
  27. #define dbg(...)
  28. #endif
  29. void pt() {std::cout << '\n'; }
  30. template<typename T, typename...Args>
  31. void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
  32. using namespace std;
  33. typedef long long ll;
  34. typedef pair<int, int> pii;
  35. //head
  36. const int N = 1e5 + 5;
  37. int n;
  38. void run(){
  39. cin >> n;
  40. int x = n;
  41. int tot = 0;
  42. while(x) {
  43. ++tot; x /= 10;
  44. }
  45. int ans = 0;
  46. for(int i = 1; i <= 9; i++) {
  47. ans += tot - 1;
  48. if(tot != 10) {
  49. int now = 0;
  50. for(int j = 1; j <= tot; j++) now = now * 10 + i;
  51. if(now <= n) ++ans;
  52. }
  53. }
  54. cout << ans << '\n';
  55. }
  56. int main() {
  57. ios::sync_with_stdio(false);
  58. cin.tie(0); cout.tie(0);
  59. cout << fixed << setprecision(20);
  60. int T; cin >> T;
  61. while(T--) run();
  62. return 0;
  63. }

B. Make Them Odd

用一个set从大到小模拟一下这个过程即可。

Code
  1. /*
  2. * Author: heyuhhh
  3. * Created Time: 2019/12/14 19:13:49
  4. */
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <cstring>
  8. #include <vector>
  9. #include <cmath>
  10. #include <set>
  11. #include <map>
  12. #include <queue>
  13. #include <iomanip>
  14. #define MP make_pair
  15. #define fi first
  16. #define se second
  17. #define sz(x) (int)(x).size()
  18. #define all(x) (x).begin(), (x).end()
  19. #define INF 0x3f3f3f3f
  20. #define Local
  21. #ifdef Local
  22. #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  23. void err() { std::cout << '\n'; }
  24. template<typename T, typename...Args>
  25. void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  26. #else
  27. #define dbg(...)
  28. #endif
  29. void pt() {std::cout << '\n'; }
  30. template<typename T, typename...Args>
  31. void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
  32. using namespace std;
  33. typedef long long ll;
  34. typedef pair<int, int> pii;
  35. //head
  36. const int N = 1e5 + 5;
  37. set <int> S;
  38. void run(){
  39. int n; cin >> n;
  40. for(int i = 1; i <= n; i++) {
  41. int x; cin >> x;
  42. if((x & 1) == 0) S.insert(x);
  43. }
  44. int ans = 0;
  45. while(!S.empty()) {
  46. auto it = S.end(); --it;
  47. int now = *it;
  48. S.erase(it);
  49. now >>= 1;
  50. if((now & 1) == 0) S.insert(now);
  51. ++ans;
  52. }
  53. cout << ans << '\n';
  54. }
  55. int main() {
  56. ios::sync_with_stdio(false);
  57. cin.tie(0); cout.tie(0);
  58. cout << fixed << setprecision(20);
  59. int T; cin >> T;
  60. while(T--) run();
  61. return 0;
  62. }

C. As Simple as One and Two

题意:

给出一个串\(s\),现在要删除最少的字符,使得串中不含\(one,two\)。

思路:

直接的想法就是找到\(one,two\)就删除,但这里可能有个问题:比如\(twoone、tttwo\)这种,显然有较优的删除方法。

对于有单词重复的情况,显然我们删除一个\(w\)和一个\(n\)最优(因为只可能在两边重复)。

对于两个单词连接起来的情况,我们删除一个\(o\)最优。

所以分情况讨论即可。

Code
  1. /*
  2. * Author: heyuhhh
  3. * Created Time: 2019/12/14 19:26:17
  4. */
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <cstring>
  8. #include <vector>
  9. #include <cmath>
  10. #include <set>
  11. #include <map>
  12. #include <queue>
  13. #include <iomanip>
  14. #define MP make_pair
  15. #define fi first
  16. #define se second
  17. #define sz(x) (int)(x).size()
  18. #define all(x) (x).begin(), (x).end()
  19. #define INF 0x3f3f3f3f
  20. #define Local
  21. #ifdef Local
  22. #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  23. void err() { std::cout << '\n'; }
  24. template<typename T, typename...Args>
  25. void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  26. #else
  27. #define dbg(...)
  28. #endif
  29. void pt() {std::cout << '\n'; }
  30. template<typename T, typename...Args>
  31. void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
  32. using namespace std;
  33. typedef long long ll;
  34. typedef pair<int, int> pii;
  35. //head
  36. const int N = 2e5 + 5;
  37. char s[N];
  38. int del[N];
  39. bool chk1(int p) {
  40. return s[p] == 'o' && s[p + 1] == 'n' && s[p + 2] == 'e';
  41. }
  42. bool chk2(int p) {
  43. return s[p] == 't' && s[p + 1] == 'w' && s[p + 2] == 'o';
  44. }
  45. void run(){
  46. cin >> (s + 1);
  47. int n = strlen(s + 1);
  48. for(int i = 1; i <= n; i++) del[i] = 0;
  49. for(int i = 1; i <= n - 2; i++) {
  50. if(chk2(i)) {
  51. if(i + 3 <= n && s[i + 3] == 'o') del[i + 1] = 1;
  52. else del[i + 2] = 1;
  53. }
  54. if(chk1(i)) {
  55. if(i - 1 >= 1 && s[i - 1] == 'o') del[i + 1] = 1;
  56. else del[i] = 1;
  57. }
  58. }
  59. int ans = 0;
  60. for(int i = 1; i <= n; i++) ans += del[i];
  61. cout << ans << '\n';
  62. for(int i = 1; i <= n; i++) if(del[i]) cout << i << ' ';
  63. cout << '\n';
  64. }
  65. int main() {
  66. ios::sync_with_stdio(false);
  67. cin.tie(0); cout.tie(0);
  68. cout << fixed << setprecision(20);
  69. int T; cin >> T;
  70. while(T--) run();
  71. return 0;
  72. }

D. Let's Play the Words?

题意:

给出若干个互不相同\(01\)串,问最少翻转多少次,满足:

  • 所有串互不相同
  • 存在一种方案,使得串能够拼接起来

这里的拼接不要求形成环,而是线性拼接起来,只要头尾相同即可拼接,并且我们可以任意安排顺序。

思路:

之后所说的\(xy\)串指的是头为\(x\),尾为\(y\)的串。

  • 首先注意到我们翻转\(00,11\)串没用,我们可以只关注\(01,10\)串。
  • 假设两者的数量分别为\(a,b\),不妨\(a<b\),在不考虑翻转的情况时,显然贪心进行拼接,即\(10-01-\cdots-10\)。
  • 显然,我们只需要对\(\lfloor\frac{b-a}{2}\rfloor\)个\(10\)串进行翻转即可。
  • 最后选择翻转的策略是能翻转就翻转,我们用一个\(map\)来判断是否能翻转即可。
  • 另一种\(a\geq b\)的情况类似。

简而言之,贪心+\(map\)。

Code
  1. /*
  2. * Author: heyuhhh
  3. * Created Time: 2019/12/14 19:48:43
  4. */
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <cstring>
  8. #include <vector>
  9. #include <cmath>
  10. #include <set>
  11. #include <map>
  12. #include <queue>
  13. #include <iomanip>
  14. #define MP make_pair
  15. #define fi first
  16. #define se second
  17. #define sz(x) (int)(x).size()
  18. #define all(x) (x).begin(), (x).end()
  19. #define INF 0x3f3f3f3f
  20. #define Local
  21. #ifdef Local
  22. #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  23. void err() { std::cout << '\n'; }
  24. template<typename T, typename...Args>
  25. void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  26. #else
  27. #define dbg(...)
  28. #endif
  29. void pt() {std::cout << '\n'; }
  30. template<typename T, typename...Args>
  31. void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
  32. using namespace std;
  33. typedef long long ll;
  34. typedef pair<int, int> pii;
  35. //head
  36. const int N = 2e5 + 5;
  37. int n;
  38. string s[N];
  39. map <string, int> mp;
  40. int cnt[2][2];
  41. bool chk[N];
  42. string rev(string &S) {
  43. reverse(S.begin(), S.end());
  44. return S;
  45. }
  46. void run(){
  47. cin >> n;
  48. mp.clear();
  49. for(int i = 1; i <= n; i++) chk[i] = 0;
  50. cnt[0][1] = cnt[1][0] = 0;
  51. for(int i = 1; i <= n; i++) {
  52. cin >> s[i];
  53. int len = s[i].length();
  54. if(len > 1) {
  55. if(s[i][0] != s[i][len - 1]) mp[s[i]] = i;
  56. if(s[i][0] == '0' && s[i][len - 1] == '1') ++cnt[0][1];
  57. else if(s[i][0] == '1' && s[i][len - 1] == '0')++cnt[1][0];
  58. }
  59. }
  60. if(cnt[0][1] == 0 && cnt[1][0] == 0) {
  61. int cnt1 = 0, cnt2 = 0;
  62. for(int i = 1; i <= n; i++) {
  63. int len = s[i].length();
  64. if(s[i][0] == '0' && s[i][len - 1] == '0') ++cnt1;
  65. else ++cnt2;
  66. }
  67. if(cnt1 && cnt2) cout << -1 << '\n';
  68. else cout << 0 << '\n' << '\n';
  69. return;
  70. }
  71. int d = abs(cnt[0][1] - cnt[1][0]);
  72. int need = d / 2, ans;
  73. if(cnt[0][1] > cnt[1][0]) {
  74. for(int i = 1; i <= n && need; i++) {
  75. int len = s[i].length();
  76. if(len > 1 && s[i][0] == '0' && s[i][len - 1] == '1') {
  77. rev(s[i]);
  78. if(mp.find(s[i]) == mp.end()) {
  79. --need; chk[i] = 1;
  80. }
  81. rev(s[i]);
  82. }
  83. }
  84. } else {
  85. for(int i = 1; i <= n && need; i++) {
  86. int len = s[i].length();
  87. if(len > 1 && s[i][0] == '1' && s[i][len - 1] == '0') {
  88. rev(s[i]);
  89. if(mp.find(s[i]) == mp.end()) {
  90. --need; chk[i] = 1;
  91. }
  92. rev(s[i]);
  93. }
  94. }
  95. }
  96. if(need == 0) ans = d / 2;
  97. else {
  98. cout << -1 << '\n';
  99. return;
  100. }
  101. cout << ans << '\n';
  102. for(int i = 1; i <= n; i++) if(chk[i]) cout << i << ' ';
  103. cout << '\n';
  104. }
  105. int main() {
  106. ios::sync_with_stdio(false);
  107. cin.tie(0); cout.tie(0);
  108. cout << fixed << setprecision(20);
  109. int T; cin >> T;
  110. while(T--) run();
  111. return 0;
  112. }

E. Two Fairs

题意:

给出\(n\)个点,\(m\)条边的无向连通图。

再给定两个点\(a,b\),询问\((x,y)\)的对数。其中\((x,y)\)为合法的一对需要满足\(x\)到\(y\)的路径必须经过\(a,b\)这两个点。

\((x,y),(y,x)\)为同一种情况。

思路:

一开始想的是将无向图缩点成一颗树,然后在树上搞。方法是可行的,但过于复杂。

原问题为两点路径必须经过\(a,b\),也就是说,从\(a\)点出发,必须经过\(b\)才能到达某个点;从\(b\)出发。必须经过\(a\)才能到达某个点。

那么我们跑两次\(dfs\),分别从\(a,b\)出发,找到从\(a,b\)出发不经过对方能到达的所有点,那么剩下的点就是必须经过才能到达的点。

将两者的数量相乘即为答案。

思路的本质是将“路径缩短”,原先跑\(n\)次的问题只需要跑两次。

代码如下:

Code
  1. /*
  2. * Author: heyuhhh
  3. * Created Time: 2019/12/14 20:12:26
  4. */
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <cstring>
  8. #include <vector>
  9. #include <cmath>
  10. #include <set>
  11. #include <map>
  12. #include <queue>
  13. #include <iomanip>
  14. #define MP make_pair
  15. #define fi first
  16. #define se second
  17. #define sz(x) (int)(x).size()
  18. #define all(x) (x).begin(), (x).end()
  19. #define INF 0x3f3f3f3f
  20. #define Local
  21. #ifdef Local
  22. #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  23. void err() { std::cout << '\n'; }
  24. template<typename T, typename...Args>
  25. void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  26. #else
  27. #define dbg(...)
  28. #endif
  29. void pt() {std::cout << '\n'; }
  30. template<typename T, typename...Args>
  31. void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
  32. using namespace std;
  33. typedef long long ll;
  34. typedef pair<int, int> pii;
  35. //head
  36. const int N = 2e5 + 5, M = 5e5 + 5;
  37. int n, m, a, b;
  38. struct Edge {
  39. int v, next;
  40. }e[M << 1];
  41. int head[N], tot;
  42. void adde(int u, int v) {
  43. e[tot].v = v; e[tot].next = head[u]; head[u] = tot++;
  44. }
  45. int cnt;
  46. int col[N];
  47. void init() {
  48. for(int i = 1; i <= n; i++) head[i] = -1; tot = 0;
  49. }
  50. void dfs(int u, int fa, int x) {
  51. col[u] = 1;
  52. if(u == x) return;
  53. for(int i = head[u]; i != -1; i = e[i].next) {
  54. int v = e[i].v;
  55. if(!col[v]) dfs(v, u, x);
  56. }
  57. }
  58. void run(){
  59. cin >> n >> m >> a >> b;
  60. init();
  61. for(int i = 1; i <= m; i++) {
  62. int u, v; cin >> u >> v;
  63. adde(u, v); adde(v, u);
  64. }
  65. dfs(a, 0, b);
  66. int cnt1 = 0, cnt2 = 0;
  67. for(int i = 1; i <= n; i++) {
  68. cnt1 += 1 - col[i];
  69. col[i] = 0;
  70. }
  71. dfs(b, 0, a);
  72. for(int i = 1; i <= n; i++) {
  73. cnt2 += 1 - col[i];
  74. col[i] = 0;
  75. }
  76. ll ans = 1ll * cnt1 * cnt2;
  77. cout << ans << '\n';
  78. }
  79. int main() {
  80. ios::sync_with_stdio(false);
  81. cin.tie(0); cout.tie(0);
  82. cout << fixed << setprecision(20);
  83. int T; cin >> T;
  84. while(T--) run();
  85. return 0;
  86. }

Codeforces Round #606 (Div. 2)的更多相关文章

  1. 【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

    比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B ...

  2. 20191214 Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

    概述 切了 ABCE,Room83 第一 还行吧 A - Happy Birthday, Polycarp! 题解 显然这样的数不会很多. 于是可以通过构造法,直接求出 \([1,10^9]\) 内所 ...

  3. Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

    链接 签到题,求出位数,然后9*(位数-1)+ 从位数相同的全一开始加看能加几次的个数 #include<bits/stdc++.h> using namespace std; int m ...

  4. Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4) 题解

    Happy Birthday, Polycarp! Make Them Odd As Simple as One and Two Let's Play the Words? Two Fairs Bea ...

  5. Codeforces Round #606 (Div. 1) Solution

    从这里开始 比赛目录 我菜爆了. Problem A As Simple as One and Two 我会 AC 自动机上 dp. one 和 two 删掉中间的字符,twone 删掉中间的 o. ...

  6. Codeforces Round #606 (Div. 2) E - Two Fairs(DFS,反向思维)

  7. Codeforces Round #606 (Div. 2) D - Let's Play the Words?(贪心+map)

  8. Codeforces Round #606 Div. 2 比赛总结

    比赛情况 bq. A题 Wrong Answer on test 2 , E题sb题没切.bqbqbq. 比赛总结 bq. 那就直接上题解吧!^-^ A 数位dp,分类讨论,注意细节. Talk is ...

  9. Codeforces Round #606 (Div. 2) - E. Two Fairs(割点+dfs)

    题意:给你一张无向连通图,对于求有多少对$(x,y)$满足互相到达必须经过$(a,b)$,其中$x\neq a,x\neq b,y\neq a,y\neq b$ 思路:显然$a,b$都必须为割点,所以 ...

随机推荐

  1. GO汇总

    1.基础 GO语言介绍以及开发环境配置 Go-包 Go-数据类型以及变量,常量 Go-获取变量数据类型 GO-数组与切片 GO-切片拷贝以及赋值 Go-函数 Go-闭包 GO-逻辑判断(if,else ...

  2. mysql的锁机制详解

    这段时间一直在学习mysql数据库.项目组一直用的是oracle,所以对mysql的了解也不深.本文主要是对mysql锁的总结. Mysql的锁主要分为3大类: 表级锁:存储引擎为Myisam.锁住整 ...

  3. How to: Use the Entity Framework Data Model Located in an External Assembly 如何:使用位于外部程序集中的EF数据模型

    If you have a non-XAF application, and want to develop an XAF application that utilizes the same dat ...

  4. Sqoop数据传递

    1.环境准备:打开Hadoop.Mysql jps cd /apps/hadoop/sbin ./start-all.sh sudo service mysql start mysql -u root ...

  5. nginx将http升级到https并且同时支持http和https两种请求、http自动转向https

    1.http升级到https 1.1.检查 Nginx 是否支持 SSL /usr/local/nginx/sbin/nginx -V configure arguments中是否有--with-ht ...

  6. 使用 getUserMedia API获取麦克风和相机等流媒体

    概览 mediaDevices 是 Navigator 对象的只读属性,一个单列对象,可以连接访问相机和麦克风,屏幕共享等媒体输入设备 方法 enumerateDevices 请求一个可用的媒体输入和 ...

  7. HTML+CSS基础知识点简要汇总(思维导图)

  8. JS 测试 Prototype

    JS 测试 Prototype 测试 JavaScript 框架库 - Prototype 引用 Prototype 如需测试 JavaScript 库,您需要在网页中引用它. 为了引用某个库,请使用 ...

  9. 设置自动获取IP和DNS

    问题阐述 设置ipv4的自动获取时遇到一个问题,ip和dns自动获取可以确认设置,但是全局时就是报错,回头去看ipv4的ip和dns也还是原来的样子 由于一直使用的都是自动获取,很少会有主动设置ip或 ...

  10. redis cluster集群动态伸缩--删除主从节点

    目标:从集群中剔除一组主从(5007,5008) 经过上一节增加5007,5008主从服务节点后,目前集群的情况是这样的: b3363a81c3c59d57143cd3323481259c044e66 ...