传送门

A. Prime Subtraction

判断一下是否相差为\(1\)即可。

B. Kill 'Em All

随便搞搞。

C. Standard Free2play

题意:

现在有一个高度为\(h\)的悬崖,每一层有平台,但可能是隐藏状态。

高度为\(h\)的那层平台一定是在外面的,假设当前高度为\(x\),那么每次可以改变\(x\)和\(x-1\)层平台的状态。

规定一个人若从\(x\)掉到\(x-1\)或者\(x-2\)都没事,否则就出事了。

问最少改变多少平台的状态,能够使在\(h\)高度的人顺利到达地面(高度为\(0\))。

思路:

我大概模拟了一下这个过程,然后发现对于连续的\(x,x-1,\cdots,x-k\),最终答案是否加一与\(x\)和\(x-k\)的奇偶性相关。

并且从\(h_i\)到\(h_{i+1}\),假设中间有空平台,其实最后到达的是\(h_{i+1}-1\),然后就这样模拟一下这个过程就行了。

注意一下细节:最后到地面的时候记得判断一下高度是否大于\(2\),否则答案会加一。

代码如下:

Code
  1. #include <bits/stdc++.h>
  2. #define MP make_pair
  3. #define fi first
  4. #define se second
  5. #define sz(x) (int)(x).size()
  6. #define all(x) (x).begin(), (x).end()
  7. // #define Local
  8. using namespace std;
  9. typedef long long ll;
  10. typedef pair<int, int> pii;
  11. const int N = 2e5 + 5;
  12. int q;
  13. int h, n;
  14. int a[N];
  15. void run() {
  16. cin >> h >> n;
  17. a[n + 1] = 0;
  18. for(int i = 1; i <= n; i++) cin >> a[i];
  19. int ans = 0;
  20. for(int i = 1, j; i <= n; i = j + 1) {
  21. j = i;
  22. if(i != 1) {
  23. ++i;
  24. if(a[i] != a[i - 1] - 1) {
  25. ++ans;
  26. continue;
  27. }
  28. }
  29. if(i > n) break;
  30. j = i;
  31. while(j + 1 <= n && a[j] - a[j + 1] == 1) ++j;
  32. if((a[i] & 1) != (a[j] & 1) && a[j] != 1) ++ans;
  33. }
  34. cout << ans << '\n';
  35. }
  36. int main() {
  37. ios::sync_with_stdio(false);
  38. cin.tie(0); cout.tie(0);
  39. cout << fixed << setprecision(20);
  40. #ifdef Local
  41. freopen("../input.in", "r", stdin);
  42. freopen("../output.out", "w", stdout);
  43. #endif
  44. cin >> q;
  45. while(q--) run();
  46. return 0;
  47. }

D. AB-string

题意:

给出一个只含\(A,B\)的字符串,现在定义一个好的串是指:对于串中的每个数,都包含在一个长度大于\(1\)的回文串内。

问给出的字符串中,有多少好的串。

思路:

  • 容易发现,对于一个长度大于\(2\)串,其中间的数一定被包含在某个回文中;
  • 所以我们直接考虑两边的数。
  • 进一步发现,只有这样的串不满足条件:\(A\)或者\(B\)只出现一次,并且出现在两端某个位置。
  • 所以最终不合法的情况一定是“一段一段”的,可以直接压缩连续的段,统计个数然后直接算就行。

我做法是正反扫两边来搞的,稍微复杂一点。

