C/C++教程

Codeforces Round #753(div3)(ABCD)

本文主要是介绍Codeforces Round #753(div3)(ABCD),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

A - Linear Keyboard
You are given a keyboard that consists of 2626 keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.

You have to type the word ss on this keyboard. It also consists only of lowercase Latin letters.

To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.

Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.

For example, consider a keyboard where the letters from ‘a’ to ‘z’ are arranged in consecutive alphabetical order. The letters ‘h’, ‘e’, ‘l’ and ‘o’ then are on the positions 88, 55, 1212 and 1515, respectively. Therefore, it will take |5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13∣5−8∣+∣12−5∣+∣12−12∣+∣15−12∣=13 units of time to type the word “hello”.

Determine how long it will take to print the word ss.

Input
The first line contains an integer tt (1 \leq t \leq 10001≤t≤1000) — the number of test cases.

The next 2t2t lines contain descriptions of the test cases.

The first line of a description contains a keyboard — a string of length 2626, which consists only of lowercase Latin letters. Each of the letters from ‘a’ to ‘z’ appears exactly once on the keyboard.

The second line of the description contains the word ss. The word has a length from 11 to 5050 letters inclusive and consists of lowercase Latin letters.

Output
Print tt lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word ss on the given keyboard.

Example
Input
5
abcdefghijklmnopqrstuvwxyz
hello
abcdefghijklmnopqrstuvwxyz
i
abcdefghijklmnopqrstuvwxyz
codeforces
qwertyuiopasdfghjklzxcvbnm
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
qwertyuiopasdfghjklzxcvbnm
abacaba
Output
13
0
68
0
74
题意
给你一串字符,字符的下标代表字符的位置,然后再给目标串,字符a[i],到b[i],的移动距离为|j-i|,问要打完目标串要移动多少距离?
题解
首先字符串的顺序无所谓,只要知道每个字符的下标就行,所以,用a[字符-‘a’],这样只要定义a[26],然后遍历把距离相加。

#include<iostream>
#include<string>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {

        string a,b;
        cin>>a;
        cin>>b;
        int str[27],s=0,n=a.size(),n1=b.size(),x,y;
        for(int i=0;i<n;++i)
        {

            str[a[i]-'a']=i;
        }

        for(int i=0;i<n1;i++)
        {
            if(i==0)
            {

                x=str[b[i]-'a'];
                continue;
            }
            y=str[b[i]-'a'];
            if(x>y)
            s+=(x-y);
            else s+=(y-x);
            x=y;
        }
        cout<<s<<endl;
    }

}

B - Odd Grasshopper
The grasshopper is located on the numeric axis at the point with coordinate x_0x
0

.

Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate xx with a distance dd to the left moves the grasshopper to a point with a coordinate x - dx−d, while jumping to the right moves him to a point with a coordinate x + dx+d.

The grasshopper is very fond of positive integers, so for each integer ii starting with 11 the following holds: exactly ii minutes after the start he makes a jump with a distance of exactly ii. So, in the first minutes he jumps by 11, then by 22, and so on.

The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.

For example, if after 1818 consecutive jumps he arrives at the point with a coordinate 77, he will jump by a distance of 1919 to the right, since 77 is an odd number, and will end up at a point 7 + 19 = 267+19=26. Since 2626 is an even number, the next jump the grasshopper will make to the left by a distance of 2020, and it will move him to the point 26 - 20 = 626−20=6.

Find exactly which point the grasshopper will be at after exactly nn jumps.

Input
The first line of input contains an integer tt (1 \leq t \leq 10^41≤t≤10
4
) — the number of test cases.

Each of the following tt lines contains two integers x_0x
0

(-10^{14} \leq x_0 \leq 10^{14}−10
14
≤x
0

≤10
14
) and nn (0 \leq n \leq 10^{14}0≤n≤10
14
) — the coordinate of the grasshopper’s initial position and the number of jumps.

Output
Print exactly tt lines. On the ii-th line print one integer — the answer to the ii-th test case — the coordinate of the point the grasshopper will be at after making nn jumps from the point x_0x
0

