复健,时间有限题解比较简陋


A. Middle of the Contest

将小时转成分钟,得到起止时间在一天中的分钟数,取平均值即可,复杂度O(1)。平均值转换会时间的时候注意前导0。

void solve(int x) {
x /= 2;
printf("%02d:%02d\n", x / 60, x % 60);
}
int main() {
// freopen("in.txt", "r", stdin);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)cpp;
int h1, h2, m1, m2;
char c;
cin >> h1 >> c >> m1;
cin >> h2 >> c >> m2;
solve(h1 * 60 + m1 + h2 * 60 + m2);
}

B. Preparation for International Women’s Day

要加起来能被k整除, 只需要看模k的余数即可。余数为i的与余数为k-i的互补可以被k整除,通过计数看有多少对能互补。需要注意的是,为余数为0k/2(k为偶数)时,只能同余数的互补,此时计数是偶数个时都能配对,奇数个时能配对的数量是计数 - 1。复杂度O(n + k)

int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, k, x, cnt = 0;
cin >> n >> k;
int d[k] = {0};
for (int i = 0; i < n; ++i) cin >> x, d[x % k]++;
for (int i = 0; i < (k + 1) / 2; ++i) {
if (i == 0)
cnt += d[i] / 2;
else {
cnt += min(d[i], d[k - i]);
}
}
if (k % 2 == 0) cnt += d[k / 2] / 2;
cout << cnt * 2;
}

C. Balanced Team

单调队列,从小到大添加元素,保证队首和队尾差不超过5,超过了则出队,否则用当前队列大小更新最优解。如果元素x < yx一定比y先入队,而且能与x共存的最小值sx和能与y共存的最小值sysx <= sy。使用单调队列,每次入队后进行出队操作,出队完成后队首就是能与入队元素共存的最小值,队列内的元素就是以入队元素为最大值时所有能存在的元素。复杂度O(n)

int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n;
cin >> n;
ll a[n + 1];
for (int i = 1; i <= n; ++i) cin >> a[i];
sort(a + 1, a + 1 + n);
int cnt = 0, l = 1, r = 2;
while (l <= r && r <= n) {
if (a[r] - a[l] > 5) cnt = max(cnt, r - l), l++;
r++;
}
cnt = max(cnt, r - l);
cout << cnt;
}

D. Zero Quantity Maximization

d * a[i] + b[i] = 0可得d = - b[i] / a[i],统计每种d取值的个数,取最大即可。对于a[i]0的情况需要特殊讨论,如果b[i]也为0则此时d可以取任意值;否则,d的取值为0。另外,为了避免浮点误差,不能直接统计d,而是要统计<a, b>这个配对;同时,为了归一化,需要将ab同时除以他们的最大公约数,并保证a是正数。复杂度O(nlog(n)),log是因为用了map来计数。

// Author : RioTian
// Time : 20/11/10
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n;
cin >> n;
ll a[n + 1], b[n + 1];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
cin >> b[i];
}
int zeroBCnt = 0, zeroBothCnt = 0;
map<pair<int, int>, int> hash;
for (int i = 0; i < n; ++i) {
if (a[i] == 0) {
if (b[i] == 0) ++zeroBothCnt;
continue;
}
if (b[i] == 0) {
++zeroBCnt;
}
int divisor = gcd(abs(a[i]), abs(b[i]));
a[i] /= divisor;
b[i] /= divisor;
if (a[i] < 0) {
a[i] = -a[i];
b[i] = -b[i];
}
if (hash[make_pair(a[i], b[i])])
++hash[make_pair(a[i], b[i])];
else
hash[make_pair(a[i], b[i])] = 1;
}
int ans = zeroBCnt;
for (auto item : hash) {
if (item.second > ans) ans = item.second;
}
cout << ans + zeroBothCnt << endl;
return 0;
}

E. K Balanced Teams

与C题思路类似,先得到取每个元素为最大值,能共存的元素有哪些(排过序的数组保留首位指针即可),比如位置为i的元素最小可共存元素的位置是maxStart[i],这样数组里面maxStart[i]i都是可共存元素。问题就转成如何在里面选k个,让元素尽量多,这样dp即可。dp[i][j]表示前i个元素选j队的最优值,则如果选maxStart[i]i,最优值为dp[maxStart[i] - 1], j - 1] + i - maxStart[i] + 1;如果不选,最优值为dp[i - 1][j];两者取最优得到状态转移方程。另外由于j只会从j - 1转移,因此可以用滚动数组节约内存。复杂度O(nk)

