Codeforces Round #741 (Div. 2) 个人题解 A~D
比赛链接:Here
1562A. The Miracle and the Sleeper
题意:
- 给出 \(l,r\) 求出最大化的 \(a\ mod\ b\) (\(l\le b\le b\le a\le r\))
思路:
很容易就看出 \(l\le ⌊\frac r2⌋ + 1\) 时 \(r\bmod \left(\left\lfloor\frac{r}{2}\right\rfloor+1\right)=\left\lfloor\frac{r-1}{2}\right\rfloor\) 就是最大可能的答案,
但如果 \(l > ⌊\frac r2⌋ + 1\) 的话最大值应该是 \(r\bmod l=r - l\)
时间复杂度:\(\mathcal{O}(1)\)
void solve() {
ll l, r;
cin >> l >> r;
if (r / 2 >= l) cout << ((r - 1) / 2) << "\n";
else cout << r % l << "\n";
}
1562B. Scenes From a Memory
题意:
- 给出一个字符串 \(s\; (|s|\le50)\) 请问可以从字符串中删除的最大位数是多少使得字符串数字变为非素数? 如:\(237\) 删除第二位的 \(3\) 变为 \(27\)
思路:
说实话赛时想复杂了,因为在 \(|s|\le 50\) 的情况下其实素数并不多(实际也只要考虑 \(3\) 位数的情况就ok),即使直接去模拟情况都可以。
但利用素数筛就能很快AC了
bool prime[110];
int n;
string s;
void solve() {
for (int i = 0; i < n; i++) {
if (s[i] == '1' || s[i] == '4' || s[i] == '6' || s[i] == '8' || s[i] == '9') {
cout << 1 << endl;
cout << s[i] << endl;
return;
}
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (!prime[(s[i] - '0') * 10 + (s[j] - '0')]) {
cout << 2 << endl;
cout << s[i] << s[j] << endl;
return;
}
}
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
for (int i = 2; i < 100; ++i) {
prime[i] = 1;
for (int j = 2; j * j <= i; ++j)
if (i % j == 0)prime[i] = 0;
}
int _; for (cin >> _; _--;) {
cin >> n >> s;
solve();
}
}
1562C. Rings
题意:
- 给出一个 \(01\) 字符串,以及函数 \(f(s):\) 将 $01 $ 转为十进制整数,请找出两对下标 \((l_1,r_1),(l_2,r_2)\) 使得 \(f(s_{(l_1,r_1)}) = k\times f(s_{(l_2,r_2)})\) 成立
思路:
麻了,前面应是没想到怎么去构造一个这样的等式,手写 \(11,..,1\) 和 \(00,...,0\) 等特殊情况的时候想到我只要定位到第一个 \(0(pos)\) 的位置就可以了,然后以 \(pos\) 为界划分为两部分就肯定能构造 \(f(s_{(l_1,r_1)}) = k\times f(s_{(l_2,r_2)})\)
const int N = 1e5 + 10;
int main() {
// ! 记得注释掉,麻了,输入流乱了导致后面debug半天没找到问题
// cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
ll n; cin >> n;
char s[N];
scanf("%s", s + 1);
ll cnt0 = 0, cnt1 = 0;
for (int i = 1; i <= n; ++i) if (s[i] == '1') cnt1 += 1;
if (cnt1 == n) {
cout << "1 " << n - 1 << " 2 " << n << "\n";
continue;
}
int p = 0;
for (int i = 1; i <= n; ++i)
if (s[i] == '0') {
p = i;
break;
}
if (p - 1 < (n / 2)) cout << p << " " << n << " " << p + 1 << " " << n << "\n";
else cout << 1 << " " << p << " " << 1 << " " << p - 1 << "\n";
}
}
1562D. Two Hundred Twenty One (Easy and Hard)
题意:
题意待补
思路:
把 \(+,-\) 号转为 \(+1,-1\) 再维护前缀和,可以快速判断奇偶(D1)
核心在于利用前缀和进行二分(D2)
不过有人用 map + set 也过了就很神奇(TQL
// D1
const int N = 3e5 + 10;
int a[N], sum[N];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
int n, q;
string s;
cin >> n >> q >> s;
int id = 0;
for (int i = 0; i < s.size(); ++i) {
if (i & 1) {
if (s[i] == '+') a[++id] = -1;
else a[++id] = 1;
} else {
if (s[i] == '-') a[++id] = -1;
else a[++id] = 1;
}
sum[id] = sum[id - 1] + a[id];
}
while (q--) {
int l, r; cin >> l >> r;
if (abs(sum[r] - sum[l - 1]) == 0) cout << "0\n";
else if (abs(sum[r] - sum[l - 1]) & 1) cout << "1\n";
else cout << "2\n";
}
}
}
// D2
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
int n, q;
cin >> n >> q;
string _s;
cin >> _s;
vector<int> which(n);
for (int i = 0; i < n; i++) {
which[i] = (_s[i] == '-') != (i & 1);
}
vector<int> psum(n + 1, 0);
for (int i = 0; i < n; i++) {
psum[i + 1] = psum[i] + (which[i] ? -1 : 1);
}
while (q--) {
int l, r;
cin >> l >> r; l--; r--;
if (psum[r + 1] == psum[l]) cout << "0\n\n";
else {
if ((r + 1 - l) & 1) cout << "1\n";
else {
cout << "2\n";
cout << (r + 1) << ' ';
r--;
}
int d = min(psum[l], psum[r + 1]) + abs(psum[l] - psum[r + 1]) / 2;
// d -> d+1
int s = l;
int e = r + 1;
while (s + 1 < e) {
int m = (s + e) / 2;
if ((psum[m] <= d) == (psum[s] <= d)) s = m;
else e = m;
}
cout << s + 1 << "\n";
}
}
}
}
Codeforces Round #741 (Div. 2) 个人题解 A~D的更多相关文章
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- Codeforces Round #538 (Div. 2) (A-E题解)
Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...
- Codeforces Round #531 (Div. 3) ABCDEF题解
Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...
- Codeforces Round #527 (Div. 3) ABCDEF题解
Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...
- Codeforces Round #499 (Div. 1)部分题解(B,C,D)
Codeforces Round #499 (Div. 1) 这场本来想和同学一起打\(\rm virtual\ contest\)的,结果有事耽搁了,之后又陆陆续续写了些,就综合起来发一篇题解. B ...
- Codeforces Round #545 (Div. 1) 简要题解
这里没有翻译 Codeforces Round #545 (Div. 1) T1 对于每行每列分别离散化,求出大于这个位置的数字的个数即可. # include <bits/stdc++.h&g ...
- Codeforces Round #624 (Div. 3)(题解)
Codeforces Round #624 (Div.3) 题目地址:https://codeforces.ml/contest/1311 B题:WeirdSort 题意:给出含有n个元素的数组a,和 ...
- Codeforces Round #821(Div.2) (A-C) 题解
Codeforces Round #821(Div.2) (A-C) A.Consecutive Sum 大致题意 给定一组共 n 个数据 ,如果俩个数的下标在 mod k 意义下同余,则可以交换a[ ...
随机推荐
- uni-app框架开发app发布流程
uni-app框架开发app发布流程 1.首先公司申请软著 步骤:申请软著详细流程 - 阿长*长 - 博客园 (cnblogs.com) 一.安卓端 1,点击发行>原生-app云打包 正式包和自 ...
- MySQL运行在docker容器中会损失多少性能
前言 自从使用docker以来,就经常听说MySQL数据库最好别运行在容器中,性能会损失很多.一些之前没使用过容器的同事,对数据库运行在容器中也是忌讳莫深,甚至只要数据库跑在容器中出现性能问题时,首先 ...
- 华为ar502H物联网边缘计算网关,在容器内控制/dev/do0开关命令
执行以下命令进行开关do继电开关,可以听见电位器声音. echo -en "\x01" > /dev/do0 echo -en "\x00" > ...
- 解决Vscode中代码格式化时老换行
问题: 小颖用vscode的格式化代码后发现代码老是换行,有时看起来就很难受,比如下面的: 问度娘后终于弄好啦,记录下,省的以后换电脑了重装了vscode又不会了,主要是百度给的解决方法好几个,但有的 ...
- 小闫s人格大爆发 | 坐椅子上?study:sleep
hadoop视频 搭建环境 刷单词 看电影
- jmeter二次开发自定义函数助手
需求:在工作中,需要使用唯一的字符串来作为订单ID,于是想到了UUID,要求uuid中不能有特殊字符包括横线,所以就有了重新写一个uuid进行使用: 准备:idea 依赖包: 注意事项:必须有包且包的 ...
- MySQL搭建主从集群详细步骤~
一. Docker安装MySQL搭建主从 docker run [OPTIONS] IMAGE [COMMAND] [ARG...] docker run -p 3306:3306 很多 -d --n ...
- 实践解析HPA各关联组件扭转关系
本文分享自华为云社区<HPA各关联组件扭转关系以及建议>,作者:可以交个朋友. 一.背景 应用程序的使用存在波峰波谷现象,在应用流量处于低谷期间,可以释放因过多的Pod而浪费的硬件资源.在 ...
- 华企盾DSC服务器配置无法创建数据库
测试连接的数据库用户没有创建数据库的权限,应该打开数据库控制台,把对应用户的权限都勾上
- 【Python微信机器人】第六七篇: 封装32位和64位Python hook框架实战打印微信日志
目录修整 目前的系列目录(后面会根据实际情况变动): 在windows11上编译python 将python注入到其他进程并运行 注入Python并使用ctypes主动调用进程内的函数和读取内存结构体 ...