Codeforces Round #605 (Div. 3)

A. Three Friends

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 x 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′, b′ and 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≤109) — 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
inputCopy
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
outputCopy
0
36
0
0
1999999994
1999999994
2
4
题解

#include <bits/stdc++.h>
using namespace std;
int t;
long long a[5];
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>a[1]>>a[2]>>a[3];
        sort(a+1,a+1+3);
        if(a[3]-a[1]<=2){cout<<0<<endl;continue;}
        if(a[3]-a[1]==3)
        {
            cout<<2<<endl;
            continue;
        }
        if(a[1]==a[2])a[2]++;
        else if(a[2]==a[3])a[2]--;
        a[1]++;a[3]--;
        long long ans=abs(a[1]-a[2])+abs(a[2]-a[3])+abs(a[1]-a[3]);
        cout<<ans<<endl;
    }
    return 0;
}

B. Snow Walking Robot

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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⋅104) — the number of test cases.

The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 105 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 s) does not exceed 105 over all test cases (∑|s|≤105).

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 t 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
inputCopy
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
outputCopy
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:

#include <bits/stdc++.h>
using namespace std;
int t;
string s;
int a[5];
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>s;
        memset(a,0,sizeof(a));
        int len=s.length();
        for(int i=0;i<len;i++)
        {
            if(s[i]=='U')a[1]++;
            else if(s[i]=='D')a[2]++;
            else if(s[i]=='R')a[3]++;
            else a[4]++;
        }
        //cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<endl;
        if(a[1]==0||a[2]==0)
        {
            if(a[3]>0&&a[4]>0){cout<<"2\nRL\n";}
            else cout<<"0\n";continue;
        }
        else if(a[3]==0||a[4]==0)
        {
            if(a[1]>0&&a[2]>0){cout<<"2\nUD\n";}
            else cout<<"0\n";continue;
        }
        else
        {
            int k1=min(a[1],a[2]);
            int k2=min(a[3],a[4]);
            cout<<2*k1+2*k2<<endl;
            for(int i=1;i<=k1;i++)cout<<"U";
            for(int i=1;i<=k2;i++)cout<<"R";
            for(int i=1;i<=k1;i++)cout<<"D";
            for(int i=1;i<=k2;i++)cout<<"L";cout<<endl;
        }
    }
    return 0;
}

C. Yet Another Broken Keyboard

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Recently, Norge found a string s=s1s2…sn 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 n(n+1)2 of them!

A substring of s is a non-empty string x=s[a…b]=sasa+1…sb (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 c1,c2,…,ck 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⋅105, 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 k 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 s that can be typed using only available letters c1,c2,…,ck.

Examples
inputCopy
7 2
abacaba
a b
outputCopy
12
inputCopy
10 3
sadfaasdda
f a d
outputCopy
21
inputCopy
7 1
aaaaaaa
b
outputCopy
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].

#include <bits/stdc++.h>
using namespace std;
int n,k;
string s;
int a[30];
int change(char c)
{
    return c-'a';
}
int main()
{
    cin>>n>>k;
    cin>>s;
    string h;
    for(int i=1;i<=k;i++)
    {
        cin>>h;
        int f=change(h[0]);
        a[f]=1;
    }
    long long ans=0,m=0;
    int f;
    for(int i=0;i<n;i++)
    {
         f=change(s[i]);
        if(a[f]==1)m++;
        else {ans+=m*(m+1)/2;m=0;}
    }
    ans+=m*(m+1)/2;
    cout<<ans<<endl;
    return 0;
}

D. Remove One Element

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a consisting of n 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 a with indices from l to r is a[l…r]=al,al+1,…,ar. The subarray a[l…r] is called strictly increasing if al<al+1<⋯<ar.

Input
The first line of the input contains one integer n (2≤n≤2⋅105) — the number of elements in a.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤109), where ai is the i-th element of a.

Output
Print one integer — the maximum possible length of the strictly increasing contiguous subarray of the array a after removing at most one element.

Examples
inputCopy
5
1 2 5 3 4
outputCopy
4
inputCopy
2
1 2
outputCopy
2
inputCopy
7
6 5 4 3 2 4 3
outputCopy
2
Note
In the first example, you can delete a3=5. Then the resulting array will be equal to [1,2,3,4] and the length of its largest increasing subarray will be equal to 4.

#include <bits/stdc++.h>
using namespace std;
int n;
int a[200005];
int s[200005];
vector<int> b[200005];
vector<int>v;
int num=0;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    int ma=0;
    b[num].push_back(a[1]);
    s[1]=1;
    for(int i=2;i<=n;i++)
    {
        s[i]=1;
        if(a[i]>a[i-1]){s[i]=s[i-1]+1;b[num].push_back(a[i]);}
        else {v.push_back(num);num++;b[num].push_back(a[i]);}
        ma=max(ma,s[i]);
    }
    for(auto i:v)
    {
       if(i!=num&&b[i+1][0]>b[i][b[i].size()-2]||(b[i+1][1]>b[i][b[i].size()-1]))
        {
            int tp=b[i].size()-1+b[i+1].size();
            ma=max(ma,tp);
        }
 
    }
    cout<<ma<<endl;
    return 0;
}

补题
E. Nearest Opposite Parity
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i−ai (if 1≤i−ai) or to the position i+ai (if i+ai≤n).

For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that aj has the opposite parity from ai (i.e. if ai is odd then aj has to be even and vice versa).

Input
The first line of the input contains one integer n (1≤n≤2⋅105) — the number of elements in a.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤n), where ai is the i-th element of a.

Output
Print n integers d1,d2,…,dn, where di is the minimum the number of moves required to reach any position j such that aj has the opposite parity from ai (i.e. if ai is odd then aj has to be even and vice versa) or -1 if it is impossible to reach such a position.

Example
inputCopy
10
4 5 7 6 7 5 4 4 6 4
outputCopy
1 1 1 2 -1 1 1 3 1 1
参考RSHS学长的思路

#include <bits/stdc++.h>
using namespace std;
int a[200010];
int b[200010];
int b1[200010];
int n;
vector<int> v[200010];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++){cin>>a[i];}
    queue<int>q;
    for(int i=1;i<=n;i++)
    {
        b[i]=-1;
        if(i+a[i]<=n)v[i+a[i]].push_back(i);
        if(i-a[i]>=1)v[i-a[i]].push_back(i);
        if(a[i]&1)
        {
            q.push(i);b[i]=0;
        }
    }
    while(!q.empty())
    {
        int now=q.front();q.pop();
        for(auto i:v[now])
        {
            if(b[i]==-1){q.push(i);b[i]=b[now]+1;}
        }
    }
    for(int i=1;i<=n;i++)v[i].clear();
    for(int i=1;i<=n;i++)
    {
        b1[i]=-1;
        if(i+a[i]<=n)v[i+a[i]].push_back(i);
        if(i-a[i]>=1)v[i-a[i]].push_back(i);
        if(a[i]%2==0)
        {
            q.push(i);b1[i]=0;
        }
    }
    while(!q.empty())
    {
        int now=q.front();q.pop();
        for(auto i:v[now])
        {
            if(b1[i]==-1){q.push(i);b1[i]=b1[now]+1;}
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(a[i]&1)cout<<b1[i]<<" ";
        else cout<<b[i]<<" ";
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值