比赛链接

A

题解

知识点:枚举。

只要一个Q后面有一个A对应即可,从后往前遍历,记录A的数量,遇到Q则数量减一,如果某次Q计数为0则NO。

时间复杂度 \(O(n)\)

空间复杂度 \(O(1)\)

代码

  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. bool solve() {
  5. int n;
  6. cin >> n;
  7. string s;
  8. cin >> s;
  9. s = "?" + s;
  10. int cnt = 0;
  11. for (int i = n;i >= 1;i--) {
  12. if (s[i] == 'Q') {
  13. if (cnt == 0) return false;
  14. cnt--;
  15. }
  16. else cnt++;
  17. }
  18. cout << "YES" << '\n';
  19. return true;
  20. }
  21. int main() {
  22. std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  23. int t = 1;
  24. cin >> t;
  25. while (t--) {
  26. if (!solve()) cout << "NO" << '\n';
  27. }
  28. return 0;
  29. }

B

题解

知识点:构造。

可以证明 \(\lfloor \frac{n}{2} \rfloor\) 是最优答案。交错构造, \(i+\lfloor \frac{n}{2} \rfloor\) 和 \(i\) ,注意 \(i\) 从 \(1\) 到 \(\lfloor \frac{n}{2} \rfloor\) ,在最后如果 \(n\) 是奇数则补一个 \(n\) 。

时间复杂度 \(O(n)\)

空间复杂度 \(O(1)\)

代码

  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. bool solve() {
  5. int n;
  6. cin >> n;
  7. for (int i = 1;i <= n / 2;i++) {
  8. cout << i + n / 2 << ' ' << i << ' ';
  9. }
  10. if (n & 1) cout << n << ' ';
  11. cout << '\n';
  12. return true;
  13. }
  14. int main() {
  15. std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  16. int t = 1;
  17. cin >> t;
  18. while (t--) {
  19. if (!solve()) cout << -1 << '\n';
  20. }
  21. return 0;
  22. }

C

题解

知识点:构造。

可以两两构造。找到一对非 \(0\) 数 \(a[i],a[j]\) ,当 \(a[i] = a[j]\),如果 \(i,j\) 奇偶性相同则 \([i,i],[i+1,j]\) ,否则分段 \([i,j]\) ;当 \(a[i] \neq a[j]\) ,如果 \(i,j\) 奇偶性相同则 \([i,j]\) ,否则 \([i,i],[i+1,j]\) 。

注意两对之间以及首尾可能会存在空隙,最后要把上面答案遍历一遍,填补空隙。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. int a[200007];
  5. bool solve() {
  6. int n;
  7. cin >> n;
  8. vector<int> pos;
  9. for (int i = 1;i <= n;i++) cin >> a[i];
  10. for (int i = 1;i <= n;i++) {
  11. if (a[i]) pos.push_back(i);
  12. }
  13. if (pos.size() & 1) return false;
  14. if (!pos.size()) {
  15. cout << 1 << '\n';
  16. cout << 1 << ' ' << n << '\n';
  17. return true;
  18. }
  19. vector<pair<int, int>> v;
  20. for (int i = 0;i < pos.size();i += 2) {
  21. if (a[pos[i]] == a[pos[i + 1]]) {
  22. if ((pos[i] & 1) == (pos[i + 1] & 1)) {
  23. v.push_back({ pos[i], pos[i] });
  24. v.push_back({ pos[i] + 1,pos[i + 1] });
  25. }
  26. else v.push_back({ pos[i],pos[i + 1] });
  27. }
  28. else {
  29. if ((pos[i] & 1) != (pos[i + 1] & 1)) {
  30. v.push_back({ pos[i], pos[i] });
  31. v.push_back({ pos[i] + 1,pos[i + 1] });
  32. }
  33. else v.push_back({ pos[i],pos[i + 1] });
  34. }
  35. }
  36. vector<pair<int, int>> ans;
  37. int prer = 0;
  38. for (auto [i, j] : v) {
  39. if (i != prer + 1) ans.push_back({ prer + 1, i - 1 });
  40. ans.push_back({ i,j });
  41. prer = j;
  42. }
  43. if (ans.back().second != n) ans.push_back({ ans.back().second + 1,n });
  44. cout << ans.size() << '\n';
  45. for (auto [i, j] : ans) {
  46. cout << i << ' ' << j << '\n';
  47. }
  48. return true;
  49. }
  50. int main() {
  51. std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  52. int t = 1;
  53. cin >> t;
  54. while (t--) {
  55. if (!solve()) cout << -1 << '\n';
  56. }
  57. return 0;
  58. }

