比赛链接:https://codeforces.com/contest/1405

A. Permutation Forgery

题意

给出一个大小为 $n$ 的排列 $p$,定义

\begin{equation} F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]) 。\nonumber \end{equation}

试找出一个不同于 $p$ 的 $p'$ 满足 $F(p') = F(p)$ 。

题解

反转 $p$ 即可。

代码

#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;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
reverse(a.begin(), a.end());
for (int i = 0; i < a.size(); i++)
cout << a[i] << " \n"[i == a.size() - 1];
}
return 0;
}

B. Array Cancellation

题意

给出一个大小为 $n$,满足 $\sum_{i=0}^{n-1} a_i = 0$ 的数组 $a$,每次可选择的操作如下:

  • 选择 $i < j$,将 $a_i$ 减一,$a_j$ 加一,花费为 $0$
  • 选择 $i > j$,将 $a_i$ 减一,$a_j$ 加一,花费为 $1$

问将 $a_i$ 均变为 $0$ 的最小花费是多少。

题解一

分别存储正数和负数的位置,然后贪心加双指针模拟。

代码

#include <bits/stdc++.h.>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
vector<int> posi, nega;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] > 0) posi.push_back(i);
if (a[i] < 0) nega.push_back(i);
}
int j = 0;
for (int i = 0; i < int(posi.size()); i++) {
while (a[posi[i]] > 0) {
while (j < int(nega.size()) and nega[j] < posi[i])
++j;
if (j == int(nega.size())) break;
int mi = min(a[posi[i]], -a[nega[j]]);
a[posi[i]] -= mi;
a[nega[j]] += mi;
if (a[nega[j]] == 0) ++j;
}
}
long long ans = 0;
for (int i = 0; i < n; i++)
if (a[i] > 0) ans += a[i];
cout << ans << "\n";
}
return 0;
}

题解二

最小负前缀的绝对值即为最少花费。

证明

对于最小负前缀,内部操作不会影响该负前缀的总和,涉及外部操作的最佳方案就是对负前缀加一,因为 $\sum_{i=0}^{n-1} a_i = 0$,所以此时后缀为正且绝对值与该负前缀相等,将二者各视为一个整体操作即可。

代码

#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;
cin >> n;
long long ans = 0, cur = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
cur += x;
ans = min(ans, cur);
}
cout << -ans << "\n";
}
return 0;
}

C. Balanced Bitstring

题意

如果一个偶数长 $k$ 的二进制串有 $\frac{k}{2}$ 个 $0$ 和 $1$,那么称这个二进制串为 $k$ 平衡串。给出一个由 $0,1,?$ 组成的字符串 $s$,$s$ 中的 $?$ 可被替换为 $0$ 或 $1$,判断 $s$ 是否可能为 $k$ 平衡串。

题解

因为共享中间的 $k-1$ 个字符,所以 $s_i = s_{i+k}$ 。

所以如果第一个 $k$ 长子串确定合法,则整个字符串确定合法。

另外还需枚举 $k$ 个起点并判断一开始的字符串中步长为 $k$ 的路径上的所有字符是否相同。

代码

#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;
string s;
cin >> s;
bool ok = true;
int zero = 0, one = 0;
for (int i = 0; i < k; i++) {
bool a0 = false, a1 = false;
for (int j = i; j < n; j += k)
if (s[j] != '?')
(s[j] == '0' ? a0 : a1) = true;
if (a0 and a1)
ok = false;
else if (a0 or a1)
++(a0 ? zero : one);
}
if (max(zero, one) > k / 2)
ok = false;
cout << (ok ? "YES" : "NO") << "\n";
}
return 0;
}

D. Tree Tag

题意

给出一颗树,Alice在结点 $a$,每次可以走的距离最多为 $da$,Bob在结点 $b$,每次可以走的距离最多为 $db$,Alice先走,问在无限步内Alice能否走到Bob的位置。

题解

