Codeforces Round #829 (Div. 2) A-E
A
题解
知识点:枚举。
只要一个Q后面有一个A对应即可,从后往前遍历,记录A的数量,遇到Q则数量减一,如果某次Q计数为0则NO。
时间复杂度 \(O(n)\)
空间复杂度 \(O(1)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool solve() {
int n;
cin >> n;
string s;
cin >> s;
s = "?" + s;
int cnt = 0;
for (int i = n;i >= 1;i--) {
if (s[i] == 'Q') {
if (cnt == 0) return false;
cnt--;
}
else cnt++;
}
cout << "YES" << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << "NO" << '\n';
}
return 0;
}
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)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n / 2;i++) {
cout << i + n / 2 << ' ' << i << ' ';
}
if (n & 1) cout << n << ' ';
cout << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}
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)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[200007];
bool solve() {
int n;
cin >> n;
vector<int> pos;
for (int i = 1;i <= n;i++) cin >> a[i];
for (int i = 1;i <= n;i++) {
if (a[i]) pos.push_back(i);
}
if (pos.size() & 1) return false;
if (!pos.size()) {
cout << 1 << '\n';
cout << 1 << ' ' << n << '\n';
return true;
}
vector<pair<int, int>> v;
for (int i = 0;i < pos.size();i += 2) {
if (a[pos[i]] == a[pos[i + 1]]) {
if ((pos[i] & 1) == (pos[i + 1] & 1)) {
v.push_back({ pos[i], pos[i] });
v.push_back({ pos[i] + 1,pos[i + 1] });
}
else v.push_back({ pos[i],pos[i + 1] });
}
else {
if ((pos[i] & 1) != (pos[i + 1] & 1)) {
v.push_back({ pos[i], pos[i] });
v.push_back({ pos[i] + 1,pos[i + 1] });
}
else v.push_back({ pos[i],pos[i + 1] });
}
}
vector<pair<int, int>> ans;
int prer = 0;
for (auto [i, j] : v) {
if (i != prer + 1) ans.push_back({ prer + 1, i - 1 });
ans.push_back({ i,j });
prer = j;
}
if (ans.back().second != n) ans.push_back({ ans.back().second + 1,n });
cout << ans.size() << '\n';
for (auto [i, j] : ans) {
cout << i << ' ' << j << '\n';
}
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}
D
题解
知识点:数论,贪心。
记录每个数字出现的次数,尝试从小到大合成出 \(x\) 。从 \(1\) 开始往后遍历,每次将 \(i\) 合成 \(i+1\) ,显然 \(i+1\) 个 \(i\) 将产生 \(1\) 个 \(i+1\) 。如果出现非 \(x\) 的数 \(i\) 不能全部使用 ,那么整个式子就无法被 \(x!\) 整除。
时间复杂度 \(O(n)\)
空间复杂度 \(O(1)\)
代码
#include <bits/stdc++.h>
using namespace std;
int cnt[500007];
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, x;
cin >> n >> x;
for (int i = 1;i <= n;i++) {
int tmp;
cin >> tmp;
cnt[tmp]++;
}
for (int i = 1;i < x;i++) {
if (cnt[i] % (i + 1)) {
cout << "NO" << '\n';
return 0;
}
cnt[i + 1] += cnt[i] / (i + 1);
}
cout << "YES" << '\n';
return 0;
}
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}\),于是有:
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)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 998244353;
int a[200007];
ll qpow(ll a, ll k) {
ll ans = 1;
while (k) {
if (k & 1) ans = (ans * a) % mod;
k >>= 1;
a = (a * a) % mod;
}
return ans;
}
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
int cnt0 = count(a + 1, a + n + 1, 0);
int cnt1 = count(a + 1, a + cnt0 + 1, 1);
int c2 = 1LL * n * (n - 1) / 2 % mod;
int ans = 0;
for (int i = 1;i <= cnt1;i++) {
ans = (ans + 1LL * c2 * qpow(1LL * i * i % mod, mod - 2) % mod) % mod;
}
cout << ans << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}
Codeforces Round #829 (Div. 2) A-E的更多相关文章
- 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 ...
- Codeforces Round #829 (Div. 2)/CodeForces1754
CodeForces1754 注:所有代码均为场上所书 Technical Support 解析: 题目大意 给定一个只包含大写字母 \(\texttt{Q}\) 和 \(\texttt{A}\) 的 ...
- Codeforces Round #829 (Div. 2) D. Factorial Divisibility(数学)
题目链接 题目大意: \(~~\)给定n个正整数和一个数k,问这n个数的阶乘之和能不能被k的阶乘整除 既:(a\(_{1}\)!+a\(_{2}\)!+a\(_{3}\)!+....+a\(_{n}\ ...
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- 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 ...
随机推荐
- Redis 16 哨兵模式
参考源 https://www.bilibili.com/video/BV1S54y1R7SB?spm_id_from=333.999.0.0 版本 本文章基于 Redis 6.2.6 概述 主从切换 ...
- python爬虫之BeautifulSoup4使用
钢铁知识库,一个学习python爬虫.数据分析的知识库.人生苦短,快用python. 上一章我们讲解针对结构化的html.xml数据,使用Xpath实现网页内容爬取.本章我们再来聊另一个高效的神器:B ...
- Linux配置bond模式 双网卡绑定步骤
什么是bond 网卡bond是通过多张网卡绑定为一个逻辑网卡,实现本地网卡的冗余,带宽扩容和负载均衡,在生产场景中是一种常用的技术.Kernels 2.4.12及以后的版本均供bonding模块,以前 ...
- KingbaseES 与Oracle 函数稳定性对于性能影响差异比较
一.函数的属性 KingbaseES 函数在定义时有三种稳定性级别:volatile.stable 和 immutable.默认情况下,创建函数的稳定性为volatile.以下是这三种函数的区别: V ...
- 【读书笔记】C#高级编程 第十二章 动态语言扩展
(一)DLR C#4的动态功能是Dynamic Language Runtime(动态语言运行时,DLR)的一部分.DLR是添加到CLR的一系列服务. (二)dynamic类型 dynamic类型允许 ...
- 当web项目没有配置<welcome-file>index_1.jsp</welcome-file>默认标签启动tomcat后默认访问的页面是什么呢?
当web项目没有配置index_1.jsp默认标签启动tomcat后默认访问的页面是什么呢? 结果我启动后居然默认打开了index.jsp页面 为什么呢?为什么会访问我的.indexjsp页面呢?不是 ...
- 【2022-09-09】Django框架(九)
Django框架(九) cookie与session简介 网址的发展史: 1.起初网站都没有保存用户功能的需求,所有用户访问返回的结果都是一样的. 比如:新闻网页,博客网页,小说... (这些网页是不 ...
- 重复造轮子 SimpleMapper
接手的项目还在用 TinyMapper 的一个早期版本用来做自动映射工具,TinyMapper 虽然速度快,但在配置里不能转换类型,比如 deleted 在数据库中用 0.1 表示,转换成实体模型时没 ...
- http服务(postman调用方法及反参)
#region 监听url #region 监听url路径请求 static HttpListener httpobj; private void listeningUrl() { //提供一个简单的 ...
- G&GH05 删除文件和.gitignore
注意事项与声明 平台: Windows 10 作者: JamesNULLiu 邮箱: jamesnulliu@outlook.com 博客: https://www.cnblogs.com/james ...