D

题解

知识点:数论,贪心。

记录每个数字出现的次数,尝试从小到大合成出 \(x\) 。从 \(1\) 开始往后遍历,每次将 \(i\) 合成 \(i+1\) ,显然 \(i+1\) 个 \(i\) 将产生 \(1\) 个 \(i+1\) 。如果出现非 \(x\) 的数 \(i\) 不能全部使用 ,那么整个式子就无法被 \(x!\) 整除。

时间复杂度 \(O(n)\)

空间复杂度 \(O(1)\)

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int cnt[500007];
  4. int main() {
  5. std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  6. int n, x;
  7. cin >> n >> x;
  8. for (int i = 1;i <= n;i++) {
  9. int tmp;
  10. cin >> tmp;
  11. cnt[tmp]++;
  12. }
  13. for (int i = 1;i < x;i++) {
  14. if (cnt[i] % (i + 1)) {
  15. cout << "NO" << '\n';
  16. return 0;
  17. }
  18. cnt[i + 1] += cnt[i] / (i + 1);
  19. }
  20. cout << "YES" << '\n';
  21. return 0;
  22. }

E

题解

知识点:概率dp。

设 \(f[i]\) 代表将 \(i\) 个还没排好的 \(1\) (如 1100101 有 \(2\) 个 \(1\) 没排好)排好的期望步数。

对于 \(f[i]\) ,下一步排好一个 \(1\) (即到达 \(i-1\) 状态)的概率是 \(\dfrac{i^2}{C_n^2}\) ,下一步啥都没变的概率就是 \(1-\dfrac{i^2}{C_n^2}\),于是有:

\[\begin{aligned}
f[i] &= (f[i-1]+1) \cdot \dfrac{i^2}{C_n^2} + (f[i]+1) \cdot (1-\dfrac{i^2}{C_n^2})\\
\dfrac{i^2}{C_n^2} \cdot f[i] &= \dfrac{i^2}{C_n^2} \cdot f[i-1] + 1\\
f[i] &= f[i-1] + \dfrac{C_n^2}{i^2}
\end{aligned}
\]

一步到达 \(i-1\) 后再排完的期望这步的概率一步啥也没干的期望这步的概率就是 \(f[i]\) 。

于是可以递推,\(f[0] = 0\) ,求的是 \(f[cnt1]\) ,\(cnt1\) 是初始没排好 \(1\) 的个数。

这里其实有个概率论的定理:如果一个事件的结果A发生的概率是 \(P\) ,则一直做这件事直到第一次发生结果A的期望 \(X\) 是 \(\dfrac{1}{P}\) 。

证明:

\[\begin{aligned}
X &= 1\cdot P+(X+1)\cdot (1-P)\\
P\cdot X &= 1\\
X &= \frac{1}{P}
\end{aligned}
\]

时间复杂度 \(O(n\log n)\)

空间复杂度 \(O(n)\)

