比赛链接: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. Thread线程控制之sleep、join、setDaemon方法的用处

    Thread线程控制之sleep.join.setDaemon方法的用处 1. sleep方法 public static void sleep(long millis) throws Interru ...

  2. day122:MoFang:OSSRS流媒体直播服务器&基于APICloud的acLive直播推流模块实现RTMP直播推流

    目录 1.docker安装OSSRS流媒体直播服务器 2.基于APICloud的acLive直播推流模块实现RTMP直播推流 3.直播流管理 1.docker安装OSSRS流媒体直播服务器 1.OSS ...

  3. GMT UTC CST ISO 夏令时 时间戳,都是些什么鬼?

    目录 ✍前言 本文提纲 版本约定 ✍正文 GMT:格林威治时间 凭什么格林威治作为标准时间? 地球自转 中国有哪几个时区? 美国有哪几个时区? GMT和Http协议的渊源 UTC:世界标准时间 UTC ...

  4. 【ORA】Specified value of MEMORY_TARGET is too small, needs to be at least 3072M解决办法

    今天安装EM12C的时候遇到了一个报错: 修改好数据库中的参数大小后,重新启动报错 Specified value of MEMORY_TARGET is too small, needs to be ...

  5. ftp设置二进制上传

    一个不重要的数据库,备份是用expdp导出,然后上传到ftp服务器上面.上周这个主机宕机了,要在别的数据库恢复,发现报如下错误: ORA-39001: invalid argument value O ...

  6. dblink查找对应的目标端session

    v$session试图中process字段代表的是客户端所在机器的进程号 例如我使用toad连接数据库,查询到的process即toad的进程号 SELECT process FROM V$SESSI ...

  7. 动态sql语句、逆向工程(generator)、分页助手(pagehelper)

    1.动态sql语句 if if where 配合使用 <select id="selectByWhere" resultType="com.alibaba.wlq. ...

  8. 图解 | 原来这就是TCP

    你是一台电脑,你的名字叫 A 经过<图解 | 原来这就是网络>这篇文章中的一番折腾,只要你知道另一位伙伴 B 的 IP 地址,且你们之间的网络是通的,无论多远,你都可以将一个数据包发送给你 ...

  9. 前端知识(一)03 初识 ECMAScript 6-谷粒学院

    目录 一.ECMAScript 6 1.什么是 ECMAScript 6 2.ECMAScript 和 JavaScript 的关系 二.基本语法 1.let声明变量 2.const声明常量(只读变量 ...

  10. day131:2RenMJ:2RenMJ游戏简介&部署MJ项目到本地

    目录 1.游戏简介 1.如何做出一款麻将游戏? 2.麻将运行界面 3.麻将项目所用技术快速概览 4.web开发 / 游戏开发 / APP开发 比较 5.firefly游戏框架介绍 2.部署麻将项目到本 ...