Codeforces Round 945 (Div. 2) A-D
A. Chess For Three
模拟。
首先可以发现每一次对局三人的得分总和加
#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;
using i128 = __int128;
const int INF = 0x3f3f3f3f;
void solve() {
vector<int> p(3);
cin >> p[0] >> p[1] >> p[2];
int sum = accumulate(all(p), 0);
if (sum % 2) {
cout << -1 << '\n';
} else {
int ans = 0;
sort(all(p));
while (p[2] != 0 && p[1] != 0) {
p[1]--, p[2]--;
ans++;
sort(all(p));
}
cout << ans << '\n';
}
}
void prework() {
}
int main() {
cctie;
prework();
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
B. Cat, Fox and the Lonely Array
二分、前缀和。
考虑二分
证明可二分性:如果数组
时间复杂度:
#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;
using i128 = __int128;
const int INF = 0x3f3f3f3f;
void solve() {
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<array<int, 20>> s(n + 1);
for (int j = 0; j < 20; j++) {
for (int i = 1; i <= n; i++) {
s[i][j] = s[i - 1][j] + (a[i] >> j & 1);
}
}
auto check = [&](int k)->bool {
for (int j = 0; j < 20; j++) {
for (int i = k + 1; i <= n; i++) {
if (!!(s[i][j] - s[i - k][j]) != !!(s[i - 1][j] - s[i - 1 - k][j])) {
return false;
}
}
}
return true;
};
int l = 1, r = n;
while (l < r) {
int mid = l + r >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
cout << l << '\n';
}
void prework() {
}
int main() {
cctie;
prework();
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
C. Cat, Fox and Double Maximum
贪心。
注意到
时间复杂度:
#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;
using i128 = __int128;
const int INF = 0x3f3f3f3f;
void solve() {
int n;
cin >> n;
vector<int> p(n + 1);
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
vector<int> ord(n + 1);
iota(ALL(ord), 1);
sort(ALL(ord), [&](int i, int j){return p[i] < p[j];});
set<int> S;
for (int i = 1; i <= n; i++) {
S.insert(i);
}
bool odd = (ord[n] & 1);
vector<int> q(n + 1);
for (int ii = 1; ii <= n; ii++) {
int i = ord[ii];
if (odd) {
if ((i & 1) && i != 1) {
q[i] = *S.rbegin();
S.erase(--S.end());
}
} else {
if (!(i & 1) && i != n) {
q[i] = *S.rbegin();
S.erase(--S.end());
}
}
}
if (odd) {
for (int i = 2; i <= n; i += 2) {
int x = INF;
if (i != n) {
x = min(x, p[i + 1] + q[i + 1]);
}
if (i != 2) {
x = min(x, p[i - 1] + q[i - 1]);
}
auto it = --S.lower_bound(x - p[i]);
q[i] = *it;
S.erase(it);
}
} else {
for (int i = 3; i <= n; i += 2) {
int x = INF;
if (i != n - 1) {
x = min(x, p[i + 1] + q[i + 1]);
}
if (i != 1) {
x = min(x, p[i - 1] + q[i - 1]);
}
auto it = --S.lower_bound(x - p[i]);
q[i] = *it;
S.erase(it);
}
}
for (int i = 1; i <= n; i++) {
if (!q[i]) {
q[i] = *S.begin();
S.erase(S.begin());
}
}
for (int i = 1; i <= n; i++) {
cout << q[i] << " \n"[i == n];
}
}
void prework() {
}
int main() {
cctie;
prework();
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
D. Cat, Fox and Maximum Array Split
交互、思维。
时间复杂度:
#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;
using i128 = __int128;
const int INF = 0x3f3f3f3f;
void solve() {
int n, k;
cin >> n >> k;
auto query = [&](int l, int x)->int {
cout << "? " << l << " " << x << endl;
int r;
cin >> r;
return r;
};
int mx = n;
while (query(1, n * mx) != n) {
mx--;
}
auto check = [&](int m)->bool {
int cur = 0;
for (int i = 0; i < k; i++) {
if (cur == n) {
return false;
}
int pos = query(cur + 1, m);
if (pos == n + 1) {
return false;
}
cur = pos;
}
return cur == n;
};
auto output = [&](int m)->void {
cout << "! " << m << endl;
int r;
cin >> r;
};
for (int t = n / k; t >= 1; t--) {
int m = t * mx;
if (check(m)) {
output(m);
goto over;
}
}
output(-1);
over:
}
void prework() {
}
int main() {
cctie;
prework();
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
分类:
题解 / Codeforces
标签:
题解
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】