Codeforces Round #644 (Div. 3)
比赛链接:https://codeforces.com/contest/1360
A - Minimal Square
题意
计算能包含两个 $a \times b$ 矩形的最小正方形的面积。
题解
将两个矩形的较短边拼在一起即可。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int a, b; cin >> a >> b;
if (a > b) swap(a, b);
int mx = max(a + a, b);
cout << mx * mx << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}
B - Honest Coach
题意
将 $n$ 个数分为两组,使得一组的最大值与另一组的最小值相差最小。
题解
将 $n$ 个数排序后从相差最小的两个数间分开即可。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int n; cin >> n;
int a[n] = {};
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n);
int mi = INT_MAX;
for (int i = 1; i < n; i++)
mi = min(mi, a[i] - a[i - 1]);
cout << mi << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}
C - Similar Pairs
题意
将偶数个数两两配对,要求配对的数具有相同的奇偶性或相差为 $1$ 。
题解
如果奇偶数均有偶数个则它们内部两两配对即可,否则找出一对相差为 $1$ 奇偶数配对,余下的奇偶数个数又均为偶数。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int n; cin >> n;
vector<int> odd;
map<int, int> even;
for (int i = 0; i < n; i++) {
int x; cin >> x;
if (x % 2) odd.push_back(x);
else even[x]++;
}
bool flag = false;
if (odd.size() % 2) {
for (auto x : odd)
if (even[x - 1] or even[x + 1])
flag = true;
} else flag = true;
cout << (flag ? "YES" : "NO") << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}
D - Buying Shovels
题意
要买刚好 $n$ 个铲子,有 $k$ 种包装的铲子,每种包装内的铲子数量分别为 $1{\sim}k$,只能挑一种包装购买,计算同一包装最少需要买多少个。
题解
在 $n$ 的因子中找小于等于 $k$ 的最大因子,$n$ 除以该最大因子即为最少个数。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int n, k; cin >> n >> k;
int ans = n;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (i <= k) ans = min(ans, n / i);
if (n / i <= k) ans = min(ans, i);
}
}
cout << ans << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}
E - Polygon
题意
$n \times n$ 网格的最上边一排和最左边一列外各有 $n$ 门大炮,大炮可向网格内发射 $1$,每次发射的 $1$ 遇到边界或 $1$ 停下来,判断是否存在某种发射顺序可以得到给出的网格。
题解
因为大炮的位置在左和上,所以每个非右下边界的 $1$ 右或下方一定有一个 $1$ 。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int n; cin >> n;
char MP[n][n] = {};
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> MP[i][j];
bool flag = true;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - 1; j++)
if (MP[i][j] == '1' and MP[i + 1][j] != '1' and MP[i][j + 1] != '1')
flag = false;
cout << (flag ? "YES" : "NO") << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}
F - Spy-string
题意
给出 $n$ 个字符串,找出一个字符串,要求该字符串与每个字符串相差最多一个字母。
题解
记录每个字符串的衍生字符串,被衍生了 $n$ 次的字符串即为答案。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int n, m; cin >> n >> m;
map<string, int> mp;
for (int i = 0; i < n; i++) {
string s; cin >> s;
set<string> st;
for (int j = 0; j < m; j++) {
char t = s[j];
for (int k = 0; k < 26; k++) {
if (t - k >= 'a') {
s[j] = t - k;
st.insert(s);
}
if (t + k <= 'z') {
s[j] = t + k;
st.insert(s);
}
}
s[j] = t;
}
for (auto i : st) ++mp[i];
}
for (auto i : mp) {
if (i.second == n) {
cout << i.first << "\n";
return;
}
}
cout << -1 << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}
G - A/B Matrix
题意
构造一个 $n \times m$ 的 $01$ 矩阵,使得每行有 $a$ 个 $1$,每列有 $b$ 个 $1$ 。
题解
所有行的 $1$ 的个数:$n \times a$
所有列的 $1$ 的个数:$m \times b$
如果存在满足要求的 $01$ 矩阵,二者必须相等,之后按行或列贪心构造即可。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int n, m, a, b; cin >> n >> m >> a >> b;
if (n * a != m * b) {
cout << "NO" << "\n";
return;
}
bool MP[n][m] = {};
int col = 0;
for (int row = 0; row < n; row++) {
for (int i = 0; i < a; i++) {
MP[row][col] = '1';
if (++col == m) col = 0;
}
}
cout << "YES" << "\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << MP[i][j];
cout << "\n";
}
} int main() {
int t; cin >> t;
while (t--) solve();
}
H - Binary Median
题意
$0 \sim 2^m - 1$ 中去掉 $n$ 个数后第 $\lfloor \frac{k - 1}{2} \rfloor$ 个数是多少(即余下的 $k$ 个数的中位数)。
题解
对于中位数来说,如果去掉的数的值小于等于它自身,那么中位数的值会向右偏移一,最小的中位数即为刚开始去掉 $n$ 个最大的数,从小到大统计是否有值小于等于中位数,同时更新中位数的值即可。
代码
#include <bits/stdc++.h>
using ll = long long;
using namespace std; void solve() {
int n, m; cin >> n >> m;
ll ans = ((1LL << m) - n - 1) / 2;
ll a[n] = {};
for (int i = 0; i < n; i++) {
string s; cin >> s;
a[i] = bitset<64>(s).to_ullong();
}
sort(a, a + n);
for (auto i : a) ans += (i <= ans);
cout << bitset<64>(ans).to_string().substr(64 - m) << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}
Codeforces Round #644 (Div. 3)的更多相关文章
- [每日一题2020.06.11]Codeforces Round #644 (Div. 3) H
A-E见 : 这里 题目 我觉得很有必要把H拿出来单独发( 其实是今天懒得写题了 ) problem H 一个从 1 到 $ 2^m - 1$ 的长度为m的连续二进制序列, 删去指定的n个数, 问剩余 ...
- [每日一题2020.06.10]Codeforces Round #644 (Div. 3) ABCDEFG
花了5个多少小时总算把div3打通一次( 题目链接 problem A 题意 : 两个x*y的矩形不能重叠摆放, 要放进一个正方形正方形边长最小为多少 先求n = min(2x, 2y, x+y) 再 ...
- Codeforces Round #644 (Div. 3) D. Buying Shovels (数学)
题意:商店里有\(k\)个包裹,第\(i\)个包裹中含有\(i\)个物品,现在想要买\(n\)物品,你可以选择某一个包裹购买任意次,使得物品数刚好等于\(n\),求最少的购买次数. 题解:首先,假如\ ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
随机推荐
- Python模块化编程与装饰器
Python的模块化编程 我们首先以一个例子来介绍模块化编程的应用场景,有这样一个名为requirements.py的python3文件,其中两个函数的作用是分别以不同的顺序来打印一个字符串: # r ...
- window安装nvm
先说一下背景,最近做的两个项目一个是祖传angularjs1.X版本另一个是react hooks结合tailwindcss,前者angularjs的node版本比较低,而tailwindcss的no ...
- php压缩文件夹并下载到本地
/** * @param $path 要压缩的文件夹路径 * @param $filename 要生成的压缩包名称 */ public function create_zip($path,$filen ...
- SpringBoot 好“吃”的启动原理
原创:西狩 编写日期 / 修订日期:2020-12-30 / 2020-12-30 版权声明:本文为博主原创文章,遵循 CC BY-SA-4.0 版权协议,转载请附上原文出处链接和本声明. 不正经的前 ...
- vue-cli3x4x修改本地端口port
一.推荐方法 "scripts": { "serve": "vue-cli-service serve --port 3000", &quo ...
- CentOS7,非LVM根分区扩容步骤:
1.查看现有的分区大小 非LVM分区,目前磁盘大小为40G,根分区总容量为40G,(是自定义分区安装的) 2.关机增加磁盘大小至100G 如果你们是vmwaer虚拟软件安装的那如下入扩容: 3.查看磁 ...
- 将连续增长 N 次字符串所需的内存重分配次数从必定 N 次降低为最多 N 次 二进制安全
SDS 与 C 字符串的区别 - Redis 设计与实现 http://redisbook.com/preview/sds/different_between_sds_and_c_string.htm ...
- int ping = 11; 限流 客户端验证与服务端是连接的
int ping = 11; ZooKeeper Programmer's Guide https://zookeeper.apache.org/doc/r3.1.2/zookeeperProgram ...
- Centos 7 Redis 安装并开机启动
一.下载并编译 cd /usr/local/bin目录可以自己选,我将redis安装到/usr/local/bin目录下.wget http://download.redis.io/releases/ ...
- Understanding go.sum and go.mod file in Go
https://golangbyexample.com/go-mod-sum-module/ Understanding go.sum and go.mod file in Go (Golang) – ...