CodeForces1754

注:所有代码均为场上所书

Technical Support

解析:

题目大意

给定一个只包含大写字母 \(\texttt{Q}\) 和 \(\texttt{A}\) 的字符串,如果字符串里的每一个 \(\texttt{Q}\) 都能与在其之后的 \(\texttt{A}\) 一一对应地匹配,则输出字符串 \(\texttt{Yes}\),否则输出字符串 \(\texttt{No}\)。注意,可以有 \(\texttt{A}\) 没有被匹配,但每个 \(\texttt{Q}\) 必须成功地匹配。


思路:

变相的考了括号序列,如果令 Q 为 1,A 为 -1,那么原序列最终的和必须 \(\leq 0\)。


code

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef pair <int, int> pii;
  4. inline int read ()
  5. {
  6. int x = 0, f = 1;
  7. char ch = getchar ();
  8. while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar (); }
  9. while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar (); }
  10. return x * f;
  11. }
  12. int n, sum;
  13. bool solve ()
  14. {
  15. n = read (); sum = 0;
  16. for (int i = 1; i <= n; i++)
  17. {
  18. char ch = getchar ();
  19. if (ch == 'Q') sum++;
  20. else sum--;
  21. sum = max (0, sum); // 不可能回答了负数个问题
  22. }
  23. if (sum <= 0) return true;
  24. else return false;
  25. }
  26. signed main()
  27. {
  28. int t = read ();
  29. while (t--) puts(solve () ? "Yes" : "No");
  30. return 0;
  31. }

Kevin and Permutation

解析:

题目大意:

求一个 \(1\sim n\) 的排列 \(p\),使得 \(\min\limits_{i=1}^{n-1}\lvert p_{i+1}-p_i\rvert\) 最大。


思路:

首先最大值一定是 \(\lfloor\frac{n}{2}\rfloor\),考虑构造:

\(\lfloor\frac{n}{2}\rfloor,2\times\lfloor\frac{n}{2}\rfloor,3\times\lfloor\frac{n}{2}\rfloor,\cdots\lfloor\frac{n}{2}\rfloor-1,2\times \lfloor\frac{n}{2}\rfloor-1,\cdots,2,\lfloor\frac{n}{2}\rfloor+2,\cdots,1,\lfloor\frac{n}{2}\rfloor+1\)。

不懂看代码。


code:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef pair <int, int> pii;
  4. inline int read ()
  5. {
  6. int x = 0, f = 1;
  7. char ch = getchar ();
  8. while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar (); }
  9. while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar (); }
  10. return x * f;
  11. }
  12. int n;
  13. void solve ()
  14. {
  15. n = read (); int x = n / 2;
  16. for (int j = x; j >= 1; j--)
  17. for (int i = j; i <= n; i += x) printf ("%d ", i);
  18. puts ("");
  19. }
  20. signed main()
  21. {
  22. int t = read ();
  23. while (t--) solve ();
  24. return 0;
  25. }

Make Nonzero Sum (easy version)

解析:

题目大意

给你一个数组 \([a_1,a_2,...a_n]\) ,其中每一项 \(a_i\) 都为 \(1\) 或 \(-1\) ,你需要构造一个划分 \([l_1,r_1],[l_2,r_2],[l_3,r_3],...[l_k,r_k]\) 使得:

  • 将每一个区间内的数按照以下方法计算出\(s_i=a_{l_i}-a_{l_i+1}+a_{l_i+2}-a_{l_i+3}+...\pm a_{r_i}\)

  • 对于一个合法的划分,所有的 \(s_i\) 之和为 \(0\)

如果存在这样的划分,输出任何一个,否则输出 -1 ,代表无解。

称一组区间 \([l_1,r_1],[l_2,r_2],[l_3,r_3],...[l_k,r_k]\) 为数组 \([a_1,a_2,...a_n]\) 的划分当且仅当 \(1=l_1\leq r_1,l_2\leq r_2,l_3\leq r_3,...,,l_k\leq r_k = n\) 且对于 \(1\leq i \leq k-1\) ,均有 \(r_i+1=l_{i+1}\)