// Author : RioTian
// Time : 20/11/10
#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 10;
int a[N], maxStart[N], dp[N][2];
int n, k;
int main() {
cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a, a + n);
int l = 0, r = 0;
while (r < n) {
if (a[r] - a[l] <= 5) {
maxStart[r] = l, ++r;
continue;
}
++l;
}
for (int i = 1; i <= k; ++i) {
for (int j = 0; j < n; ++j) {
if (maxStart[j]) {
dp[j][i % 2] =
max(dp[j - 1][i % 2],
dp[maxStart[j] - 1][(i - 1) % 2] + j - maxStart[j] + 1);
continue;
}
dp[j][i % 2] = max(dp[j - 1][i % 2], j - maxStart[j] + 1);
}
}
cout << dp[n - 1][k % 2] << endl;
return 0;
}

F1. Spanning Tree with Maximum Degree

直接找到度最大的节点bfs即可,复杂度O(n + m)

#include <iostream>
#include <queue>
#include <vector> using namespace std; vector<int> node[200010];
bool visited[200010]; void bfs(int start) {
queue<int> qu;
qu.push(start);
visited[start] = true;
while (qu.size()) {
int cur = qu.front();
qu.pop();
for (auto next : node[cur]) {
if (visited[next]) continue;
visited[next] = true;
qu.push(next);
cout << cur + 1 << ' ' << next + 1 << endl;
}
}
} int main() {
int n, m, x, y, tmp, maxCnt = 0, maxNode = -1;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> x >> y;
--x;
--y;
node[x].push_back(y);
node[y].push_back(x);
tmp = node[x].size() > node[y].size() ? x : y;
if (node[tmp].size() > maxCnt) {
maxCnt = node[tmp].size();
maxNode = tmp;
}
}
bfs(maxNode);
return 0;
}

F2. Spanning Tree with One Fixed Degree

如果从1的一个分支出发能从另一个分支回到1,则这些分支划分为同一组,dfs即可得到这些分组。如果一组里面所有分支都被去掉了,则这组里面的节点就无法出现在树里面,因此至少要保留一个。dfs得到有多少这样的组,每组里面取一个分支,剩下还可以取则任意取。这些分支作为bfs的第一步,继续搜下去,按搜索顺序输出即可。非法的情况有:dfs时存在节点没有走到;需要的度数比组数少(此时至少有一组所有分支都被去掉);需要的度数比1链接的分支多。复杂度O(n + m)

#include <cstring>
#include <iostream>
#include <queue>
#include <vector> using namespace std;
bool visited[200010];
vector<int> node[200010];
vector<int> group[200010];
queue<int> qu;
int n, m, d, g; void dfs(int cur, int parent) {
visited[cur] = true;
for (auto next : node[cur]) {
if (visited[next]) {
if (parent == -1) group[g - 1].push_back(next);
continue;
}
if (parent == -1) {
group[g++].push_back(next);
}
dfs(next, cur);
}
} void bfs() {
while (qu.size()) {
int cur = qu.front();
qu.pop();
for (auto next : node[cur]) {
if (!visited[next]) {
visited[next] = true;
cout << cur + 1 << ' ' << next + 1 << endl;
qu.push(next);
}
}
}
} int main() {
int x, y;
cin >> n >> m >> d;
for (int i = 0; i < m; ++i) {
cin >> x >> y;
--x;
--y;
node[x].push_back(y);
node[y].push_back(x);
}
memset(visited, 0, sizeof(visited));
dfs(0, -1);
for (int i = 0; i < n; ++i) {
if (!visited[i]) {
cout << "NO" << endl;
return 0;
}
}
if (g > d || node[0].size() < d) {
cout << "NO" << endl;
return 0;
}
cout << "YES" << endl;
memset(visited, 0, sizeof(visited));
visited[0] = true;
d -= g;
for (int i = 0; i < g; ++i) {
cout << '1' << ' ' << group[i][0] + 1 << endl;
visited[group[i][0]] = true;
qu.push(group[i][0]);
for (int j = 1; d && j < group[i].size(); ++j) {
cout << '1' << ' ' << group[i][j] + 1 << endl;
visited[group[i][j]] = true;
qu.push(group[i][j]);
--d;
}
}
bfs();
return 0;
}

