Codeforces Round #741 (Div. 2) 个人题解 A~D
比赛链接:Here
1562A. The Miracle and the Sleeper
题意:
- 给出
求出最大化的 ( )
思路:
很容易就看出
但如果
时间复杂度:
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
题意:
- 给出一个字符串
请问可以从字符串中删除的最大位数是多少使得字符串数字变为非素数? 如: 删除第二位的 变为
思路:
说实话赛时想复杂了,因为在
但利用素数筛就能很快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
题意:
- 给出一个
字符串,以及函数 将 转为十进制整数,请找出两对下标 使得 成立
思路:
麻了,前面应是没想到怎么去构造一个这样的等式,手写
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)
题意:
题意待补
思路:
把
核心在于利用前缀和进行二分(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";
}
}
}
}
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)