.

Example
Input
9
0 1
0 2
10 10
10 99
177 13
10000000000 987654321
-433494437 87178291199
1 0
-1 1
Output
-1
1
11
110
190
9012345679
-87611785637
1
0
Note
The first two test cases in the example correspond to the first two jumps from the point x_0 = 0x
0

=0.

Since 00 is an even number, the first jump of length 11 is made to the left, and the grasshopper ends up at the point 0 - 1 = -10−1=−1.

Then, since -1−1 is an odd number, a jump of length 22 is made to the right, bringing the grasshopper to the point with coordinate -1 + 2 = 1−1+2=1.
题意
给一个起始坐标,和跳多少次,第一次跳的距离d=1,第二次d=2,…第n次 d=n;
条之前的坐标 奇数 向右跳,偶数向左,问最后的位置坐标、
题解
奇数 +奇数 +o +j +o +j +o
j o o j | j o o j, j
1 2 3 4 5 6 7 8

偶数 +j +o +j +o +j +o +j +o
o j j o | o j j o o
通过列举奇偶,可以发现规律,不管奇偶四个数就相当于没有跳在原地。就是对4求余,然后枚举余数等于0,1,2,3

#include<iostream>
#include<string>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {

       long long n,x,k;
       cin>>x>>n;
       k=n%4;

       if(x%2==0)
       {
           if(k==1)
                x=x-n;
           else if(k==2)
            x=x+1;
           else if(k==3)
            x=x+1+n;

       }
       else {
      if(k==1)
                x=x+n;
           else if(k==2)
            x=x-1;
           else if(k==3)
            x=x-1-n;
       }
       cout<<x<<endl;
    }

}

C - Minimum Extraction
Yelisey has an array aa of nn integers.

If aa has length strictly greater than 11, then Yelisei can apply an operation called minimum extraction to it:

First, Yelisei finds the minimal number mm in the array. If there are several identical minima, Yelisey can choose any of them.
Then the selected minimal element is removed from the array. After that, mm is subtracted from each remaining element.
Thus, after each operation, the length of the array is reduced by 11.

For example, if a = [1, 6, -4, -2, -4]a=[1,6,−4,−2,−4], then the minimum element in it is a_3 = -4a
3

=−4, which means that after this operation the array will be equal to a=[1 {- (-4)}, 6 {- (-4)}, -2 {- (-4)}, -4 {- (-4)}] = [5, 10, 2, 0]a=[1−(−4),6−(−4),−2−(−4),−4−(−4)]=[5,10,2,0].

Since Yelisey likes big numbers, he wants the numbers in the array aa to be as big as possible.

Formally speaking, he wants to make the minimum of the numbers in array aa to be maximal possible (i.e. he want to maximize a minimum). To do this, Yelisey can apply the minimum extraction operation to the array as many times as he wants (possibly, zero). Note that the operation cannot be applied to an array of length 11.

Help him find what maximal value can the minimal element of the array have after applying several (possibly, zero) minimum extraction operations to the array.

Input
The first line contains an integer tt (1 \leq t \leq 10^41≤t≤10
4
) — the number of test cases.

The next 2t2t lines contain descriptions of the test cases.

In the description of each test case, the first line contains an integer nn (1 \leq n \leq 2 \cdot 10^51≤n≤2⋅10
5
) — the original length of the array aa. The second line of the description lists nn space-separated integers a_ia
i

(-10^9 \leq a_i \leq 10^9−10
9
≤a
i

≤10
9
) — elements of the array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 2 \cdot 10^52⋅10
5
.

Output
Print tt lines, each of them containing the answer to the corresponding test case. The answer to the test case is a single integer — the maximal possible minimum in aa, which can be obtained by several applications of the described operation to it.

Example
Input
8
1
10
2
0 0
3
-1 2 0
4
2 10 1 7
2
2 3
5
3 2 -4 -2 0
2
-1 1
1
-2
Output
10
0
2
5
2
2
2
-2
Note
In the first example test case, the original length of the array n = 1n=1. Therefore minimum extraction cannot be applied to it. Thus, the array remains unchanged and the answer is a_1 = 10a
1