Code
  1. #include <bits/stdc++.h>
  2. #define MP make_pair
  3. #define fi first
  4. #define se second
  5. #define sz(x) (int)(x).size()
  6. #define all(x) (x).begin(), (x).end()
  7. // #define Local
  8. using namespace std;
  9. typedef long long ll;
  10. typedef pair<int, int> pii;
  11. const int N = 3e5 + 5;
  12. int n;
  13. char s[N];
  14. void run() {
  15. cin >> (s + 1);
  16. ll ans = 0;
  17. for(int i = 1, j; i < n; i = j) {
  18. j = i;
  19. while(j <= n && s[j] == s[i]) ++j;
  20. --j; i = j;
  21. while(j + 1 <= n && s[j + 1] != s[i]) ++j;
  22. ans += j - i;
  23. }
  24. for(int i = n, j; i > 1; i = j) {
  25. j = i;
  26. while(j >= 1 && s[j] == s[i]) --j;
  27. ++j; i = j;
  28. while(j - 1 >= 1 && s[j - 1] != s[i]) --j;
  29. if(i - j - 1 >= 0) ans += i - j - 1;
  30. }
  31. ans = 1ll * (n - 1) * n / 2 - ans;
  32. cout << ans << '\n';
  33. }
  34. int main() {
  35. ios::sync_with_stdio(false);
  36. cin.tie(0); cout.tie(0);
  37. cout << fixed << setprecision(20);
  38. #ifdef Local
  39. freopen("../input.in", "r", stdin);
  40. freopen("../output.out", "w", stdout);
  41. #endif
  42. while(cin >> n) run();
  43. return 0;
  44. }

E. Keyboard Purchase

题意:

给出一个最多由前\(m\)个字母组成的字符串。

现在串的贡献就为\(\sum_{i=2}^{n}|pos_{s_{i-1}}-pos_{s_i}|\),\(pos_c\)表示\(c\)这个字符在排列中的位置,这个排列是自己定的。

现在就要求所有排列中最小的贡献。

思路:

  • 一开始想的就是枚举第一个位置的数...枚举第二个位置的数...但复杂度是阶乘级别的,然后没做出来;然后将问题转化为二元组,似乎也不行...就卡住了。
  • 后来发现,直接可以二进制压缩,当二进制上面有\(x\)个\(1\)时,就相当于固定了前\(x\)个位置,位置固定后,绝对值就很好化简了。
  • 然后枚举新添加进来的数,把绝对值拆开单独计算它的贡献,比如它和前面某些数相邻,此时他的贡献就是正的,对于其它的,他的贡献就是负的。
  • 直接做复杂度是\(O(2^m*m^2)\)的,可以预处理一下\(g(state,i)\)表示\(state\)中与\(i\)相邻的有多少个,转移直接利用二进制最后一位来进行转移,显然统计出来的\(g(state,i)\)不重不漏,最终复杂度就为\(O(2^m*m)了\)。

关键在于状态的定义,除开一般的“存在性”的含义,还有隐性含义固定前面的位置,并且仔细思考,这样其实可以直接枚举到所有的情况,之前似乎还没碰过这种hhh,感觉很巧妙。

详见代码:

Code
  1. #include <bits/stdc++.h>
  2. #define MP make_pair
  3. #define fi first
  4. #define se second
  5. #define sz(x) (int)(x).size()
  6. #define all(x) (x).begin(), (x).end()
  7. // #define Local
  8. #define INF 0x3f3f3f3f
  9. using namespace std;
  10. typedef long long ll;
  11. typedef pair<int, int> pii;
  12. const int N = 2e6 + 5, M = 20;
  13. int n, m;
  14. char s[N];
  15. int cnt[M][M], num[N];
  16. int g[N][M], f[N];
  17. int lg2[N];
  18. int lowbit(int x) {return x & -x;}
  19. void run() {
  20. cin >> (s + 1);
  21. memset(cnt, 0, sizeof(cnt));
  22. memset(f, INF, sizeof(f));
  23. memset(num, 0, sizeof(num));
  24. int lim = 1 << m;
  25. for(int i = 2; i < 1 << m; i++) lg2[i] = lg2[i >> 1] + 1;
  26. for(int i = 1; i < n; i++) {
  27. ++cnt[s[i] - 'a'][s[i + 1] - 'a'];
  28. ++cnt[s[i + 1] - 'a'][s[i] - 'a'];
  29. }
  30. for(int i = 0; i < lim; i++) {
  31. for(int j = 0; j < m; j++) {
  32. if(i == 0) g[i][j] = 0;
  33. else g[i][j] = g[i ^ lowbit(i)][j] + cnt[j][lg2[lowbit(i)]];
  34. }
  35. }
  36. for(int i = 0; i < lim; i++) {
  37. for(int j = 0; j < m; j++) {
  38. if(i >> j & 1) ++num[i];
  39. }
  40. }
  41. f[0] = 0;
  42. for(int i = 0; i < lim; i++) {
  43. for(int j = 0; j < m; j++) {
  44. if(i >> j & 1) {
  45. int msk1 = i ^ (1 << j);
  46. int msk2 = (lim - 1) ^ i;
  47. f[i] = min(f[i], f[msk1] + num[i] * (g[msk1][j] - g[msk2][j]));
  48. }
  49. }
  50. }
  51. cout << f[lim - 1] << '\n';
  52. }
  53. int main() {
  54. ios::sync_with_stdio(false);
  55. cin.tie(0); cout.tie(0);
  56. cout << fixed << setprecision(20);
  57. #ifdef Local
  58. freopen("../input.in", "r", stdin);
  59. freopen("../output.out", "w", stdout);
  60. #endif
  61. while(cin >> n >> m) run();
  62. return 0;
  63. }

F. The Maximum Subtree

题意:

求树上最大毛毛虫。

思路:

直接\(dp\)就行,一开始初始化错了改了一小时...

感觉难点就是问题的转化?但感觉问题转化也不是很难...

Code
  1. #include <bits/stdc++.h>
  2. #define MP make_pair
  3. #define fi first
  4. #define se second
  5. #define sz(x) (int)(x).size()
  6. #define all(x) (x).begin(), (x).end()
  7. // #define Local
  8. using namespace std;
  9. typedef long long ll;
  10. typedef pair<int, int> pii;
  11. const int N = 3e5 + 5;
  12. int n, q;
  13. int ans;
  14. int g[N];
  15. struct Edge {
  16. int v, next;
  17. }e[N << 1];
  18. int head[N], tot;
  19. void adde(int u, int v) {
  20. e[tot].v = v; e[tot].next = head[u]; head[u] = tot++;
  21. }
  22. void dfs(int u, int fa) {
  23. int son = 0, mx = 0, mx2 = 0;
  24. for(int i = head[u]; i != -1; i = e[i].next) {
  25. int v = e[i].v;
  26. if(v == fa) continue;
  27. ++son;
  28. dfs(v, u);
  29. g[u] = max(g[u], g[v]);
  30. if(g[v] > mx) {
  31. mx2 = mx, mx = g[v];
  32. } else if(g[v] > mx2) mx2 = g[v];
  33. }
  34. if(son == 0) {
  35. g[u] = 1; return;
  36. }
  37. ans = max(ans, mx + mx2 + max(son - 1, 1) + (fa != 0));
  38. g[u] += son;
  39. }
  40. void run() {
  41. cin >> n;
  42. for(int i = 1; i <= n; i++) head[i] = -1, g[i] = 0;
  43. tot = 0;
  44. for(int i = 1; i < n; i++) {
  45. int u, v; cin >> u >> v;
  46. adde(u, v); adde(v, u);
  47. }
  48. ans = 0;
  49. dfs(1, 0);
  50. cout << ans << '\n';
  51. }
  52. int main() {
  53. ios::sync_with_stdio(false);
  54. cin.tie(0); cout.tie(0);
  55. cout << fixed << setprecision(20);
  56. #ifdef Local
  57. freopen("../input.in", "r", stdin);
  58. freopen("../output.out", "w", stdout);
  59. #endif
  60. cin >> q;
  61. while(q--) run();
  62. return 0;
  63. }

