标签:贪心 cal 字符 pac round 题解 == codeforce inline
比赛链接:https://codeforces.com/contest/1440
枚举字符串中 \(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 << "\n";
}
return 0;
}
贪心,先把较小的数填入每个数组的前 \(\lceil \frac{n}{2} \rceil - 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 << "\n";
}
return 0;
}
依次操作每个 \(2 \times 2\) 方阵即可,方阵中 \(1\) 的个数的规律为:1 -> 2 -> 3 -> 0 。
见C2。
依次把所有 \(1\) 都挤到右下角的 \(2 \times 2\) 的方阵中,然后操作一下该方阵即可。
除右下角的方阵外最多操作 \(n \times m - 4\) 次,右下角的方阵最多操作 \(3\) 次,所以最多操作 \(n \times 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 << "\n";
int cnt = 0;
for (auto [x, y] : v) {
cout << x + 1 << ‘ ‘ << y + 1 << ‘ ‘;
if (++cnt % 3 == 0) cout << "\n";
}
}
return 0;
}
Codeforces Round #684 (Div. 2)【ABC1C2】
标签:贪心 cal 字符 pac round 题解 == codeforce inline
原文地址:https://www.cnblogs.com/Kanoon/p/13997583.html