【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 ...
随机推荐
- Topshelf + QuartzNet 实现挂载在 WIndows Services 中的定时任务
直接贴代码了: 首先我们可以把所有的 Job 放到一个单独的 DLL 中,好处是可以共享这些业务 Job.比如我们新建一个 QuartzNetDemo.WinService.Jobs 的类库. 然后, ...
- 2019-11-25-win10-uwp-发布旁加载自动更新
原文:2019-11-25-win10-uwp-发布旁加载自动更新 title author date CreateTime categories win10 uwp 发布旁加载自动更新 lindex ...
- Git在提交代码时出现的fatal: Authentication failed的问题
git push origin master remote: Incorrect username or password ( access token ) fatal: Authentication ...
- asp.net mvc4 bundle添加带min的js问题
今天在用ScriptBundle的时候发现js文件有min的,无法bundle进去,具体我也不知道怎么回事. @Tony Tan 回复:bundles.IgnoreList可以设置 去除min.js的 ...
- 深入浅出《设计模式》之工厂模式(C++)
前言 模式介绍 在之前简单工厂模式中,我们介绍了简单工厂模式的缺陷是违背了开放-封闭原则.如果在面馆中添加了烤海参,那将会修改waiter工厂类.违背了类内封闭原则. 还以面馆为例,现在两种面,用一个 ...
- 转cocopods的使用
一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库. 二.安装 由于 ...
- afnetwork使用
Usage HTTP Request Operation Manager AFHTTPRequestOperationManager encapsulates the common patterns ...
- 电信NBIOT 5 - NB73模块下行测试(自己平台-电线平台-NB73)
电信NBIOT 1 - 数据上行(中国电信开发者平台对接流程) 电信NBIOT 2 - 数据上行(中间件获取电信消息通知) 电信NBIOT 3 - 数据下行 电信NBIOT 4 - NB73模块上行测 ...
- 8.如何自己设计一个类似 Dubbo 的 RPC 框架?
作者:中华石杉 面试题 如何自己设计一个类似 Dubbo 的 RPC 框架? 面试官心理分析 说实话,就这问题,其实就跟问你如何自己设计一个 MQ 一样的道理,就考两个: 你有没有对某个 rpc 框架 ...
- 简单使用:spring boot整合spring Data JPA
JPA顾名思义就是Java Persistence API的意思,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. 1.jpa具有什么优势? (1).标准 ...