Educational Codeforces Round 74 (Rated for Div. 2)的更多相关文章

  1. Educational Codeforces Round 74 (Rated for Div. 2) D. AB-string

    链接: https://codeforces.com/contest/1238/problem/D 题意: The string t1t2-tk is good if each letter of t ...

  2. Educational Codeforces Round 74 (Rated for Div. 2) C. Standard Free2play

    链接: https://codeforces.com/contest/1238/problem/C 题意: You are playing a game where your character sh ...

  3. Educational Codeforces Round 74 (Rated for Div. 2) B. Kill 'Em All

    链接: https://codeforces.com/contest/1238/problem/B 题意: Ivan plays an old action game called Heretic. ...

  4. Educational Codeforces Round 74 (Rated for Div. 2) A. Prime Subtraction

    链接: https://codeforces.com/contest/1238/problem/A 题意: You are given two integers x and y (it is guar ...

  5. Educational Codeforces Round 74 (Rated for Div. 2)【A,B,C【贪心】,D【正难则反的思想】】

    A. Prime Subtractiontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inpu ...

  6. Educational Codeforces Round 74 (Rated for Div. 2)补题

    慢慢来. 题目册 题目 A B C D E F G 状态 √ √ √ √ × ∅ ∅ //√,×,∅ 想法 A. Prime Subtraction res tp A 题意:给定\(x,y(x> ...

  7. Educational Codeforces Round 74 (Rated for Div. 2)E(状压DP,降低一个m复杂度做法含有集合思维)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;char s[100005];int pos[ ...

  8. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

随机推荐

  1. JQuery之Ajax基础

    众所周知JQuery中的Ajax主要用于数据传输,其数据传输格式为JSON格式数据,比XML格式数据传输更快. ajax 是 Asynchronous JavaScript and XML的简写,aj ...

  2. Pluralsight 科技公司公布自己的avaScript 成为最受欢迎的开发技术

    根据 SDTimes 报道,Pluralsight 科技公司公布自己的 Technology Index,JavaScript 位居榜首. Pluralsight,是美国的一家面向软件开发者的在线教育 ...

  3. [转]Python十个高大上的语法

    Python 是一种代表简单思想的语言,其语法相对简单,很容易上手.不过,如果就此小视 Python 语法的精妙和深邃,那就大错特错了.本文精心筛选了最能展现 Python 语法之精妙的十个知识点,并 ...

  4. iOS多线程知识梳理

    iOS多线程知识梳理 线程进程基础概念 进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 线程 1个进程要想执行任务,必须得有线程(每1个 ...

  5. 45-管理 Machine

    用 docker-machine 创建 machine 的过程很简洁,非常适合多主机环境.除此之外,Docker Machine 也提供了一些子命令方便对 machine 进行管理.其中最常用的就是无 ...

  6. SqlServer性能优化,查看CPU、内存占用大的会话及SQL语句

    1,查看CPU占用量最高的会话及SQL语句   select spid,cmd,cpu,physical_io,memusage, (select top 1 [text] from ::fn_get ...

  7. n个数字相加

    求s=a+aa+aaa+aaaa+aa...a的值 其中a是一个数字,多少个数字相加由键盘输入控制 a = int(input("数字:")) count = int(input( ...

  8. 我想外包开发一个APP,需要多少钱,多少时间?

    在一个阳光明媚的下午,我正瘫坐在椅子上改bug.忽然有人给我发微信:“我想做个app,多长时间,多少钱?” 从我从业iOS开发到现在,这个问题被问过无数次,比那句:“你是程序员,那你会修电脑吗?”还要 ...

  9. [译]Vulkan教程(02)概况

    [译]Vulkan教程(02)概况 这是我翻译(https://vulkan-tutorial.com)上的Vulkan教程的第2篇. This chapter will start off with ...

  10. C# LINQ Join两个表连接,关联多个条件的写法

    1.sql语句: select * from Users u join Teachers t on u.UserID==t.TeacherID and u.Name=t.Name 2.linq写法: ...