比赛链接: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. 找到值相差为 \(1\) 的逆序对:\(i<j\),\(a_i = a_j + 1\)
  2. 将已为升序的数视为一个整体,找到 \(t\) 满足:\(i \le t < j\),\(a_t > a_{t+1}\)
  3. 分为 \(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]\)
  4. 将对称组交换,转至步骤 \(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】的更多相关文章

  1. Codeforces Global Round 11 个人题解(B题)

    Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...

  2. Codeforces Global Round 11 A~D题解

    A.Avoiding Zero 题目链接:https://codeforces.ml/contest/1427 题目大意:给定一个数组a1,a2...,an,要求找出一个a重排后的数组b1,b2,.. ...

  3. Codeforces Global Round 11 D. Unshuffling a Deck(构造/相邻逆序对)

    题目链接:https://codeforces.com/contest/1427/problem/D 题意 给出一个大小为 \(n\) 的排列,每次操作可以将 \(n\) 个数分为 \(1 \sim ...

  4. Codeforces Global Round 11 C. The Hard Work of Paparazzi(dp/最长上升子序列)

    题目链接:https://codeforces.com/contest/1427/problem/C 题意 \(r\) 行与 \(r\) 列相交形成了 \(r \times r\) 个点,初始时刻记者 ...

  5. Codeforces Global Round 11 B. Chess Cheater(贪心)

    题目链接:https://codeforces.com/contest/1427/problem/B 题意 给出一个长为 \(n\) 由 W, L 组成的字符串,如果一个 W 左侧为 W,则它提供 2 ...

  6. Codeforces Global Round 11 A. Avoiding Zero(前缀和)

    题目链接:https://codeforces.com/contest/1427/problem/A 题意 将 \(n\) 个数重新排列使得不存在为 \(0\) 的前缀和. 题解 计算正.负前缀和,如 ...

  7. Codeforces Global Round 11 C. The Hard Work of Paparazzi (DP)

    题意:有\(r\)X\(r\)的网格图,有\(n\)位名人,会在\(t_i\)时出现在\((x_i,y_i)\),如果过了\(t_i\)名人就会消失,从某一点走到另外一点需要花费的时间是它们之间的曼哈 ...

  8. Codeforces Global Round 11 B. Chess Cheater (贪心,结构体排序)

    题意:你和朋友进行了\(n\)个回合的棋艺切磋,没有平局,每次要么输要么赢,每次赢可以得一分,假如前一局也赢了,那么可以得两分,结果已成定局,但是你确可以作弊,最多修改\(k\)个回合的结果,问你作弊 ...

  9. 【手抖康复训练1 】Codeforces Global Round 6

    [手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...

随机推荐

  1. 【Problems】Could not set property 'id' of 'xxx' with value '' Cause argument type mismatch

    一个问题:向comment表添加记录时,报错, 无法设置值. reflection.ReflectionException: Could not set property 'id' of 'class ...

  2. 【Spring】Spring中的Bean - 4、Bean的生命周期

    Bean的生命周期 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 了解Spring中Bean的生命周期有何意义? 了解Sp ...

  3. ctfhub技能树—RCE—命令注入

    打开靶机 查看页面信息 输入127.0.0.1进行测试 构造payload 127.0.0.1&ls 查看文件内容信息 127.0.0.1 & cat 179852221619745. ...

  4. Netty的简单Demo

    这个demo是通过网上下载: 使用maven构建的: 项目结构: pom.xml: <dependencies> <dependency> <groupId>io. ...

  5. luoguP2016 战略游戏

    题目描述 Bob喜欢玩电脑游戏,特别是战略游戏.但是他经常无法找到快速玩过游戏的办法.现在他有个问题.他要建立一个古城堡,城堡中的路形成一棵树.他要在这棵树的结点上放置最少数目的士兵,使得这些士兵能了 ...

  6. Redis 实战 —— 03. Redis 简单实践 - Web应用

    需求 功能: P23 登录 cookie 购物车 cookie 缓存生成的网页 缓存数据库行 分析网页访问记录 高层次角度下的 Web 应用 P23 从高层次的角度来看, Web 应用就是通过 HTT ...

  7. 2V转3V的电源芯片电路图,2.4V转3V电路

    两节镍氢电池1.2V+1.2V是2.4V的标称电压,2.4V可以转3V输出电路应用. 在2.4V转3V和2V转3V的应用中,输出电流可最大600MA. 2V的低压输入,可以采用PW5100低压输入专用 ...

  8. LSM(Log Structured Merge Trees ) 笔记

    目录 一.大幅度制约存储介质吞吐量的原因 二.传统数据库的实现机制 三.LSM Tree的历史由来 四.提高写吞吐量的思路 4.1 一种方式是数据来后,直接顺序落盘 4.2 另一种方式,是保证落盘的数 ...

  9. 精通MySQL之架构篇

    老刘是即将找工作的研究生,自学大数据开发,一路走来,感慨颇深,网上大数据的资料良莠不齐,于是想写一份详细的大数据开发指南.这份指南把大数据的[基础知识][框架分析][源码理解]都用自己的话描述出来,让 ...

  10. MAX232数据方向

    在调试一个新板子的时候,串口调试从来都是放在前面的,而由于是一个新板子,电路图也是新的,因此有时候不知道串口线结对了没有,这个时候可能会对照PCB和原理图去看一下,但有时候看的人会很迷糊,因为不同的人 ...