# A. Buy the String

## 代码

#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;
}


# B. Sum of Medians

## 代码

#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;
}


# C2. Binary Table (Hard Version)

## 代码

#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】

(0)
(0)

© 2014 mamicode.com 版权所有 京ICP备13008772号-2