Codeforces Global Round 11【ABCD】
比赛链接:https://codeforces.com/contest/1427
A. Avoiding Zero
题意
将 \(n\) 个数重新排列使得不存在为 \(0\) 的前缀和。
题解
计算正、负前缀和,如果二者和为 \(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;
vector<int> a(n);
int pos = 0, neg = 0;
for (auto &x : a) {
cin >> x;
(x < 0 ? neg : pos) += x;
}
if (pos + neg == 0) {
cout << "NO" << "\n";
continue;
}
if (pos + neg > 0) {
sort(a.begin(), a.end(), greater<>());
} else {
sort(a.begin(), a.end(), less<>());
}
cout << "YES" << "\n";
for (int i = 0; i < n; i++) {
cout << a[i] << " \n"[i == n - 1];
}
}
return 0;
}
B. Chess Cheater
题意
给出一个长为 \(n\) 由 W, L 组成的字符串,如果一个 W 左侧为 W,则它提供 2 分,否则为 1 分。最多可以将 \(k\) 个 L 变为 W,问字符串可以得到的最大分值。
题解
本题的关键是字符串中 W 的有无及两两构成的封闭区间长度。
- 如果全为 L,则字符串的最大分值为 \(max(2k-1,\ 0)\) 。
- 如果存在 W,则每次操作都会至少增加 2 分,如果操作的为两个 W 区间内的最后一个 L,则会额外再增加 1 分。
所以计算字符串的初始分值,加上 \(2 \times min(k,\ cntL)\) 分,然后区间长度排序,每当可以减去一个完整区间长就再加上 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;
string s;
cin >> s;
int cntL = count(s.begin(), s.end(), 'L');
if (cntL == n) {
cout << max(2 * k - 1, 0) << "\n";
continue;
}
vector<int> posW;
for (int i = 0; i < n; i++)
if (s[i] == 'W') posW.push_back(i);
vector<int> seg;
for (int i = 1; i < int(posW.size()); i++)
if (posW[i] - posW[i - 1] - 1 > 0) seg.push_back(posW[i] - posW[i - 1] - 1);
int ans = s[0] == 'W';
for (int i = 1; i < n; i++)
if (s[i] == 'W') ans += s[i - 1] == 'W' ? 2 : 1;
ans += 2 * min(k, cntL);
sort(seg.begin(), seg.end());
for (auto len : seg) if (k >= len) k -= len, ans += 1;
cout << ans << "\n";
}
return 0;
}
C. The Hard Work of Paparazzi
题意
\(r\) 行与 \(r\) 列相交形成了 \(r \times r\) 个点,初始时刻记者位于左下角的 \((1,1)\) 处,接下来给出 \(n\) 个名人的出现时间和位置,出现时间严格递增,问记者最多可以拍到多少名人的照片。

