Codeforces Round 1006 (Div. 3)
A. New World, New Me, New Array
题意:你要选
点击查看代码
void solve() {
int n, k, p;
std::cin >> n >> k >> p;
k = std::abs(k);
if (p * n < k) {
std::cout << -1 << "\n";
return;
}
std::cout << (k + p - 1) / p << "\n";
}
B. Having Been a Treasurer in the Past, I Help Goblins Deceive
题意:一个字符串分为两种字符:'-'和'_'。你可以重排字符串,使得子序列"-_-"最多。
我们应该让'-'放两边,中间放'_'。两边'-'的数量应该越接近越好。
点击查看代码
void solve() {
int n;
std::cin >> n;
std::string s;
std::cin >> s;
i64 a = std::count(s.begin(), s.end(), '-');
i64 b = std::count(s.begin(), s.end(), '_');
i64 c = a / 2;
std::cout << c * (a - c) * b << "\n";
}
C. Creating Keys for StORages Has Become My Main Skill
题意:你要构造一个数组,使得所有数的或值等于
从
点击查看代码
void solve() {
int n, x;
std::cin >> n >> x;
if (n == 1) {
std::cout << x << "\n";
return;
}
std::vector<int> ans(n);
int sum = 0, max = 0, val = 0;
for (int i = 0; i < n; ++ i) {
sum |= i;
if ((sum & x) == sum) {
max = i;
val = sum;
}
}
if (max == n - 1 && (val & x) != x) {
-- max;
}
int i = 0;
while (i < n && max >= 0) {
ans[i] = max -- ;
++ i;
}
if (i < n) {
for (int j = i; j < n; ++ j) {
ans[j] = x;
}
}
for (int i = 0; i < n; ++ i) {
std::cout << ans[i] << " \n"[i == n - 1];
}
}
D. For Wizards, the Exam Is Easy, but I Couldn't Handle It
题意:左移数组的一个区间,使得逆序对最小。
左移一次相当于把
点击查看代码
void solve() {
int n;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; ++ i) {
std::cin >> a[i];
}
int sum = 0;
for (int i = 0; i < n; ++ i) {
for (int j = i + 1; j < n; ++ j) {
if (a[i] > a[j]) {
++ sum;
}
}
}
int l = 0, r = n - 1;
int ans = sum;
for (int i = 0; i < n; ++ i) {
int v = sum;
for (int j = i; j < n; ++ j) {
if (a[j] < a[i]) {
-- v;
} else if (a[j] > a[i]) {
++ v;
}
if (v < ans || (v == ans && r - l > j - i)) {
ans = v;
l = i, r = j;
}
}
}
std::cout << l + 1 << " " << r + 1 << "\n";
}
E. Do You Love Your Hero and His Two-Hit Multi-Target Attacks?
题意:构造不超过
这样的点对的
点击查看代码
void solve() {
int k;
std::cin >> k;
std::vector<std::pair<int, int>> ans;
int x = -1e9, y = -1e9;
while (k) {
int z = 0;
while ((i64)z * (z + 1) / 2 <= k) {
++ z;
}
k -= z * (z - 1) / 2;
for (int i = 1; i <= z; ++ i, ++ x) {
ans.push_back({x, y});
}
y += 1;
}
std::cout << ans.size() << "\n";
for (auto & [x, y] : ans) {
std::cout << x << " " << y << "\n";
}
}
F. Goodbye, Banker Life
题意:
这题也是人类智慧,不过其实还是有理论的。
感觉递推式应该和杨辉三角有关系,打表发现如果杨辉三角等于位置的数是奇数,那么值就是
点击查看代码
void solve() {
int n, k;
std::cin >> n >> k;
for (int i = 0; i < n; ++ i) {
std::cout << (((n - 1) & i) == i ? k : 0) << " \n"[i == n - 1];
}
}
G. I've Been Flipping Numbers for 300 Years and Calculated the Sum
题意:
发现当
观察数据范围我们得知需要不超过
对于
对于
代码省略了取模类。
点击查看代码
void solve() {
int n;
i64 k;
std::cin >> n >> k;
auto get = [&](int n, int k) -> Z {
std::vector<int> a;
while (n) {
a.push_back(n % k);
n /= k;
}
Z res = 0;
for (auto & x : a) {
res = res * k + x;
}
return res;
};
Z ans = 0;
if (k > n) {
ans += (Z)(k - n) * n;
k = n;
}
auto s1 = [&](i64 n) -> i64 {
return n * (n + 1) / 2;
};
auto s2 = [&](i64 n) -> i64 {
return n * (n + 1) * (2 * n + 1) / 6;
};
auto sum1 = [&](i64 l, i64 r) -> i64 {
return s1(r) - s1(l - 1);
};
auto sum2 = [&](i64 l, i64 r) -> i64 {
return s2(r) - s2(l - 1);
};
for (i64 l = 2, r; l <= k; l = r + 1) {
if (l * l <= n) {
r = l;
ans += get(n, l);
} else {
r = std::min(k, n / (n / l));
i64 a = n % l, d = n / l;
ans += (Z)a * sum1(l, r) - (sum2(l, r) - sum1(l, r) * l) * d + (Z)d * (r - l + 1);
}
}
std::cout << ans << "\n";
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!