注意在本题中,你不需要最小化 \(k\)。


思路:

首先发现长度大于 \(2\) 的区间是没有意义的,因为你可以拆成若干个长度为 \(1,2\) 区间的并。

考虑现在有 \(n\) 个区间 \([1,1],[2,2],[3,3],\cdots[n,n]\),现在要合并一些区间,使最终代价为 0。我们对数组求和,如果序列的和为奇数,显然无解,因为你最多只能拼出来 \(1/-1\)。

考虑和大于 0 的情况,那么和小于 0 只需要把原数组取反即可。

我们设 \(b_i\in\{0,1\}\) 表示第 \(i\) 个位置是否被一个长度为 \(2\) 的区间包括。那么如果有 \(b_{i-1}=0\and b_i=0\and a_i=1\),那么可以把 \([i-1,i-1]\) 和 \([i,i]\) 拼成 \([i-1,i]\)。这样第二个 1 在算贡献的时候就从 \(+1\) 变成了 \(-1\)。


code

  1. #include <bits/stdc++.h>
  2. #define eb emplace_back
  3. #define pb push_back
  4. #define mk make_pair
  5. #define fi first
  6. #define se second
  7. using namespace std;
  8. typedef pair <int, int> pii;
  9. const int N = 2e5 + 10;
  10. inline int read ()
  11. {
  12. int x = 0, f = 1;
  13. char ch = getchar ();
  14. while (ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar (); }
  15. while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
  16. return x * f;
  17. }
  18. int n;
  19. int a[N];
  20. bool vis[N];
  21. vector <pii> ans;
  22. void solve ()
  23. {
  24. n = read (); int sum = 0;
  25. for (int i = 1; i <= n; i++) a[i] = read (), sum += a[i];
  26. if (sum & 1) return puts("-1"), void ();
  27. if (sum < 0) { sum = -sum; for (int i = 1; i <= n; i++) a[i] = -a[i]; } sum /= 2;
  28. for (int i = 2; i <= n && sum; i++)
  29. {
  30. if (a[i] > 0 && !vis[i - 1])
  31. {
  32. vis[i - 1] = vis[i] = true;
  33. ans.eb (i - 1, i);
  34. sum--;
  35. }
  36. }
  37. for (int i = 1; i <= n; i++) if (!vis[i]) ans.eb (i, i);
  38. sort (ans.begin (), ans.end ());
  39. printf ("%d\n", (int)ans.size ());
  40. for (auto i : ans) printf ("%d %d\n", i.fi, i.se);
  41. ans.clear ();
  42. for (int i = 1; i <= n; i++) vis[i] = false;
  43. }
  44. signed main ()
  45. {
  46. int t = read ();
  47. while (t--) solve ();
  48. return 0;
  49. }

Make Nonzero Sum (hard version)

解析:

题目大意:

本题目是CF1753A1的困难版本,不同之处为困难(hard)版本中 \(a\) 数组包含\(0\)。


思路:

长度为偶数的极长连续 0 段是没有意义的,可以忽略,长度为奇数的极长连续 0 段 可以合并成一个 0,其余做法见 C1。


