Codeforces Round 859 Div. 4题解

目录

A. Plus or Minus

B. Grab the Candies

C. Find and Replace

思路

代码 

D. Odd Queries

思路:

代码

G2. Subsequence Addition (Hard Version)

思路

代码

E. Interview

思路:

代码

F. Bouncy Ball

思路

代码


 

A. Plus or Minus

签到

#include
using namespace std;
typedef long long ll;
ll a,b,c;
int main(){
    int t;
    cin>>t;
    while(t--){
        cin>>a>>b>>c;
        if(a+b==c){
            cout<<'+'<

B. Grab the Candies

贪心,先全部偶数给A,再奇数给B,比较大小

#include
using namespace std;
typedef long long ll;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        ll sum1=0,sum2=0;
        while(n--){

            int a;
            cin>>a;
            if(a%2==0){
                sum1+=a;
            }
            else sum2+=a;

        }
            if(sum1>sum2){
                cout<<"Yes"<

C. Find and Replace

You are given a string s consisting of lowercase Latin characters. In an operation, you can take a character and replace all occurrences of this character with 00 or replace all occurrences of this character with 11.

Is it possible to perform some number of moves so that the resulting string is an alternating binary string††?

For example, consider the string abacabaabacaba. You can perform the following moves:

  • Replace aa with 00. Now the string is 0b0c0b00b0c0b0.
  • Replace bb with 11. Now the string is 010c010010c010.
  • Replace cc with 11. Now the string is 01010100101010. This is an alternating binary string.

††An alternating binary string is a string of 00s and 11s such that no two adjacent bits are equal. For example, 0101010101010101, 101101, 11 are alternating binary strings, but 01100110, 0a0a00a0a0, 1010010100 are not.

Input

The input consists of multiple test cases. The first line contains an integer t (1≤t≤100 — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer n (1≤n≤2000) — the length of the string s.

The second line of each test case contains a string consisting of n lowercase Latin characters — the string s

Output

For each test case, output "YES" (without quotes) if you can make the string into an alternating binary string, and "NO" (without quotes) otherwise.

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

Example

input

8

7

abacaba

2

aa

1

y

4

bkpt

6

ninfia

6

banana

10

codeforces

8

testcase

output

YES
NO
YES
YES
NO
YES
NO
NO

Note

The first test case is explained in the statement.

In the second test case, the only possible binary strings you can make are 0000 and 1111, neither of which are alternating.

In the third test case, you can make 11, which is an alternating binary string.

思路

如果有两个相同字母 间隔奇数单位,一定不能形成01串,否则可以

代码 

#include
using namespace std;
typedef long long ll;
int main(){
    int t;
    cin>>t;
    int cnt[1000];
    while(t--){
        memset(cnt,-1,sizeof(cnt));
        int n;
        cin>>n;
        string s;
        cin>>s;
        bool flag=1;
        for(int i=0;i

D. Odd Queries

You have an array a1,a2,…,an Answer q queries of the following form:

  • If we change all elements in the range al,al+1,…of the array to k, will the sum of the entire array be odd?

Note that queries are independent and do not affect future queries.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.

The first line of each test case consists of 22 integers n and q (1≤n≤2⋅1051; 1≤q≤2⋅105 — the length of the array and the number of queries.

The second line of each test case consists of n integers a (1≤ai≤109) — the array a.

The next q lines of each test case consists of 33 integers l,r,k(1≤l≤r≤n1≤; 1≤k≤109) — the queries.

It is guaranteed that the sum of n over all test cases doesn't exceed 2⋅1052⋅105, and the sum of q doesn't exceed 2⋅1052⋅105.

Output

For each query, output "YES" if the sum of the entire array becomes odd, and "NO" otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Example

input

2

5 5

2 2 1 3 2

2 3 3

2 3 4

1 5 5

1 4 9

2 4 3

10 5

1 1 1 1 1 1 1 1 1 1

3 8 13

2 5 10

3 8 10

1 10 2

1 9 100

output

YES
YES
YES
NO
YES
NO
NO
NO
NO
YES

思路:

前缀和,然后各部分相加取模即可

代码

#include
using namespace std;
typedef long long ll;
int a[200005];
ll sum[200005];
int main(){
    int t;
    cin>>t;
    while(t--){
        int n,q;
        cin>>n>>q;
        sum[0]=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            sum[i]=a[i]+sum[i-1];
        }
        while(q--){
            ll l,r,k;
            cin>>l>>r>>k;
            ll t=(r-l+1)*k%2;
            ll ans=sum[l-1]+sum[n]-sum[r];
            if((ans+t)%2==1)cout<<"Yes"<

G2. Subsequence Addition (Hard Version)

The only difference between the two versions is that in this version, the constraints are higher.

Initially, array a contains just the number 11. You can perform several operations in order to change the array. In an operation, you can select some subsequence†† of a and add into a an element equal to the sum of all elements of the subsequence.

You are given a final array c. Check if c can be obtained from the initial array a by performing some number (possibly 0) of operations on the initial array.

†† A sequence b is a subsequence of a sequence a if b can be obtained from a by the deletion of several (possibly zero, but not all) elements. In other words, select k (1≤k≤|a|) distinct indices i1,i2,…,ik and insert anywhere into a a new element with the value equal to ai1+ai2+⋯+aik.

Input

The first line of the input contains an integer t (1≤t≤10000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n(1≤n≤2⋅105  — the number of elements the final array c should have.

The second line of each test case contains n space-separated integers ci(1≤ci≤2⋅105)  — the elements of the final array c that should be obtained from the initial array a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, output "YES" (without quotes) if such a sequence of operations exists, and "NO" (without quotes) otherwise.

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

Example

input

Copy

6

1

1

1

2

5

5 1 3 2 1

5

7 1 5 2 1

3

1 1 1

5

1 1 4 2 1

output

YES
NO
YES
NO
YES
YES

Note

For the first test case, the initial array a is already equal to [1][1], so the answer is "YES".

For the second test case, performing any amount of operations will change a to an array of size at least two which doesn't only have the element 22, thus obtaining the array [2][2] is impossible and the answer is "NO".

For the third test case, we can perform the following operations in order to obtain the final given array c:

  • Initially, a=[1].
  • By choosing the subsequence [1][1], and inserting 11 in the array, a changes to [1,1][1,1].
  • By choosing the subsequence [1,1][1,1], and inserting 1+1=21+1=2 in the middle of the array, a changes to [1,2,1][1,2,1].
  • By choosing the subsequence [1,2][1,2], and inserting 1+2=31+2=3 after the first 11 of the array, a changes to [1,3,2,1][1,3,2,1].
  • By choosing the subsequence [1,3,1][1,3,1] and inserting 1+3+1=51+3+1=5 at the beginning of the array, a changes to [5,1,3,2,1][5,1,3,2,1] (which is the array we needed to obtain).

思路

还是前缀和,如果c[i]小于等于前缀和,就合法,加进去,否则跳出

代码

#include
using namespace std;
typedef long long ll;
ll c[200005];
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>c[i];
        }
        sort(c+1,c+1+n);
        if(c[1]!=1){
            cout<<"No"<sum){
                cout<<"No"<

E. Interview

Before the last stage of the exam, the director conducted an interview. He gave Gon n piles of stones, the i-th pile having ai stones.

Each stone is identical and weighs 11 grams, except for one special stone that is part of an unknown pile and weighs 22 grams.

Codeforces Round 859 Div. 4题解_第1张图片

A picture of the first test case. Pile 22 has the special stone. The piles have weights of 1,3,3,4,51,3,3,4,5, respectively.

Gon can only ask the director questions of one kind: he can choose k piles, and the director will tell him the total weight of the piles chosen. More formally, Gon can choose an integer k (1≤k≤n) and kunique piles p1,p2,…,pk(1≤pi≤n), and the director will return the total weight mp1+mp2+⋯+mpk, where mi denotes the weight of pile i.

Gon is tasked with finding the pile that contains the special stone. However, the director is busy. Help Gon find this pile in at most 3030 queries.

Input

The input data contains several test cases. The first line contains one integer t (1≤t≤1000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤2⋅105) — the number of piles.

The second line of each test case contains n integers ai (1≤ai≤104) — the number of stones in each pile.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅1052⋅105.

After reading the input for each test case, proceed with the interaction as follows.

Interaction

You can perform the operation at most 3030 times to guess the pile.

To make a guess, print a line with the following format:

  • ? k p1 p2 p3 ... pk−1 pk?  (1≤k≤n; 1≤pi≤n; all pi are distinct) — the indices of the piles.

After each operation, you should read a line containing a single integer x — the sum of weights of the chosen piles. (Formally, x=mp1+mp2+⋯+mpk.)

When you know the index of the pile with the special stone, print one line in the following format: ! m!  (1≤m≤n).

After that, move on to the next test case, or terminate the program if there are no more test cases remaining.

If your program performs more than 3030 operations for one test case or makes an invalid query, you may receive a Wrong Answer verdict.

After you print a query or the answer, please remember to output the end of the line and flush the output. Otherwise, you may get Idleness limit exceeded or some other verdict. To do this, use the following:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see the documentation for other languages.

Hacks

To make a hack, use the following format.

The first line should contain a single integer t(1≤t≤1000) — the number of test cases.

The first line of each test case should contain two integers n,m (1≤n≤2⋅105) – the number of piles and the pile with the special stone.

The second line of each test case should contain n integers ai (1≤ai≤104) — the number of stones in each pile.

Note that the interactor is not adaptive, meaning that the answer is known before the participant asks the queries and doesn't depend on the queries asked by the participant.

Example

input

2
5
1 2 3 4 5

11

6

3

7
1 2 3 5 3 4 2

12

6

output

? 4 1 2 3 4

? 2 2 3

? 1 2

! 2

? 4 2 3 5 6

? 2 1 4

! 7

Note

In the first test case, the stone with weight two is located in pile 22, as shown in the picture. We perform the following interaction:

  • ? 4 1 2 3 4? 4 1 2 3 4 — ask the total weight of piles 11, 22, 33, and 44. The total weight we receive back is 1+3+3+4=11.
  • ? 2 2 3? 2 2 3 — ask the total weight of piles 22 and 33. The total weight we receive back is 3+3=63+3=6.
  • ? 1 2? 1 2 — ask the total weight of pile 22. The total weight we receive back is 33.
  • ! 2! 2 — we have figured out that pile 22 contains the special stone, so we output it and move on to the next test case.

In the second test case, the stone with weight two is located on index 77. We perform the following interaction:

  • ? 4 2 3 5 6? 4 2 3 5 6 — ask the total weight of piles 22, 33, 55, and 66. The total weight we receive back is 2+3+3+4=12.
  • ? 2 1 4? 2 1 4 — ask the total weight of piles 11 and 44. The total weight we receive back is 1+5=61+5=6.
  • ! 7! 7 — we have somehow figured out that pile 77 contains the special stone, so we output it and end the interaction.

思路:

第一次写交互题,有点懵, 下来看题解才明白,交互题说明输入的数据是根据 ”?” 猜测的数据而给定的,所以直接二分即可,每次将你猜测的区间告诉计算机,根据反馈数据判断区间

代码

#include
using namespace std;
int read(){
    int x=0,f=1;
    char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-')f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        x=(x<<1)+(x<<3)+(c^48);
        c=getchar();
    }
    return x*f;
}
int a[200005],pre[200005];
int check(int l,int r){
    cout<<"? "<<(r-l+1)<<' ';
    for(int i=l;i<=r;i++)cout<>l;
    return l;
}
void solve(){
    int n;
    n=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
        pre[i]=pre[i-1]+a[i];
    }
    int l=1,r=n;
    while(l>t;
    while(t--){
        solve();
        cout.flush();
    }
    return 0;
}

F. Bouncy Ball

You are given a room that can be represented by a n×m grid. There is a ball at position (i1,j1)(the intersection of row i1 and column j1), and it starts going diagonally in one of the four directions:

  • The ball is going down and right, denoted by DRDR; it means that after a step, the ball's location goes from (i,j)( to (i+1,j+1)
  • The ball is going down and left, denoted by DLDL; it means that after a step, the ball's location goes from (i,j)() to (i+1,j−1.
  • The ball is going up and right, denoted by URUR; it means that after a step, the ball's location goes from (i,j)() to (i−1,j+1)(
  • The ball is going up and left, denoted by ULUL; it means that after a step, the ball's location goes from (i,j)) to (i−1,j−1.

After each step, the ball maintains its direction unless it hits a wall (that is, the direction takes it out of the room's bounds in the next step). In this case, the ball's direction gets flipped along the axis of the wall; if the ball hits a corner, both directions get flipped. Any instance of this is called a bounce. The ball never stops moving.

Codeforces Round 859 Div. 4题解_第2张图片

In the above example, the ball starts at (1,7)and goes DLDL until it reaches the bottom wall, then it bounces and continues in the direction ULUL. After reaching the left wall, the ball bounces and continues to go in the direction URUR. When the ball reaches the upper wall, it bounces and continues in the direction DRDR. After reaching the bottom-right corner, it bounces once and continues in direction ULUL, and so on.

Your task is to find how many bounces the ball will go through until it reaches cell (i2,j2)in the room, or report that it never reaches cell (i2,j2)by printing −1−1.

Note that the ball first goes in a cell and only after that bounces if it needs to.

Input

The first line contains a single integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains six integers and a string n,m,i1,j1,i2,j2,d(2≤n,m≤25000; 1≤i1,i2≤n; 1≤j1,j2≤m; d∈{DR,DL,UR,UL}) — the dimensions of the grid, the starting coordinates of the ball, the coordinates of the final cell and the starting direction of the ball.

It is guaranteed that the sum of n⋅m over all test cases does not exceed 5⋅1045⋅104.

Output

For each test case, output a single integer — the number of bounces the ball does until it reaches cell (i2,j2) for the first time, or −1−1 if the ball never reaches the final cell.

Example

input

6

5 7 1 7 2 4 DL

5 7 1 7 3 2 DL

3 3 1 3 2 2 UR

2 4 2 1 2 2 DR

4 3 1 1 1 3 UL

6 4 1 2 3 4 DR

output

3
-1
1
-1
4
0

思路

纯模拟,考虑各种边界情况,ex题

代码

#include
using namespace std;
int n,m,x1,x2,y1,y2,f,num,cnt;
void check(int x,int y,int t){
    if(x==n&&y==m){
        if(t!=4)num++;
        f=4;
        return;
    }
    if(x==1&&y==1){
        if(t!=1)num++;
        f=1;
        return;
    }
    if(x==n&&y==1){
        if(t!=3)num++;
        f=3;
        return;
    }
    if(x==1&&y==m){
        if(t!=2)num++;
        f=2;
        return;
    }
    if(y==1&&t==4){
        num++;
        f=3;
        return;
    }
    if(y==1&&t==2){
        num++;
        f=1;
        return;
    }
    if(y==m&&t==1){
        num++;
        f=2;
        return;
    }
    if(y==m&&t==3){
        num++;
        f=4;
        return;
    }
    if(x==1&&t==3){
        num++;
        f=1;
        return;
    }
    if(x==1&&t==4){
        num++;
        f=2;
        return;
    }
    if(x==n&&t==1){
        num++;
        f=3;
        return;
    }
    if(x==n&&t==2){
        num++;
        f=4;
        return;
    }
}
int x,y;
int d[5][2]={0,0,1,1,1,-1,-1,1,-1,-1};
int main(){
    int t;
    cin>>t;
    while(t--){
        string s;
        bool flag=0;
        cin>>n>>m>>x1>>y1>>x2>>y2>>s;
        if(s=="DR")f=1;
        if(s=="DL")f=2;
        if(s=="UR")f=3;
        if(s=="UL")f=4;
        num=0,cnt=0;
        x=x1,y=y1;
        while(1){
           // cout<=5)break;    //五种方向各走一遍
            check(x,y,f);   //改变方向
            x=x+d[f][0];    //走一步
            y=y+d[f][1];
        }
        if(flag)cout<

 

 

你可能感兴趣的:(算法,c++,数据结构)