Codeforces Round 931 (Div. 2) A-D2
A. Too Min Too Max
贪心、排序。
对数组排序后,显然当下标
时间复杂度:
#include <bits/stdc++.h>
using namespace std;
#define cctie ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define all(x) x.begin(), x.end()
#define ALL(x) x.begin() + 1, x.end()
using i64 = long long;
void solve() {
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(ALL(a));
cout << 2 * (a[n] + a[n - 1] - a[1] - a[2]) << '\n';
}
void prework() {
}
int main() {
cctie;
prework();
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
B. Yet Another Coin Problem
枚举、贪心。
价值为
#include <bits/stdc++.h>
using namespace std;
#define cctie ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define all(x) x.begin(), x.end()
#define ALL(x) x.begin() + 1, x.end()
using i64 = long long;
void solve() {
int n;
cin >> n;
int ans = n;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 5; k++) {
for (int l = 0; l < 3; l++) {
int x = n - i - j * 3 - k * 6 - l * 10;
if (x >= 0 && x % 15 == 0) {
ans = min(ans, i + j + k + l + x / 15);
}
}
}
}
}
cout << ans << '\n';
}
void prework() {
}
int main() {
cctie;
prework();
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
C. Find a Mine
数学。
随机选择一个点作为询问点时,距离询问点最近的雷可能存在的位置太多,不好在
时间复杂度:
#include <bits/stdc++.h>
using namespace std;
#define cctie ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define all(x) x.begin(), x.end()
#define ALL(x) x.begin() + 1, x.end()
using i64 = long long;
int ask(int x, int y) {
cout << "? " << x << " " << y << endl;
int d;
cin >> d;
return d;
}
void get(int x, int y) {
cout << "! " << x << " " << y << endl;
}
void solve() {
int n, m;
cin >> n >> m;
int d1 = ask(1, 1), d2 = ask(1, m), d3 = ask(n, 1);
int x1 = (d1 + d2 + 3 - m) / 2, y1 = x1 - (d2 - m + 1);
int y2 = (d1 + d3 + 3 - n) / 2, x2 = y2 - (d3 - n + 1);
auto is_legal = [&](int x, int y)->bool {
return 1 <= x && x <= n && 1 <= y && y <= m && x + y == 2 + d1;
};
if (is_legal(x1, y1)) {
if (is_legal(x2, y2)) {
int d = ask(x1, y1);
if (d) {
get(x2, y2);
} else {
get(x1, y1);
}
} else {
get(x1, y1);
}
} else {
get(x2, y2);
}
}
void prework() {
}
int main() {
cctie;
prework();
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
D1. XOR Break — Solo Version
位运算、分类讨论。
这里讨论一种最多只需要操作 2 次的做法。
当二进制数
当二进制数
当
拆补完后,可以一步转换成
#include <bits/stdc++.h>
using namespace std;
#define cctie ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define all(x) x.begin(), x.end()
#define ALL(x) x.begin() + 1, x.end()
using i64 = long long;
void solve() {
i64 n, m;
cin >> n >> m;
if (__builtin_popcountll(n) == 1) {
cout << -1 << '\n';
} else {
int h = 0, k = 0;
for (int i = 62; ~i; i--) {
int u = n >> i & 1, v = m >> i & 1;
if (u) {
if (!h) {
h = i;
}
if (!v) {
k = i;
break;
}
}
}
vector<i64> ans(1, n);
n -= 1LL << k;
if (h == k) {
int lh;
for (int i = h - 1; ~i; i--) {
int u = n >> i & 1, v = m >> i & 1;
if (v > u) {
cout << -1 << '\n';
return;
}
if (u) {
lh = i;
break;
}
}
k = lh;
}
for (int i = k - 1; ~i; i--) {
int u = n >> i & 1;
if (!u) {
n += 1LL << i;
}
}
ans.push_back(n);
if (n != m) {
ans.push_back(m);
}
cout << ans.size() - 1 << '\n';
for (i64 i : ans) {
cout << i << " \n"[i == m];
}
}
}
void prework() {
}
int main() {
cctie;
prework();
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
D2. XOR Break — Game Version
位运算、博弈论。
我们先分析
当
当
所以我们得到偶必得奇且奇能得偶,而
由于操作次数限制在不超过
#include <bits/stdc++.h>
using namespace std;
#define cctie ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define all(x) x.begin(), x.end()
#define ALL(x) x.begin() + 1, x.end()
using i64 = long long;
void solve() {
i64 n;
cin >> n;
int turn;
if (__builtin_popcountll(n) & 1) {
cout << "second" << endl;
turn = 1;
} else {
cout << "first" << endl;
turn = 0;
}
i64 p1 = 1, p2 = 1;
while (p1 != 0 || p2 != 0) {
if (!turn) {
for (int i = 62; ~i; i--) {
int u = n >> i & 1;
if (u) {
p1 = 1LL << i;
break;
}
}
p2 = n ^ p1;
cout << p1 << " " << p2 << endl;
} else {
cin >> p1 >> p2;
if (__builtin_popcountll(p1) & 1) {
n = p2;
} else {
n = p1;
}
}
turn ^= 1;
}
}
void prework() {
}
int main() {
cctie;
prework();
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】