代码

  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. const int mod = 998244353;
  5. int a[200007];
  6. ll qpow(ll a, ll k) {
  7. ll ans = 1;
  8. while (k) {
  9. if (k & 1) ans = (ans * a) % mod;
  10. k >>= 1;
  11. a = (a * a) % mod;
  12. }
  13. return ans;
  14. }
  15. bool solve() {
  16. int n;
  17. cin >> n;
  18. for (int i = 1;i <= n;i++) cin >> a[i];
  19. int cnt0 = count(a + 1, a + n + 1, 0);
  20. int cnt1 = count(a + 1, a + cnt0 + 1, 1);
  21. int c2 = 1LL * n * (n - 1) / 2 % mod;
  22. int ans = 0;
  23. for (int i = 1;i <= cnt1;i++) {
  24. ans = (ans + 1LL * c2 * qpow(1LL * i * i % mod, mod - 2) % mod) % mod;
  25. }
  26. cout << ans << '\n';
  27. return true;
  28. }
  29. int main() {
  30. std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  31. int t = 1;
  32. cin >> t;
  33. while (t--) {
  34. if (!solve()) cout << -1 << '\n';
  35. }
  36. return 0;
  37. }

Codeforces Round #829 (Div. 2) A-E的更多相关文章

  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)/CodeForces1754

    CodeForces1754 注:所有代码均为场上所书 Technical Support 解析: 题目大意 给定一个只包含大写字母 \(\texttt{Q}\) 和 \(\texttt{A}\) 的 ...

  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. CF580D Kefa and Dishes (状压DP)

    枚举最后食物 #include <iostream> #include <cstdio> #include <cstring> #include <algor ...

  2. django中视图函数的FBV和CBV

    1.什么是FBV和CBV FBV是指视图函数以普通函数的形式:CBV是指视图函数以类的方式. 2.普通FBV形式 def index(request): return HttpResponse('in ...

  3. 高并发之网络IO模型

    你好,我是坤哥 今天我们聊一下高并发下的网络 IO 模型 高并发即我们所说的 C10K(一个 server 服务 1w 个 client),C10M,写出高并发的程序相信是每个后端程序员的追求,高并发 ...

  4. 透过inode来理解硬链接和软链接

    什么是inode? 每个文件都对应一个唯一的inode,inode用来存储文件的元信息,包括: 对应的文件 文件字节数 文件数据块的位置 文件的inode号码 文件的硬链接数 文件的读写权限 文件的时 ...

  5. [PostgreSql]生产级别数据库安装要考虑哪些问题?

    大家好,我是字母哥(coder)! 我让公司的小伙伴写一个生产级别的PostgreSQL的安装文档,结果他和我说:"不是用一个命令就能安装好么?还用写文档么?".我知道他想说的是这 ...

  6. 【MySQL】从入门到精通8-SQL数据库编程

    上期:[MySQL]从入门到精通7-设计多对多数据库 第零章:Mac用户看这里: mac终端写MySQL和windows基本相同,除了配置环境变量和启动有些许不同以外. 先配置环境变量,在终端输入vi ...

  7. 【SWIFT】从零开始的SWIFT语言学习笔记-2:简单值、数组与字典

    1.0.3 简单值.数组与字典 知识点: 使用var创建变量 var myVariable = 65 myVariable = myVariable + 1 使用let创建常量 let myConst ...

  8. ping: sina.cn: Name or service not known

    该方法针对Ubuntu18及以后版本. 第一次遇到ping:报错Name or service not known这个问题在百度上找了很久说的都是什么修改 /etc/resolv.conf,但每次修改 ...

  9. DispatcherServlet 分发流程

    0 太长不看版 HTTPServlet 的 Service 方法将请求按类进行分解 主要是根据HTTP方法的类型调用 doXXX 方法 GET 和 HEAD 方法需要对 if-modified-sin ...

  10. Ubuntu系统apt添加第三方PPA源

    一.前言 1.1目的 在使用Ubuntu时往往apt源会自带很多常用软件,但是大部分都是比较老的版本,本文主要是为了实现以下两个目的: 通过添加第三方的PPA源解决软件版本过低或者没有安装包的情况: ...