Codeforces Round #544 (Div. 3)简单题解的更多相关文章

  1. Codeforces Round #544 (Div. 3) 题解

    Codeforces Round #544 (Div. 3) D. Zero Quantity Maximization 题目链接:https://codeforces.com/contest/113 ...

  2. # Codeforces Round #529(Div.3)个人题解

    Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...

  3. Codeforces Round #557 (Div. 1) 简要题解

    Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...

  4. Codeforces Round #538 (Div. 2) (A-E题解)

    Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...

  5. Codeforces Round #531 (Div. 3) ABCDEF题解

    Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...

  6. Codeforces Round #499 (Div. 1)部分题解(B,C,D)

    Codeforces Round #499 (Div. 1) 这场本来想和同学一起打\(\rm virtual\ contest\)的,结果有事耽搁了,之后又陆陆续续写了些,就综合起来发一篇题解. B ...

  7. Codeforces Round #540 (Div. 3) 部分题解

    Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...

  8. Codeforces Round #527 (Div. 3) ABCDEF题解

    Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...

  9. Codeforces Round #821(Div.2) (A-C) 题解

    Codeforces Round #821(Div.2) (A-C) A.Consecutive Sum 大致题意 给定一组共 n 个数据 ,如果俩个数的下标在 mod k 意义下同余,则可以交换a[ ...

  10. Codeforces Round #545 (Div. 1) 简要题解

    这里没有翻译 Codeforces Round #545 (Div. 1) T1 对于每行每列分别离散化,求出大于这个位置的数字的个数即可. # include <bits/stdc++.h&g ...

随机推荐

  1. 【Javaweb】Servlet十 | HttpServletResponse类和HttpServletRequest类

    HttpServletResponse类的作用 HttpServletResponse类和HttpServletRequest类一样.每次请求进来,Tomcat服务器都会创建一个Response对象传 ...

  2. 老是听到做PPT要会“内容可视化”,到底啥是内容可视化?

    在PPT中,内容可视化是指将文字.数据和概念等抽象信息转化为图像.图表.图表及其他可视化元素来呈现.通过合适的颜色.形状.大小和布局等视觉设计元素来强调信息的关键点和关系, 从而提高观众对信息的理解和 ...

  3. 数据分析工具 Datainside、Power BI、帆软(FineReport)怎么选?

    Datainside.Power BI和帆软(FineReport)都是数据分析领域常用的工具,它们各自有不同的特点和适用场景.下面将会详细介绍每个工具的功能和优势,以便您进行选择. Datainsi ...

  4. 掌握这些,轻松管理BusyBox:inittab文件的配置和作用解析

    BusyBox 是一个轻量级的开源工具箱,其中包含了许多标准的 Unix 工具,例如 sh.ls.cp.sed.awk.grep 等,同时它也支持大多数关键的系统功能,例如自启动.进程管理.启动脚本等 ...

  5. Protobuf的使用,结合idea

    安装Protobuf并配置idea Protocol Buffers(又名 protobuf)是 Google 的中立语言, 平台中立.可扩展的结构化数据序列化机制. 官网: https://gith ...

  6. Docker的安装、镜像加速配置

    wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce. ...

  7. setup的执行时机

    setup是在beforeCreate之前执行的,也就是vue实例还未被创建,因为setup中并没有this指针 <script> export default { setup() { c ...

  8. 2024年 Kubernetes 四大趋势预测

    Kubernetes 在生产环境中的复杂性已经成为常态,在2023年这个平台工程盛行的时代,容器管理的最大亮点可能在于其灵活性,然而在运维政策和治理等方面仍然存在诸多挑战.Kubernetes 最大的 ...

  9. nginx-下载安装与配置

    nginx下载 从官网下载,使用命令在linux下载即可,这个是目前稳定版最新的1.24.0版本,如果想要用旧版本直接修改版本号即可(旧版本我用的是1.12.2) 下载需要使用wget命令,默认是没有 ...

  10. Redis 事务管理

    Redis 提供了五个指令用于处理事务:MULTI.EXEC.DISCARD.WATCH.UNWATCH,这五个命令是 Redis 进行事务处理的基础. 这些指令允许一组命令在一个步骤中执行,其中有两 ...