题解
This is a classical dynamic-programming task with a twist.
这是一个有些变化的经典动态规划问题。
与最长上升子序列问题的不同之处的是,本题判断条件由 \(a_j > a_i\) 变为了 \(dis_{ij} \le t_j - t_i\) 以及利用 \(r\) 将 \(O_{(n^2)}\) 优化至了 \(O_{(nr)}\) 。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int r, n;
cin >> r >> n;
//如果 r == 1,此时只有 1 个点
if (r == 1) {
cout << n << "\n";
return 0;
}
vector<int> t(n + 1), x(n + 1), y(n + 1);
t[0] = 0, x[0] = 1, y[0] = 1;
for (int i = 1; i <= n; i++)
cin >> t[i] >> x[i] >> y[i];
vector<int> dp(n + 1, -1e9), mx_dp(n + 1);
dp[0] = 0; //初始时只有时刻 0 的 (1,1) 可达
for (int i = 1; i <= n; i++) {
//继承之前可达的 dp 状态
for (int j = max(i - 2 * (r - 1), 0); j < i; j++) {
if (abs(x[i] - x[j]) + abs(y[i] - y[j]) <= t[i] - t[j])
dp[i] = max(dp[i], dp[j] + 1);
}
//如果 i 大于等于最长路径,那么对于 dp[0] ~ dp[i - 2 * (r - 1)] 一定是可达的
if (i >= 2 * (r - 1)) dp[i] = max(dp[i], mx_dp[i - 2 * (r - 1)] + 1);
mx_dp[i] = max(dp[i], mx_dp[i - 1]);
}
cout << mx_dp[n] << "\n";
return 0;
}
D. Unshuffling a Deck
题意
给出一个大小为 \(n\) 的排列,每次操作可以将 \(n\) 个数分为 \(1 \sim n\) 个非空连续份,然后将对称的份两两交换,试给出在 \(n\) 次操作内将排列排为升序的操作过程。
题解
- 找到值相差为 \(1\) 的逆序对:\(i<j\),\(a_i = a_j + 1\)
- 将已为升序的数视为一个整体,找到 \(t\) 满足:\(i \le t < j\),\(a_t > a_{t+1}\)
- 分为 \(4\) 份,\(D_1=[a_1,a_2,\dots,a_{i-1}],\ D_2=[a_i,a_{i+1},\dots, a_t],\ D_3=[a_{t+1},a_{t+2},\dots, a_j],\ D_4=[a_{j+1},a_{j+2},\dots, a_n]\)
- 将对称组交换,转至步骤 \(1\) 。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n), pos(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
--a[i];
}
vector<vector<int>> ans;
while (not is_sorted(a.begin(), a.end())) {
for (int i = 0; i < n; i++) {
pos[a[i]] = i;
}
//1
for (int i = 1; i < n; i++) {
if (pos[i] < pos[i - 1]) {
//2
int l = pos[i];
int r = pos[i - 1];
int mid = l;
while (a[mid + 1] == a[mid] + 1) ++mid;
//3
ans.push_back({l, mid - l + 1, r - mid, n - r - 1});
//4
vector<int> b;
for (int i = r + 1; i < n; i++) b.push_back(a[i]);
for (int i = mid + 1; i < r + 1; i++) b.push_back(a[i]);
for (int i = l; i < mid + 1; i++) b.push_back(a[i]);
for (int i = 0; i < l; i++) b.push_back(a[i]);
a.swap(b);
break;
}
}
}
cout << ans.size() << "\n";
for (auto &v : ans) {
//每份非空
while (v.back() == 0) v.pop_back();
while (v.front() == 0) v.erase(v.begin());
cout << v.size() << "\n";
for (int i = 0; i < int(v.size()); i++) {
cout << v[i] << " \n"[i == int(v.size()) - 1];
}
}
return 0;
}
Codeforces Global Round 11【ABCD】的更多相关文章
- Codeforces Global Round 11 个人题解(B题)
Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...
- Codeforces Global Round 11 A~D题解
A.Avoiding Zero 题目链接:https://codeforces.ml/contest/1427 题目大意:给定一个数组a1,a2...,an,要求找出一个a重排后的数组b1,b2,.. ...
- Codeforces Global Round 11 D. Unshuffling a Deck(构造/相邻逆序对)
题目链接:https://codeforces.com/contest/1427/problem/D 题意 给出一个大小为 \(n\) 的排列,每次操作可以将 \(n\) 个数分为 \(1 \sim ...
- Codeforces Global Round 11 C. The Hard Work of Paparazzi(dp/最长上升子序列)
题目链接:https://codeforces.com/contest/1427/problem/C 题意 \(r\) 行与 \(r\) 列相交形成了 \(r \times r\) 个点,初始时刻记者 ...
- Codeforces Global Round 11 B. Chess Cheater(贪心)
题目链接:https://codeforces.com/contest/1427/problem/B 题意 给出一个长为 \(n\) 由 W, L 组成的字符串,如果一个 W 左侧为 W,则它提供 2 ...
- Codeforces Global Round 11 A. Avoiding Zero(前缀和)
题目链接:https://codeforces.com/contest/1427/problem/A 题意 将 \(n\) 个数重新排列使得不存在为 \(0\) 的前缀和. 题解 计算正.负前缀和,如 ...
- Codeforces Global Round 11 C. The Hard Work of Paparazzi (DP)
题意:有\(r\)X\(r\)的网格图,有\(n\)位名人,会在\(t_i\)时出现在\((x_i,y_i)\),如果过了\(t_i\)名人就会消失,从某一点走到另外一点需要花费的时间是它们之间的曼哈 ...
- Codeforces Global Round 11 B. Chess Cheater (贪心,结构体排序)
题意:你和朋友进行了\(n\)个回合的棋艺切磋,没有平局,每次要么输要么赢,每次赢可以得一分,假如前一局也赢了,那么可以得两分,结果已成定局,但是你确可以作弊,最多修改\(k\)个回合的结果,问你作弊 ...
- 【手抖康复训练1 】Codeforces Global Round 6
[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...
随机推荐
- (开源项目)abattoir unity游戏
(开源项目)abattoir unity游戏 欢迎各位的改进和提议! 名称: abattoir(角斗场) 版本: v1.0 作者: N-n-N(笔者) 简介: 添加娱乐(冲撞)模式和普通(一般)模式 ...
- uni-app阻止事件向父级冒泡
在官网找到的就只有这个方法,但是我放在app项目里并不支持,所以就想到vue的阻止事件冒泡的方法,现在分享,免得大家踩坑 <view class="parent" @ ...
- [C#.NET 拾遗补漏]14:使用结构体实现共用体
在 C 和 C# 编程语言中,结构体(Struct)是值类型数据结构,它使得一个单一变量可以存储多种类型的相关数据.在 C 语言中还有一种和结构体非常类似的语法,叫共用体(Union),有时也被直译为 ...
- LeetCode225 用队列实现栈
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...
- 【Oracle】下载11.2.0.4的地址
https://updates.oracle.com/download/13390677.html 这个地址就是下载Oracle 11.2.0.4版本的地址,需要有metalink账号才可以下载
- 【RAC】运行root.sh的时候报错root.sh Oracle CRS stack is already configured and will be running under init(1M)
环境:oracle10g 系统:CentOS6.4 开始的时候,在节点1上运行root.sh发现出现90s 的时候hang住了,结束掉,结局完事后,再次运行root.sh报错 WARNING: dir ...
- 【ORA】ORA-27090: Unable to reserve kernel resources for asynchronous disk I/O
操作系统是CentOS 5.11 数据库 10.2.0.5.0 晚上查看数据库,发现数据库报错查看相关的trace文件内容如下: *** SERVICE NAME:(SYS$BACKGROUND) 2 ...
- Linux echo和cat和grep和tr的基础用法
Linux vim 搜索 echo : 显示输出功能 echo oldboy>1.txtx cat 1.txtx > 重定向 文件内容覆盖 >> 追加重定向 ...
- 算法模板 - C++ 高精度运算
C++算法板子 高精度 高精度推荐用python来写,python有大整数,这里写的是关于C++的高精度运算模板 1.高精 * 低精 #include <iostream> #includ ...
- Poj-P1088题解【动态规划/记忆化搜索】
本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: http://poj.org/problem?id=1088 题目描述: 区域由一个二维数组给 ...