比赛传送门

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)的更多相关文章

  1. Codeforces Round #605 (Div. 3) 比赛总结

    比赛情况 2h才刀了A,B,C,D.E题的套路做的少,不过ygt大佬给我讲完思路后赛后2min就AC了这题. 比赛总结 比赛时不用担心"时间短,要做多快",这样会匆匆忙忙,反而会做 ...

  2. Codeforces Round #605 (Div. 3)

    地址:http://codeforces.com/contest/1272 A. Three Friends 仔细读题能够发现|a-b| + |a-c| + |b-c| = |R-L|*2 (其中L ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)

    链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...

  8. Codeforces Round #605 (Div. 3) A. Three Friends(贪心)

    链接: https://codeforces.com/contest/1272/problem/A 题意: outputstandard output Three friends are going ...

  9. Codeforces Round #605 (Div. 3) 题解

    Three Friends Snow Walking Robot Yet Another Broken Keyboard Remove One Element Nearest Opposite Par ...

随机推荐

  1. 应用层内存溢出/越界/重复释放等问题检查工具(ASan)

    https://github.com/google/sanitizers/wiki https://github.com/google/sanitizers/wiki/AddressSanitizer ...

  2. 记一次内存无法回收导致频繁fullgc机器假死的思路

    确定挂机 络绎不绝的来不同类型的bug 当bug滚滚而来时,不要怀疑,你的发布的应用基本是不可用状态了.观察哨兵监控数据,特别是内存打到80%基本就挂机了,或者监控数据缺失也基本是挂机了.此时应当马上 ...

  3. date——系统时间的命令

    这是一个可以用各种姿势获得各种时间的命令.最近在写自动化定时脚本时学了一下. 参考:https://www.cnblogs.com/ginvip/p/6357378.html 比如: 利用cronta ...

  4. C#,二分法,BinarySearch()

    static int BinarySearch(int[] arr,int key,int low,int high) { low = 0;high = arr.Length - 1; while(l ...

  5. 如何利用 VisualStudio2019 遠端工具進行偵錯

    Hi 這次要來介紹 如何使用 Visual Studio 2019 遠端工具進行 Release 應用程式偵錯 首先我們先下載 2019 專用的遠端工具(這裡依照不同的 VisualStudio 版本 ...

  6. Linux目录和文件——查询目录和文件的命令

    Linux目录和文件——查询目录和文件的命令 摘要:本文主要学习了在Linux系统中是如何查询目录和文件的. which命令 which命令是根据PATH环境变量设置的路径,去搜索执行文件. 基本语法 ...

  7. Web Api 模型绑定 二

    [https://docs.microsoft.com/zh-cn/aspnet/core/web-api/?view=aspnetcore-2.2] 1.ApiController属性使模型验证错误 ...

  8. python3访问限制

    在Class内部,可以有属性和方法,而外部代码可以通过直接调用实例变量的方法来操作数据,这样,就隐藏了内部的复杂逻辑. 但是,从前面Student类的定义来看,外部代码还是可以自由地修改一个实例的na ...

  9. DDL创建数据库,表以及约束(极客时间学习笔记)

    DDL DDL是DBMS的核心组件,是SQL的重要组成部分. DDL的正确性和稳定性是整个SQL发型的重要基础. DDL的基础语法及设计工具 DDL的英文是Data Definition Langua ...

  10. VUE 直接通过JS 修改html对象的值导致没有更新到数据中去

    业务场景 我们在使用vue 编写 代码时,我们有一个 多行文本框控件,希望在页面点击一个按钮 在 文本框焦点位置插入一个 {pk}的数据. 发现插入 这个数据后,这个数据并没有同步到 数据中,但是直接 ...