Codeforces Round #684 (Div. 2)ABC1C2
Posted kanoon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #684 (Div. 2)ABC1C2相关的知识,希望对你有一定的参考价值。
比赛链接:https://codeforces.com/contest/1440
A. Buy the String
题解
枚举字符串中 (0) 或 (1) 的个数即可。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, c0, c1, h;
cin >> n >> c0 >> c1 >> h;
string s;
cin >> s;
int cnt[2] = {};
for (char c : s) ++cnt[c - ‘0‘];
int ans = INT_MAX;
for (int i = 0; i <= n; i++) {
ans = min(ans, i * c0 + (n - i) * c1 + h * abs(i - cnt[0]));
}
cout << ans << "
";
}
return 0;
}
B. Sum of Medians
题解
贪心,先把较小的数填入每个数组的前 (lceil frac{n}{2} ceil - 1) 个元素,然后依次填完每个数组即可。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<vector<int>> a(k);
for (int i = 0, j = 0; i < n * k; i++) {
int x;
cin >> x;
a[j].push_back(x);
if (a[j].size() + 1 == (n + 1) / 2) ++j;
if (a[j].size() == n) ++j;
if (j == k) j = 0;
}
long long ans = 0;
for (int i = 0; i < k; i++) {
ans += a[i][(n + 1) / 2 - 1];
}
cout << ans << "
";
}
return 0;
}
C1. Binary Table (Easy Version)
题解
依次操作每个 (2 imes 2) 方阵即可,方阵中 (1) 的个数的规律为:1 -> 2 -> 3 -> 0 。
代码
见C2。
C2. Binary Table (Hard Version)
题解
依次把所有 (1) 都挤到右下角的 (2 imes 2) 的方阵中,然后操作一下该方阵即可。
证明
除右下角的方阵外最多操作 (n imes m - 4) 次,右下角的方阵最多操作 (3) 次,所以最多操作 (n imes m - 1) 次。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
vector<string> MP(n);
for (auto &x : MP) cin >> x;
vector<pair<int, int>> v;
auto op = [&](int x, int y) {
v.emplace_back(x, y);
MP[x][y] = MP[x][y] == ‘0‘ ? ‘1‘ : ‘0‘;
};
for (int i = 0; i < n - 2; i++) {
for (int j = 0; j < m; j++) {
if (MP[i][j] == ‘1‘) {
op(i, j);
op(i + 1, j);
if (j == m - 1) op(i + 1, j - 1);
else op(i + 1, j + 1);
}
}
}
for (int j = 0; j + 2 < m; j++) {
for (int i = n - 2; i < n; i++) {
if (MP[i][j] == ‘1‘) {
op(i, j);
op(i, j + 1);
if (i == n - 2) op(i + 1, j + 1);
if (i == n - 1) op(i - 1, j + 1);
}
}
}
auto cal = [&](int x, int y) {
string s;
s += MP[x][y];
s += MP[x][y + 1];
s += MP[x + 1][y];
s += MP[x + 1][y + 1];
for (int tot_1 = count(s.begin(), s.end(), ‘1‘); tot_1 != 0; ) {
if (tot_1 == 1) {
int cnt_0 = 0;
for (int i = 0; i < 4; i++) {
if (s[i] == ‘1‘) {
v.emplace_back(x + (i >= 2), y + (i == 1 or i == 3));
s[i] = ‘0‘;
} else if (cnt_0 < 2) {
v.emplace_back(x + (i >= 2), y + (i == 1 or i == 3));
s[i] = ‘1‘;
++cnt_0;
}
}
} else if (tot_1 == 2) {
int cnt_1 = 0;
for (int i = 0; i < 4; i++) {
if (s[i] == ‘1‘) {
if (cnt_1 < 1) {
v.emplace_back(x + (i >= 2), y + (i == 1 or i == 3));
s[i] = ‘0‘;
++cnt_1;
}
} else {
v.emplace_back(x + (i >= 2), y + (i == 1 or i == 3));
s[i] = ‘1‘;
}
}
} else if (tot_1 == 3) {
for (int i = 0; i < 4; i++) {
if (s[i] == ‘1‘) {
v.emplace_back(x + (i >= 2), y + (i == 1 or i == 3));
s[i] = ‘0‘;
}
}
} else if (tot_1 == 4) {
int cnt_1 = 0;
for (int i = 0; i < 4; i++) {
if (s[i] == ‘1‘) {
v.emplace_back(x + (i >= 2), y + (i == 1 or i == 3));
s[i] = ‘0‘;
if (++cnt_1 == 3) break;
}
}
}
tot_1 = count(s.begin(), s.end(), ‘1‘);
}
for (int i = 0; i < 4; i++) {
int nx = x + (i >= 2);
int ny = y + (i == 1 or i == 3);
MP[nx][ny] = s[i];
}
};
cal(n - 2, m - 2);
cout << v.size() / 3 << "
";
int cnt = 0;
for (auto [x, y] : v) {
cout << x + 1 << ‘ ‘ << y + 1 << ‘ ‘;
if (++cnt % 3 == 0) cout << "
";
}
}
return 0;
}
以上是关于Codeforces Round #684 (Div. 2)ABC1C2的主要内容,如果未能解决你的问题,请参考以下文章
[ACM]Codeforces Round #534 (Div. 2)
Codeforces Round #726 (Div. 2) B. Bad Boy(贪心)
Codeforces Educational Codeforces Round 67
Codeforces Round 1132Educational Round 61
codeforces Technocup 2017 - Elimination Round 2/Codeforces Round #380 (Div. 2, Rated, Based on Techn