code:

  1. #include <bits/stdc++.h>
  2. #define eb emplace_back
  3. #define pb push_back
  4. #define mk make_pair
  5. #define fi first
  6. #define se second
  7. using namespace std;
  8. typedef pair <int, int> pii;
  9. const int N = 2e5 + 10;
  10. const int INF = LLONG_MAX;
  11. inline int read ()
  12. {
  13. int x = 0, f = 1;
  14. char ch = getchar ();
  15. while (ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar (); }
  16. while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
  17. return x * f;
  18. }
  19. int n;
  20. int a[N];
  21. pii pos[N];
  22. bool vis[N];
  23. vector <pii> ans;
  24. void clear () { ans.clear (); for (int i = 1; i <= n; i++) vis[i] = false; }
  25. void solve ()
  26. {
  27. n = read ();
  28. int s = 0, len = 0; bool flag = false;
  29. for (int i = 1; i <= n; i++)
  30. {
  31. int x = read ();
  32. if (x)
  33. {
  34. if (s & 1) a[++len] = 0, pos[len] = mk (i - s, i - 1);
  35. a[++len] = x;
  36. pos[len] = mk (i - (s & 1 ? 0 : s), i);
  37. s = 0; flag = true;
  38. }
  39. else s++;
  40. }
  41. if (s) a[++len] = 0, pos[len] = mk (n - s + 1, n);
  42. if (!flag) return printf ("1\n1 %d\n", n), void ();
  43. n = len;
  44. int sum = 0;
  45. for (int i = 1; i <= n; i++) sum += a[i];
  46. if (sum & 1) return puts("-1"), void ();
  47. if (sum < 0) { sum = -sum; for (int i = 1; i <= n; i++) a[i] = -a[i]; }
  48. sum /= 2;
  49. for (int i = 2; i <= n && sum; i++)
  50. {
  51. if (a[i] > 0 && !vis[i - 1])
  52. {
  53. vis[i - 1] = vis[i] = true;
  54. ans.eb (pos[i - 1].fi, pos[i].se);
  55. sum--;
  56. }
  57. }
  58. for (int i = 1; i <= n; i++) if (!vis[i]) ans.eb (pos[i].fi, pos[i].se);
  59. sort (ans.begin (), ans.end ());
  60. printf ("%d\n", ans.size ());
  61. for (auto i : ans) printf ("%d %d\n", i.fi, i.se);
  62. clear ();
  63. }
  64. signed main ()
  65. {
  66. int t = read ();
  67. while (t--) solve ();
  68. return 0;
  69. }

Factorial Divisibility

解析:

题目大意:

给定两个正整数 \(n\) 和 \(x\) 和一个正整数序列 \(a_1 \sim a_n\)。

请问 \(\sum_{i = 1}^n a_i!\) 是否能被 \(x!\) 整除。如果能则输出一个字符串 \(\texttt{Yes}\),不能则输出字符串 \(\texttt{No}\)。


思路:

考虑合并,\(i+1\) 个 \(i!\) 可以合并成一个 \((i+1)!\),考虑从小到大合并,如果合并之后存在一个 \(i!\),且 \(i<x\),那么无解,否则有解。


code:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef pair <int, int> pii;
  4. const int N = 5e5 + 10;
  5. const int INF = LLONG_MAX;
  6. inline int read ()
  7. {
  8. int x = 0, f = 1;
  9. char ch = getchar ();
  10. while (ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar (); }
  11. while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
  12. return x * f;
  13. }
  14. int n, x;
  15. int bottle[N];
  16. signed main ()
  17. {
  18. n = read (); x = read ();
  19. for (int i = 1; i <= n; i++) bottle[read()]++;
  20. for (int i = 1; i < x; i++)
  21. {
  22. if (bottle[i] % (i + 1)) return puts("No"), 0;
  23. bottle[i + 1] += bottle[i] / (i + 1);
  24. }
  25. puts("Yes");
  26. return 0;
  27. }

Wish I Knew How to Sort

解析:

题目大意:

给定一个长度为 \(n\) 的 01 序列 \(a\) 和一种操作(\(1\le n\le2\times 10^5,\ a_i\in \{0,1\}\)),你需要用如下操作将序列从小到大排序。

  • 等概率随机选取两个位置 \(i,j\ (i<j)\),若 \(a_i>a_j\),则交换 \(a_i,a_j\)。

注意:当 \(a_i\le a_j\) 时,不进行交换,也算作一次操作。

请你求出操作被执行的期望次数。对 998244353 取模。


思路:

考虑 CF1151F,因为 0 的个数一定,设 0 的个数有 \(cnt\) 个,考虑 \(dp_{i}\) 表示前 \(cnt\) 位有 \(i\) 个 0 的期望操作次数,那么答案为 \(dp_{cnt}\)。

考虑从 \(i-1\) 转移到 \(i\) 的过程,我们需要在 \(\frac{n\times (n-1)}{2}\) 种不同的 \((i,j)\) 中(设 \(total=\frac{n\times (n-1)}{2}\)),在 \([1,cnt]\) 中任选一个 1,在 \((cnt,n]\) 中选一个 \(0\),此时前 \(cnt\) 个中有 \(i-1\) 个 0,这样的对数有 \([cnt-(i-1)]^2\) 种。

考虑转移:

\[dp_{i}=(dp_{i-1}+1)\times \frac{[cnt-(i-1)]^2}{total}+(dp_{i}+1)\times \frac{total-[cnt-(i-1)]^2}{total}\\
dp_{i}=dp_{i-1}\times \frac{[cnt-(i-1)]^2}{total}+\frac{[cnt-(i-1)]^2}{total}+dp_{i}\times \frac{total-[cnt-(i-1)]^2}{total}+\frac{total-[cnt-(i-1)]^2}{total}\\
dp_{i}=dp_{i-1}\times \frac{[cnt-(i-1)]^2}{total}+dp_{i}\times \frac{total-[cnt-(i-1)]^2}{total}+1\\
dp_{i}\times \frac{[cnt-(i-1)]^2}{total}=dp_{i-1}\times \frac{[cnt-(i-1)]^2}{total}+1\\
dp_{i}=(dp_{i-1}\times \frac{[cnt-(i-1)]^2}{total}+1)\times \frac{total}{[cnt-(i-1)]^2}\\
dp_{i}=dp_{i-1}+ \frac{total}{[cnt-(i-1)]^2}\\
\]

至此,我们已经可以 \(\mathcal O(n\log V)\) 计算答案(其中 \(\log V\) 是快速幂求逆元)。


code:

  1. #include <bits/stdc++.h>
  2. #define int long long
  3. #define pb push_back
  4. using namespace std;
  5. const int N = 5e5 + 10;
  6. const int mods = 998244353;
  7. typedef pair <int, int> pii;
  8. inline int read ( )
  9. {
  10. int x = 0, f = 1;
  11. char ch = getchar ();
  12. while (ch < '0' || ch > '9') {if (ch == '-') f = - 1; ch = getchar ();}
  13. while (ch >= '0' && ch <='9') {x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar (c);}
  14. return x * f;
  15. }
  16. int n;
  17. int a[N];
  18. int dp[N];
  19. inline int qpow (int a, int p)
  20. {
  21. int res = 1;
  22. while (p)
  23. {
  24. if (p & 1) res = (res * a) % mods;
  25. p >>= 1;
  26. a = (a * a) % mods;
  27. }
  28. return res;
  29. }
  30. void solve ()
  31. {
  32. n = read (); int cnt = 0, cnt2 = 0;
  33. for (int i = 1; i <= n; i++) a[i] = read (), cnt += !(a[i]);
  34. for (int i = 1; i <= cnt; i++) cnt2 += !(a[i]);
  35. int total = (n * (n - 1) / 2) % mods;
  36. for (int i = cnt2 + 1; i <= cnt; i++)
  37. {
  38. int t = (cnt - (i - 1)) * (cnt - (i - 1));
  39. dp[i] = (dp[i - 1] + (total * qpow (t, mods - 2)) % mods) % mods;
  40. }
  41. printf ("%lld\n", dp[cnt]);
  42. for (int i = 1; i <= cnt; i++) dp[i] = 0;
  43. }
  44. signed main()
  45. {
  46. int t = read ();
  47. while (t--) solve ();
  48. return 0;
  49. }

F

解析:

题目大意

题意没读,题解没写,题目没补,留坑。


