【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 ...
随机推荐
- 【MySQL】GTID小结
1.GTID的概念 GTID(global transaction identifier)是全局事务标识符,在MySQL5.6版本中作为一个超级特性被推出.事务标识不仅对于Master(起源)的服务器 ...
- tensorflow之tf.shape()
tf.shape()这个方法就相当于numpy当中shape属性. 下面通过列子来了解: 具体而言,tf.shape是用来获取张量的维度(shape).
- go 1.13 环境变量配置
GO111MODULE="off"GOARCH="amd64"GOBIN=""GOCACHE="/Users/js/Library ...
- 用maven对ssm进行整合
网上有很多ssm整合的教程,这里给像我这样的初学的新手提供一个整合方法,同时也记录一下自己的学习进度. 同时推荐观看相关视频 https://www.bilibili.com/video/av536 ...
- RabbitMQ、RPC、SaltStack "贡"具的使用
消息队列 使用队列的场景 在程序系统中,例如外卖系统,订单系统,库存系统,优先级较高 发红包,发邮件,发短信,app消息推送等任务优先级很低,很适合交给消息队列去处理,以便于程序系统更快的处理其他请求 ...
- 洛谷 p1541乌龟棋
洛谷 p1541乌龟棋 题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行NN个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第NN格是终点,游戏 ...
- kuangbin专题简单搜索题目几道题目
1.POJ1321棋盘问题 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形 ...
- IntelliJ IDEA 中使用 Lambok (注解无效问题的解决)
一,概述 Lambok可以说一个能很大提高开发效率的插件,只要在使用注解的方式就能实现很多常用的功能.如常用的@Data能在编译阶段自动生成toString方法,getter方法setter方法等. ...
- 虚拟机中安装Kali遇到的问题及解决方法
title: 虚拟机中安装Kali遇到的问题及解决方法 date: 2018-11-25 12:25:43 tags: 安全 --- 关于Kali版本选择 kail官方下载页面 虚拟机中当然就下载虚拟 ...
- iOS - iPhone屏幕适配/启动图适配/APP图标适配(iPhone最全尺寸包含iPhoneX/XR/XS/XS Max等)
趁iPhone新品还没有发布,先整理一下屏幕适配.启动图适配.APP图标适配的笔记,方便以后查阅: 注:部分图片来源于网络 违删; (一)iPhone屏幕适配: (1)屏幕分辨率: ①设计尺寸规范(表 ...