Alice获胜的三种情况:

  • 一开始Bob在Alice的覆盖半径内,因为Alice先手,所以一步就可以走到Bob的位置
  • Alice的覆盖直径大于等于树的直径,此时Alice可以移到树的中心并覆盖树上的每一个点
  • Alice的覆盖直径大于等于Bob的步长,此时以Alice的起点为根节点,Bob无法超出Alice的覆盖范围移到另一颗子树

代码

#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, a, b, da, db;
cin >> n >> a >> b >> da >> db;
--a, --b;
vector<vector<int>> G(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
--u, --v;
G[u].push_back(v);
G[v].push_back(u);
}
int diam = 0;
vector<int> dep(n);
function<int(int, int)> dfs = [&](int u, int p) {
int len = 0;
for (auto v : G[u]) {
if (v != p) {
dep[v] = dep[u] + 1;
int cur = 1 + dfs(v, u);
diam = max(diam, cur + len);
len = max(len, cur);
}
}
return len;
};
dfs(a, -1);
cout << (2 * da >= min(diam, db) or dep[b] <= da ? "Alice" : "Bob") << "\n";
}
return 0;
}

Codeforces Round #668 (Div. 2)【ABCD】的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. Codeforces Round #684 (Div. 2)【ABC1C2】

    比赛链接:https://codeforces.com/contest/1440 A. Buy the String 题解 枚举字符串中 \(0\) 或 \(1\) 的个数即可. 代码 #includ ...

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

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

随机推荐

  1. 【剑指 Offer】07.重建二叉树

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字. 示例: 前序遍历 preorder = [3,9,20,15,7] 中序遍历 ...

  2. 【Spring】Spring 入门

    Spring 入门 文章源码 Spring 概述 Spring Spring 是分层的 Java SE/EE 应用全栈式轻量级开源框架,以 IOC(Inverse Of Control,反转控制)和 ...

  3. 剑指offer 面试题10.1:青蛙跳台阶

    题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 编程思想 对于本题,前提只有 一次 1阶或者2阶的跳法.a.如果两种跳 ...

  4. Centos 7 杂章

    CentOS-7-x86_64-DVD-2003.iso 下载地址: http://mirrors.aliyun.com/centos/7/isos/x86_64/CentOS-7-x86_64-DV ...

  5. 克隆slave

    在日常生活中,我们做的比较多的操作就是在线添加从库,比如线上有一主一丛两个数据库,由于业务的需要一台从库的读取量无法满足现在的需求,这样就需要我们在线添加从库,出于安全考虑,我们通常需要在从库上进行在 ...

  6. 两万字长文总结,梳理 Java 入门进阶那些事

    大家好,我是程序员小跃,一名在职场已经写了6年程序的老程序员,从一开始的菊厂 Android 开发到现在某游戏公司的Java后端架构,对Java还是相对了解的挺多. 大概是半年前吧,在知乎上有个知友私 ...

  7. 【MySQL】centos6中/etc/init.d/下没有mysqld启动文件,怎么办

    如果/etc/init.d/下面没有mysqld的话,service mysqld start也是不好使的,同样,chkconfig mysqld on也是不能用 解决办法: 将mysql的mysql ...

  8. Azure Terraform(六)Common Module

    一,引言 之前我们在使用 Terraform 构筑一下 Azure 云资源的时候,直接将所以需要创建的资源全面写在 main.tf 这个文件中,这样写主要是为了演示使用,但是在实际的 Terrafor ...

  9. scrapy框架基于管道的持久化存储

    scrapy框架的使用 基于管道的持久化存储的编码流程 在爬虫文件中数据解析 将解析到的数据封装到一个叫做Item类型的对象 将item类型的对象提交给管道 管道负责调用process_item的方法 ...

  10. codeup 1934 查找元素

    题目描述: 输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数组中则输出-1. 输入: 测试数据有多组,输入n(1<=n<=200), ...