思路:


code


Codeforces Round #829 (Div. 2)/CodeForces1754的更多相关文章

  1. Codeforces Round #829 (Div. 1/Div. 2) 1753 A B C D 题解

    Div1A / 2C. Make Nonzero Sum 令最后每个\(a_i\)的系数为\(c_i\)(\(c_i=1/-1\)),发现只要满足\(c_1=1\)(下标从1开始),且c中没有两个-1 ...

  2. Codeforces Round #829 (Div. 2) A-E

    比赛链接 A 题解 知识点:枚举. 只要一个Q后面有一个A对应即可,从后往前遍历,记录A的数量,遇到Q则数量减一,如果某次Q计数为0则NO. 时间复杂度 \(O(n)\) 空间复杂度 \(O(1)\) ...

  3. Codeforces Round #829 (Div. 2) D. Factorial Divisibility(数学)

    题目链接 题目大意: \(~~\)给定n个正整数和一个数k,问这n个数的阶乘之和能不能被k的阶乘整除 既:(a\(_{1}\)!+a\(_{2}\)!+a\(_{3}\)!+....+a\(_{n}\ ...

  4. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  5. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  6. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  7. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  8. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  9. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

随机推荐

  1. Linux 09 Vim

    参考源 https://www.bilibili.com/video/BV187411y7hF?spm_id_from=333.999.0.0 版本 本文章基于 CentOS 7.6 概述 Vi Vi ...

  2. "蔚来杯"2022牛客暑期多校训练营9 G Magic Spells【马拉车+哈希】

    四川今天又上热搜了,继南部疫情的未雨绸缪后,龙槽沟是真的倾盆大雨了.我没有兴趣虚伪矫情地对罹难的游人表达同情,因为人与人互不相通徒增谈资:我也没有兴趣居高临下地对擅闯的愚人表达不屑,因为你我皆为乌合之 ...

  3. Linux—权限管理

    Linux 权限管理 1.权限简介 Linux权限是操作系统用来限制对资源访问的机制,权限一般分为读.写.执行.系统中每个文件都拥有特定的权限:属主.属组以及其他人,通过这样的机制来限制哪些用户或用户 ...

  4. kubernetes网络模型

    Overview 本文将探讨Kubernetes中的网络模型,以及对各种网络模型进行分析. Underlay Network Model 什么是Underlay Network 底层网络 Underl ...

  5. iommu分析之---smmu v3的实现

    smmu 除了完成 iommu 的统一的ops 之外,有自己独特的一些地方. 1.Stream Table Stream Table是存在内存中的一张表,在SMMU设备初始化的时候由驱动程序创建好. ...

  6. MySQL设置字段从指定数字自增,比如10000

    MySQL设置字段从指定数字自增,比如10000. 方式一 方式二 方式一 此时就解决了MySQL从指定数字进行自增 CREATE TABLE hyxxb( hyid INT AUTO_INCREME ...

  7. SpringBoot RabbitMQ 注解版 基本概念与基本案例

    前言 人间清醒 目录 前言 Windows安装RabbitMQ 环境工具下载 Erlang环境安装 RabbitMQ安装 RabbitMQ Web管理端安裝 RabbitMQ新增超级管理员 Rabbi ...

  8. 【Django】DRF开发中的一些技巧记录

    问题记录 问题1:信号没有按预期触发 描述 编写了信号函数后,并没有如预期一般在必要时候触发,函数如下: @receiver(signals.post_save, sender=Prometheus) ...

  9. 座位安排(欧拉回路,高斯消元,bitset)

    题面 由于旋转大师 F r e n c h \rm French French 的离去, A r e x t r e \rm Arextre Arextre 光荣地承担了给全班换座位的重任. 由于这是 ...

  10. Springboot重定向,没有位置没有加“/user”,但是url在请求位置的前面“/user”就出现了

    是因为":"后面没有加"/" 原先的 return "redirect:main.html"; 正确的 return "redir ...