比赛链接: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 << "\n";
}
return 0;
}

B. Sum of Medians

题解

贪心,先把较小的数填入每个数组的前 \(\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;
}

C1. Binary Table (Easy Version)

题解

依次操作每个 \(2 \times 2\) 方阵即可,方阵中 \(1\) 的个数的规律为:4 -> 1 -> 2 -> 3 -> 0 。

代码

见C2。

C2. Binary Table (Hard Version)

题解

依次把所有 \(1\) 都挤到右下角的 \(2 \times 2\) 的方阵中,然后操作一下该方阵即可。

证明

除右下角的方阵外最多操作 \(n \times m - 4\) 次,右下角的方阵最多操作 \(4\) 次,所以最多操作 \(n \times m\) 次。

代码

#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】的更多相关文章

  1. Codeforces Round #677 (Div. 3)【ABCDE】

    比赛链接:https://codeforces.com/contest/1433 A. Boring Apartments 题解 模拟即可. 代码 #include <bits/stdc++.h ...

  2. Codeforces Round #382 Div. 2【数论】

    C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...

  3. Codeforces Round #682 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1438 A. Specific Tastes of Andre 题意 构造一个任意连续子数组元素之和为子数组长度倍数的数组. ...

  4. Codeforces Round #678 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1436 A. Reorder 题解 模拟一下这个二重循环发现每个位置数最终都只加了一次. 代码 #include <bi ...

  5. Codeforces Round #676 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1421 A. XORwice 题意 给出两个正整数 \(a.b\),计算 \((a \oplus x) + (b \oplus ...

  6. Codeforces Round #675 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1422 A. Fence 题意 给出三条边 $a,b,c$,构造第四条边使得四者可以围成一个四边形. 题解 $d = max( ...

  7. Codeforces Round #668 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1405 A. Permutation Forgery 题意 给出一个大小为 $n$ 的排列 $p$,定义 \begin{equ ...

  8. Codeforces Round #658 (Div. 2)【ABC2】

    做完前四题还有一个半小时... 比赛链接:https://codeforces.com/contest/1382 A. Common Subsequence 题意 给出两个数组,找出二者最短的公共子序 ...

  9. Codeforces Round #732 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1546 A. AquaMoon and Two Arrays 题意 给出两个大小为 \(n\) 的数组 \(a, b\) ,每 ...

随机推荐

  1. 【Java基础】多线程

    多线程 基本概念 程序(program)是为完成特定任务.用某种语言编写的一组指令的集合.即指一段静态的代码,静态对象. 进程(process)是程序的一次执行过程,或是正在运行的一个程序.是一个动态 ...

  2. 按装parallels tool的失败之路

    这是一篇对于其他人来说没什么意义的博客.单纯的可以被看作是日记. 首先,我想安装parallels tool. 但是照着网上很多教程(如www.cnblogs.com/artwalker/p/1323 ...

  3. Python找对称数——纪念第一次自主编写代码

    2021-01-17 题目: [问题描述]已知10个四位数输出所有对称数及个数 n,例如1221.2332都是对称数[输入形式]10个四位数,以空格分隔开[输出形式]输入的四位数中的所有对称数,对称数 ...

  4. xtrabackup不完全恢复

    例如,在2014年6月26日下午14:00的时候有人误操作drop掉了一张表,由于库不是很大,并且为测试库,并没有访问,这个时候,我们可以进行基于位置和时间点的不完全恢复 先找到早上的备份,查看那xt ...

  5. 【ORA】ORA-27125:unable to create shared memory segment

    在安装Oracle 10g的时候出现一个了错误,在网上总结了一下大牛写的文章 ORA-27125:unable to create shared memory segment 安装时出现这个错误安装会 ...

  6. 【Oracle】11g direct path read介绍:10949 event、_small_table_threshold与_serial_direct_read

    转自刘相兵老师的博文: http://www.askmaclean.com/archives/11g-direct-path-read-10949-_small_table_threshold-_se ...

  7. 算法模板 - C++ 高精度运算

    C++算法板子 高精度 高精度推荐用python来写,python有大整数,这里写的是关于C++的高精度运算模板 1.高精 * 低精 #include <iostream> #includ ...

  8. mysqldumpslow基本使用

    参数解释 -s, 是表示按照何种方式排序 c: 访问计数 l: 锁定时间 r: 返回记录 t: 查询时间 al:平均锁定时间 ar:平均返回记录数 at:平均查询时间 -t, 是top n的意思,即为 ...

  9. Code Review 的几个技巧

    No magic: Explicit not implicit: 覆盖度比深度重要,覆盖度追求100%: 频率比仪式感重要,坐公交蹲厕所打开手机都可以 Review 别人代码,不需要专门组织会议: 粒 ...

  10. moco框架加入cookies

    一.带cookie信息的get请求 注意:cookie是放在request里的,一般登录的场景这些会用到 1.代码 2.接口管理工具添加 注意:cooike的域和路径都要添加 二.带cookie信息的 ...