CF-CodeTon-3解题报告
A. Indirect Sort
题意:有一个排列
,每次可以选三个不同的位置,从左到右依次为 。如果 ,将 加上 ,否则交换 。问是否能将其排成非降序列。
贪心。如果第一个元素是
By tourist
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
bool ok = true;
for (int i = 0; i < n; i++) {
ok &= (a[0] <= a[i]);
}
cout << (ok ? "Yes" : "No") << '\n';
}
return 0;
}
B. Maximum Substring
题意:定义一个 01 字符串的权值为:如果只有 0 或只有 1,则为 0/1 个数的平方,否则为 0 的个数乘 1 的个数。给定一个字符串,找出所有子串的最大权值。
分类讨论。有 0 有 1 时,全选一定最优,否则为最长的连续 0/1 子串长度的平方。三种情况取最大值即可。
By tourist
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
string s;
cin >> s;
int mx = 1;
int t = 1;
for (int i = 1; i < n; i++) {
if (s[i] == s[i - 1]) {
t += 1;
mx = max(mx, t);
} else {
t = 1;
}
}
int x = 0;
for (int i = 0; i < n; i++) {
x += (s[i] == '0');
}
int y = n - x;
long long ans = (long long) x * y;
ans = max(ans, (long long) mx * mx);
cout << ans << '\n';
}
return 0;
}
C. Complementary XOR
题意:有两个 01 串
,每次操作可以选一个区间,将 这段区间内的元素取反, 这段元素外的区间取反。构造一种方案,在 次操作内将两个串归零,或判断无解。
容易发现,进行操作后
于是,我们先对每个 1 的位单独选中取反,最多
By jiangly
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n;
std::cin >> n;
std::string a, b;
std::cin >> a >> b;
for (int i = 0; i < n; i++) {
if (a[i] ^ b[i] ^ a[0] ^ b[0]) {
std::cout << "NO\n";
return;
}
}
std::cout << "YES\n";
std::vector<std::array<int, 2>> ans;
for (int i = 0; i < n; i++) {
if (a[i] == '1') {
ans.push_back({i, i + 1});
}
}
if (a[0] ^ b[0] ^ (ans.size() & 1)) {
ans.push_back({0, 1});
ans.push_back({1, n});
ans.push_back({0, n});
}
std::cout << ans.size() << "\n";
for (auto [l, r] : ans) {
std::cout << l + 1 << " " << r << "\n";
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
D. Count GCD
题意:给你一个数组
,求数组 的方案数,满足 的值域在 ,且 为 的前缀 。
由于
By tourist
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
Mint ans = 1;
for (int i = 0; i < n - 1; i++) {
int x = a[i];
int y = a[i + 1];
if (x % y != 0) {
ans = 0;
break;
}
if (x == y) {
ans *= m / x;
continue;
}
int bound = m / y;
int num = x / y;
vector<int> fs;
for (int j = 2; j * j <= num; j++) {
if (num % j == 0) {
fs.push_back(j);
while (num % j == 0) {
num /= j;
}
}
}
if (num > 1) {
fs.push_back(num);
}
int sz = (int) fs.size();
int cnt = 0;
for (int mask = 0; mask < (1 << sz); mask++) {
int sign = 1;
int u = 1;
for (int j = 0; j < sz; j++) {
if (mask & (1 << j)) {
sign *= -1;
u *= fs[j];
}
}
cnt += sign * (bound / u);
}
ans *= cnt;
}
cout << ans << '\n';
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步