【cf比赛记录】Codeforces Round #605 (Div. 3)
比赛传送门
Div3真的是暴力杯,比div2还暴力吧(这不是明摆的嘛),所以对我这种一根筋的挺麻烦的,比如A题就自己没转过头来浪费了很久,后来才醒悟过来了。然后这次竟然还上分了......

A题:爆搜
B题:字符串,简单贪心
C题:字符串,简单数学
D题:DP
A. Three Friends
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Three friends are going to meet each other. Initially, the first friend stays at the position \(x=a\), the second friend stays at the position \(x=b\) and the third friend stays at the position \(x=c\) on the coordinate axis \(Ox\).
In one minute each friend independently from other friends can change the position xx by \(1\) to the left or by \(1\) to the right (i.e. set \(x:=x−1\) or \(x:=x+1\)) or even don't change it.
Let's introduce the total pairwise distance — the sum of distances between each pair of friends. Let a′a′, b′b′ and c′c′ be the final positions of the first, the second and the third friend, correspondingly. Then the total pairwise distance is \(|a′−b′|+|a′−c′|+|b′−c′|\), where \(|x|\) is the absolute value of \(x\).
Friends are interested in the minimum total pairwise distance they can reach if they will move optimally. Each friend will move no more than once. So, more formally, they want to know the minimum total pairwise distance they can reach after one minute.
You have to answer \(q\) independent test cases.
Input
The first line of the input contains one integer \(q\) (\(1≤q≤1000\)) — the number of test cases.
The next \(q\) lines describe test cases. The \(i\)-th test case is given as three integers \(a,b\) and \(c\) (\(1≤a,b,c≤10^9\)) — initial positions of the first, second and third friend correspondingly. The positions of friends can be equal.
Output
For each test case print the answer on it — the minimum total pairwise distance (the minimum sum of distances between each pair of friends) if friends change their positions optimally. Each friend will move no more than once. So, more formally, you have to find the minimum total pairwise distance they can reach after one minute.
Example
input
Copy
8
3 3 4
10 20 30
5 5 5
2 4 3
1 1000000000 1000000000
1 1000000000 999999999
3 2 5
3 2 6
output
Copy
0
36
0
0
1999999994
1999999994
2
4
//https://codeforces.com/contest/1272/problem/A
/*
题意:
a,b,c 三点位置都可以走一格或者不走
求走后 |a′−b′|+|a′−c′|+|b′−c′| 的值最小
所以三重遍历就可以搜索完了全部情况
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int T;
long long a, b, c;
int main()
{
scanf("%d", &T);
while(T--){
scanf("%I64d %I64d %I64d", &a, &b, &c);
long long A, B, C;
long long ans = 0x3f3f3f3f3f3f3f3f; // 把 ans 放一个很大的数
// 三重遍历
for(long long i = -1; i < 2; i++){
for(long long j = -1; j < 2; j++){
for(long long k = -1; k < 2; k++){
A = a; B = b; C = c;
A += i; B += j; C += k;
ans = min(ans, llabs(A - B) + llabs(A - C) + llabs(B - C));
}
}
}
printf("%I64d\n", ans);
}
return 0;
}
B. Snow Walking Robot
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell \((0,0)\) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string \(s\) consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell \((x,y)\) right now, he can move to one of the adjacent cells (depending on the current instruction).
- If the current instruction is 'L', then the robot can move to the left to \((x−1,y)\);
- if the current instruction is 'R', then the robot can move to the right to \((x+1,y)\);
- if the current instruction is 'U', then the robot can move to the top to \((x,y+1)\);
- if the current instruction is 'D', then the robot can move to the bottom to \((x,y−1)\).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except \((0,0)\)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell \((0,0)\), performs the given instructions, visits no cell other than \((0,0)\) two or more times and ends the path in the cell \((0,0)\). Also cell \((0,0)\) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not \((0,0)\)) and "UUDD" (the cell \((0,1)\) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer \(q\) independent test cases.
Input
The first line of the input contains one integer \(q\) (\(1≤q≤2⋅10^4\)) — the number of test cases.
The next \(q\) lines contain test cases. The \(i\)-th test case is given as the string ss consisting of at least \(1\) and no more than \(10^5\) characters 'L', 'R', 'U' and 'D' — the initial sequence of instructions.
It is guaranteed that the sum of \(|s|\) (where \(|s|\) is the length of ss) does not exceed 105105 over all test cases (\(∑|s|≤10^5\)).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions tt the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is \(0\), you are allowed to print an empty line (but you can don't print it).
Example
input
Copy
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
output
Copy
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:

Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR".
// https://codeforces.com/contest/1272/problem/B
/*
题意:
有一串操作指令:上下左右
要求用这些操作指令从(0,0)出发然后走完后就回到(0,0)
在走的过程中不能走回头路(即不能走到相同位置上,(0,0)除外)
且要走的路要最长
那简单,用合理的指令走最大的环就行
*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int T;
char ch[100005];
int u, d, l, r; // 记录原本的上下左右键
int main()
{
scanf("%d", &T);
while(T--){
u = d = r = l = 0;
scanf("%s", ch);
int len = strlen(ch);
for(int i = 0; i < len; i++){
if(ch[i] == 'U') u++;
else if(ch[i] == 'D') d++;
else if(ch[i] == 'R') r++;
else l++;
}
// printf("u:%d d:%d l:%d r:%d\n", u, d, l, r);
if(u == 0 || d == 0){ // 不能往上或往下走
if(r == 0 || l == 0) printf("0\n");
else {
printf("2\nLR\n");
}
}
else if(l == 0 || r == 0){ // 不能往左走或往右走
if(u == 0 || d == 0) printf("0\n");
else {
printf("2\nUD\n");
}
}
else {
int y = min(d, u);
int x = min(l, r);
printf("%d\n", (x + y) * 2); // 画个环
for(int i = 0; i < y; i++) printf("U");
for(int i = 0; i < x; i++) printf("L");
for(int i = 0; i < y; i++) printf("D");
for(int i = 0; i < x; i++) printf("R");
printf("\n");
}
}
return 0;
}
C. Yet Another Broken Keyboard
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Recently, Norge found a string \(s=s_1s_2…s_n\) consisting of \(n\) lowercase Latin letters. As an exercise to improve his typing speed, he decided to type all substrings of the string \(s\). Yes, all \(\frac{n(n+1)}{2}\) of them!
A substring of \(s\) is a non-empty string \(x=s[a…b]=s_as_{a+1}…s_b\) (\(1≤a≤b≤n\)). For example, "auto" and "ton" are substrings of "automaton".
Shortly after the start of the exercise, Norge realized that his keyboard was broken, namely, he could use only \(k\) Latin letters \(c_1,c_2,…,c_k\) out of \(26\).
After that, Norge became interested in how many substrings of the string \(s\) he could still type using his broken keyboard. Help him to find this number.
Input
The first line contains two space-separated integers \(n\) and \(k\) (\(1≤n≤2⋅10^5\), \(1≤k≤26\)) — the length of the string \(s\) and the number of Latin letters still available on the keyboard.
The second line contains the string \(s\) consisting of exactly \(n\) lowercase Latin letters.
The third line contains kk space-separated distinct lowercase Latin letters \(c1,c2,…,ck\) — the letters still available on the keyboard.
Output
Print a single number — the number of substrings of ss that can be typed using only available letters \(c1,c2,…,ck\).
Examples
input
Copy
7 2
abacaba
a b
output
Copy
12
input
Copy
10 3
sadfaasdda
f a d
output
Copy
21
input
Copy
7 1
aaaaaaa
b
output
Copy
0
Note
In the first example Norge can print substrings \(s[1…2]\), \(s[2…3]\),$ s[1…3]$, \(s[1…1]\), \(s[2…2]\), \(s[3…3]\), \(s[5…6]\), \(s[6…7]\), \(s[5…7]\), \(s[5…5]\), \(s[6…6]\), \(s[7…7]\).
// https://codeforces.com/contest/1272/problem/C
#include<iostream>
#include<cstdio>
using namespace std;
/*
题意:
有一串字符串
你的键盘可以输入 k 个字符
问用这个 k 个字符可以写多少个字符串满足远字符串的子序列
样例一:
7 2
abacaba
a b
用 a b 可以组成 12 种
分析:
原字符粗前面的 "aba" 就可以分为 6 种 后面的 "aba" 就可以组成 6 种
分析一下,当可写的字符串长度为 n 时,满足 n * (n - 1) * (n - 2) * ... * 2 * 1 = n * (n + 1) / 2
然后把这每一段的 (n) 加一起就可以(注意越界)
*/
int n, k;
char s[200005];
char ch[30];
int main()
{
cin >> n >> k;
cin >> s;
for(int i = 0; i < k; i++) cin >> ch[i];
long long ans = 0;
long long len = 0;
bool flag;
for(int i = 0; i < n; i++){
// printf("i:%d s:%c\n", i, s[i]);
flag = false;
for(int j = 0; j < k; j++){
// printf("j:%d s:%c ch:%c\n", j, s[i], ch[j]);
if(s[i] == ch[j]){
flag = true;
len++;
break;
}
}
// printf("i:%d len:%d flag:%d\n", i, len, flag);
// printf("\n");
if(!flag || i == n - 1){
ans += (len + 1) * len / 2;
len = 0;
}
}
printf("%I64d\n", ans);
return 0;
}
D. Remove One Element
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array aa consisting of nn integers.
You can remove at most one element from this array. Thus, the final length of the array is \(n−1\) or \(n\).
Your task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array.
Recall that the contiguous subarray aa with indices from ll to rr is \(a[l…r]=a_l\),\(a_{l+1},…,a_r\). The subarray \(a[l…r]\) is called strictly increasing if \(a_l<a_{l+1}<⋯<a_r\).
Input
The first line of the input contains one integer \(n\) (\(2≤n≤2⋅10^5\)) — the number of elements in \(a\).
The second line of the input contains \(n\) integers \(a_1,a_2,…,a_n\) (\(1≤ai≤10^9\)), where \(a_i\) is the \(i\)-th element of \(a\).
Output
Print one integer — the maximum possible length of the strictly increasing contiguous subarray of the array aa after removing at most one element.
Examples
input
Copy
5
1 2 5 3 4
output
Copy
4
input
Copy
2
1 2
output
Copy
2
input
Copy
7
6 5 4 3 2 4 3
output
Copy
2
Note
In the first example, you can delete \(a_3=5\). Then the resulting array will be equal to [1,2,3,4][1,2,3,4] and the length of its largest increasing subarray will be equal to \(4\).
// https://codeforces.com/contest/1272/problem/D
#include<iostream>
#include<cstdio>
using namespace std;
/*
题意:
可以剔除一个数或者不剔除
求操作或者不操作后最长的升序子序列长度
*/
// dp[][0] 表示没有剔除时当前位置的最长上升子序列
// dp[][1] 表示剔除一个数之后当前位置的的最长上升子序列
int dp[200005][2];
int n, num[200005];
int ans;
//10
//1 2 5 4 3 8 9 4 8 10
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%d", &num[i]);
dp[i][0] = 1; // 每一位最短长度是 1
}
for(int i = 2; i <= n; i++){
if(num[i] > num[i - 1]) {
dp[i][0] = dp[i - 1][0] + 1;
dp[i][1] = dp[i - 1][1] + 1; // 因为第 1 位剔除了,所以从 0 开始
}
if(num[i] > num[i - 2]){
dp[i][1] = max(dp[i][1], dp[i - 2][0] + 1); // 每次与 dp[i - 2][0] 相比,符合只剔除一个数的规则
}
ans = max(ans, max(dp[i][1], dp[i][0])); // 每次比较当前位以得到最长上升子序列长度
}
// for(int i = 1; i <= n; i++){
// printf("i:%d zero:%d one:%d\n", i, dp[i][0], dp[i][1]);
// }
printf("%d\n", ans);
return 0;
}
啊,把题目copy下来写博客的时间长了好多,下次可能不copy了。
记录辣鸡成长:

【cf比赛记录】Codeforces Round #605 (Div. 3)的更多相关文章
- Codeforces Round #605 (Div. 3) 比赛总结
比赛情况 2h才刀了A,B,C,D.E题的套路做的少,不过ygt大佬给我讲完思路后赛后2min就AC了这题. 比赛总结 比赛时不用担心"时间短,要做多快",这样会匆匆忙忙,反而会做 ...
- Codeforces Round #605 (Div. 3)
地址:http://codeforces.com/contest/1272 A. Three Friends 仔细读题能够发现|a-b| + |a-c| + |b-c| = |R-L|*2 (其中L ...
- Codeforces Round #605 (Div. 3) E - Nearest Opposite Parity
题目链接:http://codeforces.com/contest/1272/problem/E 题意:给定n,给定n个数a[i],对每个数输出d[i]. 对于每个i,可以移动到i+a[i]和i-a ...
- Codeforces Round #605 (Div. 3) E. Nearest Opposite Parity(最短路)
链接: https://codeforces.com/contest/1272/problem/E 题意: You are given an array a consisting of n integ ...
- Codeforces Round #605 (Div. 3) D. Remove One Element(DP)
链接: https://codeforces.com/contest/1272/problem/D 题意: You are given an array a consisting of n integ ...
- Codeforces Round #605 (Div. 3) C. Yet Another Broken Keyboard
链接: https://codeforces.com/contest/1272/problem/C 题意: Recently, Norge found a string s=s1s2-sn consi ...
- Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)
链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...
- Codeforces Round #605 (Div. 3) A. Three Friends(贪心)
链接: https://codeforces.com/contest/1272/problem/A 题意: outputstandard output Three friends are going ...
- Codeforces Round #605 (Div. 3) 题解
Three Friends Snow Walking Robot Yet Another Broken Keyboard Remove One Element Nearest Opposite Par ...
随机推荐
- javascript的数据类型检测
JavaScript有两种数据类型,分别是基本数据类型和引用数据类型.其中基本数据类型包括Undefined.Null.Boolean.Number.String和Symbol(ES6新增,表示独一无 ...
- Prometheus K8S中部署Alertmanager
Prometheus K8S中部署Alertmanager 设置告警和通知的主要步骤如下:一.部署Alertmanager二.配置Prometheus与Alertmanager通信三.配置告警 1. ...
- Prometheus 监控K8S Node监控
Prometheus 监控K8S Node监控 Prometheus社区提供的NodeExporter项目可以对主机的关键度量指标进行监控,通过Kubernetes的DeamonSet可以在各个主机节 ...
- 大话区块链【Blockchain】
最近这几天区块链又粉墨登场了,新闻媒体也一直在大量报道,宣称可能要在金融界掀起一番浪潮.甚至有人说很久之前中国就出现了区块链的产物——麻将.那么区块链到底是什么,麻将和区块链又有什么关系呢? 笔者这两 ...
- Java中级知识归纳(三)
十一.Java垃圾回收机制 Java的垃圾回收机制是Java虚拟机提供的能力,用于在空闲时间以不定时的方式动态回收无任何引用的对象占据的内存空间.可以使用显式调用,System.gc();Runtim ...
- Python【day 10】函数进阶-小结
本节主要内容1.动态参数 *args **kwargs 形参:*args将多个位置参数聚合打包成元组 **kwargs将多个关键字参数聚合打包成字典 实参:*li1将列表进行解包打散成多个位置参数 * ...
- CSS函数大全
CSS函数(方法)参考 CSS函数用作各种CSS属性的值. 函数 描述 attr() 返回所选元素的属性值 calc() 允许您执行计算以确定CSS属性值 cubic-bezier() 定义Cubic ...
- Docker 容器命令大全
容器命令: 命令 描述 attach 将本地标准输入,输出和错误流转到到正在运行的容器 build 从Dockerfile构建映像 commit 根据容器的更改创建新镜像 cp 在容器和本地文件系统之 ...
- 关于项目中js原型的使用
在一个项目中为了减少全局变量的使用及模块化的开发我们使用的构造函数加原型的开发模式 var App = function(){ //管理构造函数的属性 this.name = 'jack' } //页 ...
- LoadRunner性能测试工具下载
LoadRunner性能测试工具 LoadRunner是前美科利(Mercury Interactive)公司著名的性能测试产品.Mercury公司曾经是全球业务优化科技领域的领导者.2006年由惠普 ...