=10.

In the second set of input data, the array will always consist only of zeros.

In the third set, the array will be changing as follows: [\color{blue}{-1}, 2, 0] \to [3, \color{blue}{1}] \to [\color{blue}{2}][−1,2,0]→[3,1]→[2]. The minimum elements are highlighted with \color{blue}{\text{blue}}blue. The maximal one is 22.

In the fourth set, the array will be modified as [2, 10, \color{blue}{1}, 7] \to [\color{blue}{1}, 9, 6] \to [8, \color{blue}{5}] \to [\color{blue}{3}][2,10,1,7]→[1,9,6]→[8,5]→[3]. Similarly, the maximum of the minimum elements is 55.
题意
给一个数组,可以进行以下操作,选择一个最小数,将其余数组元素与他相减,删除这个数,当数组元素位于不能操作。问数组中的最小值最大是多少?
题解
先排序,进行模拟,直到数组元素个数为1,在过程中记录最小值,找到最大的就是结果,另外不需要每次都对最小值后面的元素进行减,只需用t记录,操作时再减。

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
       long long n,a[200005],t=0,b=0,m=-9999999999;
       cin>>n;
       for(int i=0;i<n;++i)
       {

           cin>>a[i];
       }
       sort(a,a+n);

       for(int i=0;i<n;++i)
       {if(m<a[i]-t)
        m=a[i]-t;
   
            if(i!=n-1)
           t+=(a[i]-t);


  
       }

       cout<<m<<endl;

    }

}


D - Blue-Red Permutation

You are given an array of integers aa of length nn. The elements of the array can be either different or the same.

Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step:

either you can select any blue element and decrease its value by 11;
or you can select any red element and increase its value by 11.
Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.

Determine whether it is possible to make 00 or more steps such that the resulting array is a permutation of numbers from 11 to nn?

In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array aa contains in some order all numbers from 11 to nn (inclusive), each exactly once.

Input
The first line contains an integer tt (1 \leq t \leq 10^41≤t≤10
4
) — the number of input data sets in the test.

The description of each set of input data consists of three lines. The first line contains an integer nn (1 \leq n \leq 2 \cdot 10^51≤n≤2⋅10
5
) — the length of the original array aa. The second line contains nn integers a_1a
1

, a_2a
2

, …, a_na
n

(-10^9 \leq a_i \leq 10^9−10
9
≤a
i

≤10
9
) — the array elements themselves.

The third line has length nn and consists exclusively of the letters ‘B’ and/or ‘R’: iith character is ‘B’ if a_ia
i

is colored blue, and is ‘R’ if colored red.

It is guaranteed that the sum of nn over all input sets does not exceed 2 \cdot 10^52⋅10
5
.

Output
Print tt lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise.

You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).

Example
Input
8
4
1 2 5 2
BRBR
2
1 1
BB
5
3 1 4 2 5
RBRRB
5
3 1 3 1 3
RBRRB
5
5 1 5 1 5
RBRRB
4
2 2 2 2
BRBR
2
1 -2
BR
4
-2 -1 4 0
RRRR
Output
YES
NO
YES
YES
NO
YES
YES
YES
Note
In the first test case of the example, the following sequence of moves can be performed:

choose i=3i=3, element a_3=5a
3

=5 is blue, so we decrease it, we get a=[1,2,4,2]a=[1,2,4,2];
choose i=2i=2, element a_2=2a
2

=2 is red, so we increase it, we get a=[1,3,4,2]a=[1,3,4,2];
choose i=3i=3, element a_3=4a
3

=4 is blue, so we decrease it, we get a=[1,3,3,2]a=[1,3,3,2];
choose i=2i=2, element a_2=2a
2

=2 is red, so we increase it, we get a=[1,4,3,2]a=[1,4,3,2].
We got that aa is a permutation. Hence the answer is YES.
题意
给定一个数组,然后给定一个字符串,字符串由B,R,组成,B

这篇关于Codeforces Round #753(div3)(ABCD)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!