2020.6.8训练题

A - Sum of Odd Integers

You are given two integers nn and kk. Your task is to find if nn can be represented as a sum of kdistinct positive odd (not divisible by 22) integers or not.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1t1051≤t≤105) — the number of test cases.

The next tt lines describe test cases. The only line of the test case contains two integers nn and kk (1n,k1071≤n,k≤107).

Output

For each test case, print the answer — "YES" (without quotes) if nn can be represented as a sum of kdistinct positive odd (not divisible by 22) integers and "NO" otherwise.

Example

Input
6
3 1
4 2
10 3
10 2
16 4
16 5
Output
YES
YES
NO
YES
YES
NO

Note

In the first test case, you can represent 33 as 33.

In the second test case, the only way to represent 44 is 1+31+3.

In the third test case, you cannot represent 1010 as the sum of three distinct positive odd integers.

In the fourth test case, you can represent 1010 as 3+73+7, for example.

In the fifth test case, you can represent 1616 as 1+3+5+71+3+5+7.

In the sixth test case, you cannot represent 1616 as the sum of five distinct positive odd integers.

思路:本题要求用k个奇数表示整数n。直接先让k-1个从1开始的奇数相加,得到和sum,如果n-sum>2(k-1),并且n-sum是奇数就输出yes,否则输出no。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll nl=1e5+5;
ll a[nl];
int main(){ ll t; cin>>t;
    while(t--){
        ll n,k;
        cin>>n;
        cin>>k;
        ll num;
        ll sum;
        sum=(k-1)*(k-1);
        num=n-sum;
        if((num>2*(k-1))&&(num%2!=0)){
            cout<<"YES"<<endl;
        }else{
            cout<<"NO"<<endl;
         }
        }
}

B - Princesses and Princes

 

The King of Berland Polycarp LXXXIV has nn daughters. To establish his power to the neighbouring kingdoms he wants to marry his daughters to the princes of these kingdoms. As a lucky coincidence there are nn other kingdoms as well.

So Polycarp LXXXIV has enumerated his daughters from 11 to nn and the kingdoms from 11 to nn. For each daughter he has compiled a list of kingdoms princes of which she wanted to marry.

Polycarp LXXXIV is very busy, so he finds a couple for his daughters greedily one after another.

For the first daughter he takes the kingdom with the lowest number from her list and marries the daughter to their prince. For the second daughter he takes the kingdom with the lowest number from her list, prince of which hasn't been taken already. If there are no free princes in the list then the daughter marries nobody and Polycarp LXXXIV proceeds to the next daughter. The process ends after the nn-th daughter.

For example, let there be 44 daughters and kingdoms, the lists daughters have are [2,3][2,3], [1,2][1,2], [3,4][3,4], [3][3], respectively.

 

 

In that case daughter 11 marries the prince of kingdom 22, daughter 22 marries the prince of kingdom 11, daughter 33 marries the prince of kingdom 33, leaving daughter 44 nobody to marry to.

Actually, before starting the marriage process Polycarp LXXXIV has the time to convince one of his daughters that some prince is also worth marrying to. Effectively, that means that he can add exactly one kingdom to exactly one of his daughter's list. Note that this kingdom should not be present in the daughter's list.

Polycarp LXXXIV wants to increase the number of married couples.

Unfortunately, what he doesn't have the time for is determining what entry to add. If there is no way to increase the total number of married couples then output that the marriages are already optimal. Otherwise, find such an entry that the total number of married couples increases if Polycarp LXXXIV adds it.

If there are multiple ways to add an entry so that the total number of married couples increases then print any of them.

For your and our convenience you are asked to answer tt independent test cases.

Input

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

Then tt test cases follow.

The first line of each test case contains a single integer nn (1n1051≤n≤105) — the number of daughters and the number of kingdoms.

Each of the next nn lines contains the description of each daughter's list. The first integer kk (0kn0≤k≤n) is the number of entries in the ii-th daughter's list. After that kk distinct integers follow gi[1],gi[2],,gi[k]gi[1],gi[2],…,gi[k] (1gi[j]n1≤gi[j]≤n) — the indices of the kingdoms in the list in the increasing order (gi[1]<gi[2]<<gi[k]gi[1]<gi[2]<⋯<gi[k]).

It's guaranteed that the total number of daughters over all test cases does not exceed 105105.

It's also guaranteed that the total number of kingdoms in lists over all test cases does not exceed 105105.

Output

For each test case print the answer to it.

Print "IMPROVE" in the first line if Polycarp LXXXIV can add some kingdom to some of his daughter's list so that the total number of married couples increases. The second line then should contain two integers — the index of the daughter and the index of the kingdom Polycarp LXXXIV should add to that daughter's list.

If there are multiple ways to add an entry so that the total number of married couples increases then print any of them.

Otherwise the only line should contain one word "OPTIMAL".

Example

Input
5
4
2 2 3
2 1 2
2 3 4
1 3
2
0
0
3
3 1 2 3
3 1 2 3
3 1 2 3
1
1 1
4
1 1
1 2
1 3
1 4
Output
IMPROVE
4 4
IMPROVE
1 1
OPTIMAL
OPTIMAL
OPTIMAL

Note

The first test case is depicted in the statement. Adding the fourth kingdom to the list of the fourth daughter makes her marry the prince of the fourth kingdom.

In the second test case any new entry will increase the number of marriages from 00 to 11.

In the third and the fourth test cases there is no way to add an entry.

In the fifth test case there is no way to change the marriages by adding any entry.

思路:先判断新娘是否有王子与其相配,如果没有则记录一下,最后将没有相配的王子与其相配。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll nl=1e5+5;
ll a[nl],b[nl],c[nl]={0};
int main(){
       ll t;
       cin>>t;
       while(t--){
        ll n,i;
        cin>>n;
        ll k,j,temp,m=0,z,d;
        for(i=0;i<n;i++){
            cin>>k;
            temp=1;
           for(j=0;j<k;j++){
            cin>>d;
            if(temp==1){
                if(c[d]==0){
                c[d]=1;
                temp=0;
                }
            }
           }
            if(temp==1){
                m=1;
                z=i+1;
            }
           }
           if(m==0){
            cout<<"OPTIMAL"<<endl;
           }else{
            cout<<"IMPROVE"<<endl;
            cout<<z<<" ";
           }
           for(i=1;i<=n;i++){
            if(c[i]==0){
                cout<<i<<endl;
                break;
            }
           }
           for(i=0;i<=n;i++){
            c[i]=0;
           }
        }
       }
 

C - EhAb AnD gCd

You are given a positive integer xx. Find any such 22 positive integers aa and bb such that GCD(a,b)+LCM(a,b)=xGCD(a,b)+LCM(a,b)=x.

As a reminder, GCD(a,b)GCD(a,b) is the greatest integer that divides both aa and bb. Similarly, LCM(a,b)LCM(a,b) is the smallest integer such that both aa and bb divide it.

It's guaranteed that the solution always exists. If there are several such pairs (a,b)(a,b), you can output any of them.

Input

The first line contains a single integer t(1t100)(1≤t≤100)  — the number of testcases.

Each testcase consists of one line containing a single integer, x(2x109)(2≤x≤109).

Output

For each testcase, output a pair of positive integers aa and bb (1a,b109)1≤a,b≤109) such that GCD(a,b)+LCM(a,b)=xGCD(a,b)+LCM(a,b)=x. It's guaranteed that the solution always exists. If there are several such pairs (a,b)(a,b), you can output any of them.

Example

Input
2
2
14
Output
1 1
6 4ple, GCD(1,1)+LCM(1,1)=1+1=2GCD(1,1)+LCM(1,1)=1+1=2.

In the second testcase of the sample, GCD(6,4)+LCM(6,4)=2+12=14GCD(6,4)+LCM(6,4)=2+12=14.

思路:由题目知要求n=GCD(a,b)+LCM(a,b),观察可以发现当a=1,b=n-1时即可满足条件。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll nl=1e5+5;
ll a[nl];
int main(){ ll t; cin>>t;
    while(t--){
        ll n;
        cin>>n;
        ll a,b;
        cout<<"1"<<" "<<n-1<<endl;
        }
}

 

 

D - CopyCopyCopyCopyCopy

Ehab has an array aa of length nn. He has just enough free time to make a new array consisting of nn copies of the old array, written back-to-back. What will be the length of the new array's longest increasing subsequence?

A sequence aa is a subsequence of an array bb if aa can be obtained from bb by deletion of several (possibly, zero or all) elements. The longest increasing subsequence of an array is the longest subsequence such that its elements are ordered in strictly increasing order.

Input

The first line contains an integer tt — the number of test cases you need to solve. The description of the test cases follows.

The first line of each test case contains an integer nn (1n1051≤n≤105) — the number of elements in the array aa.

The second line contains nn space-separated integers a1a1, a2a2, …, anan (1ai1091≤ai≤109) — the elements of the array aa.

The sum of nn across the test cases doesn't exceed 105105.

Output

For each testcase, output the length of the longest increasing subsequence of aa if you concatenate it to itself nn times.

Example

Input
2
3
3 2 1
6
3 1 4 1 5 9
Output
3
5

Note

In the first sample, the new array is [3,2,1,3,2,1,3,2,1][3,2,1,3,2,1,3,2,1]. The longest increasing subsequence is marked in bold.

In the second sample, the longest increasing subsequence will be [1,3,4,5,9][1,3,4,5,9].

思路:使用map记录数组中共有多少不同的数字。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll nl=1e5+5;
map<ll,ll>m;
ll a[nl];
int main(){
   ll t;
   cin>>t;
   while(t--){
   ll n;
   cin>>n;
   ll i;
   m[0]=1;
   ll num=0;
   for(i=1;i<=n;i++){
   cin>>a[i];
   if(m.count(a[i])){
        continue;
   }else{
        m[a[i]]=1;
        num++;
   }
}
cout<<num<<endl;
   m.clear();
   }
}
E - Ehab and Path-etic MEXs
 

You are given a tree consisting of nn nodes. You want to write some labels on the tree's edges such that the following conditions hold:

  • Every label is an integer between 00 and n2n−2 inclusive.
  • All the written labels are distinct.
  • The largest value among MEX(u,v)MEX(u,v) over all pairs of nodes (u,v)(u,v) is as small as possible.

Here, MEX(u,v)MEX(u,v) denotes the smallest non-negative integer that isn't written on any edge on the unique simple path from node uu to node vv.

Input

The first line contains the integer nn (2n1052≤n≤105) — the number of nodes in the tree.

Each of the next n1n−1 lines contains two space-separated integers uu and vv (1u,vn1≤u,v≤n) that mean there's an edge between nodes uu and vv. It's guaranteed that the given graph is a tree.

Output

Output n1n−1 integers. The ithith of them will be the number written on the ithith edge (in the input order).

Examples
Input
3
1 2
1 3
Output
0
1
Input
6
1 2
1 3
2 4
2 5
5 6
Output
0
3
2
4
1
Note

The tree from the second sample:

 

 思路:先确定只有一个节点的情况,将其设为最小,其余随意。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int nl=1e5+5;
ll a[nl],b[nl],c[nl];
map<ll,ll>m;
int main(){
    ll n;
    cin>>n;
    ll i;
    for(i=1;i<=n-1;i++){
        cin>>a[i]>>b[i];
        m[a[i]]++;
        m[b[i]]++;
        c[i]=-1;
    }
    ll j=n-2;
    for(i=1;i<=n-1;i++){
        if(m[a[i]]==1||m[b[i]]==1){
            continue;
        }else{
            c[i]=j;
            j--;
        }
    }
    for(i=1;i<=n-1;i++){
        if(c[i]==-1){
            c[i]=j;
            j--;
        }
    }
    for(i=1;i<=n-1;i++){
        cout<<c[i]<<endl;
    }
}
 
F - Yet Another Tetris Problem

You are given some Tetris field consisting of nn columns. The initial height of the ii-th column of the field is aiai blocks. On top of these columns you can place only figures of size 2×12×1 (i.e. the height of this figure is 22 blocks and the width of this figure is 11 block). Note that you cannot rotate these figures.

Your task is to say if you can clear the whole field by placing such figures.

More formally, the problem can be described like this:

The following process occurs while at least one aiai is greater than 00:

  1. You place one figure 2×12×1 (choose some ii from 11 to nn and replace aiai with ai+2ai+2);
  2. then, while all aiai are greater than zero, replace each aiai with ai1ai−1.

And your task is to determine if it is possible to clear the whole field (i.e. finish the described process), choosing the places for new figures properly.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1t1001≤t≤100) — the number of test cases.

The next 2t2t lines describe test cases. The first line of the test case contains one integer nn (1n1001≤n≤100) — the number of columns in the Tetris field. The second line of the test case contains nn integers a1,a2,,ana1,a2,…,an (1ai1001≤ai≤100), where aiai is the initial height of the ii-th column of the Tetris field.

Output

For each test case, print the answer — "YES" (without quotes) if you can clear the whole Tetris field and "NO" otherwise.

Example
Input
4
3
1 1 3
4
1 1 2 1
2
11 11
1
100
Output
YES
NO
YES
YES
Note

The first test case of the example field is shown below:

 

 

Gray lines are bounds of the Tetris field. Note that the field has no upper bound.

One of the correct answers is to first place the figure in the first column. Then after the second step of the process, the field becomes [2,0,2][2,0,2]. Then place the figure in the second column and after the second step of the process, the field becomes [0,0,0][0,0,0].

And the second test case of the example field is shown below:

 

 

It can be shown that you cannot do anything to end the process.

In the third test case of the example, you first place the figure in the second column after the second step of the process, the field becomes [0,2][0,2]. Then place the figure in the first column and after the second step of the process, the field becomes [0,0][0,0].

In the fourth test case of the example, place the figure in the first column, then the field becomes [102][102] after the first step of the process, and then the field becomes [0][0] after the second step of the process.

思路:本题是通过题目规定的两个条件使图形消失,因为第二条是要全部列均减一,所以要想图形一起消失,就要通过条件一使图形各列都相等。若可以则yes,否则no。注意特例,即n=1的情况。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll nl=1e5+5;
ll a[nl],b[nl];
int main(){
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        ll i,num=0;
        for(i=1;i<=n;i++){
            cin>>a[i];
        }
        sort(a+1,a+n);
        if(n==1){
            cout<<"YES"<<endl;
            continue;
        }
        for(i=1;i<=n-1;i++){
            b[i]=a[n]-a[i];
        }
        for(i=1;i<=n-1;i++){
            if(b[i]%2==0){
                num=1;
            }else{
                num=0;
                break;
            }
        }
        if(num==1){
            cout<<"YES"<<endl;
        }else{
            cout<<"NO"<<endl;
        }
    }
}
 G - Yet Another Palindrome Problem

You are given an array aa consisting of nn integers.

Your task is to determine if aa has some subsequence of length at least 33 that is a palindrome.

Recall that an array bb is called a subsequence of the array aa if bb can be obtained by removing some (possibly, zero) elements from aa (not necessarily consecutive) without changing the order of remaining elements. For example, [2][2], [1,2,1,3][1,2,1,3] and [2,3][2,3] are subsequences of [1,2,1,3][1,2,1,3], but [1,1,2][1,1,2] and [4][4] are not.

Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array aa of length nn is the palindrome if ai=ani1ai=an−i−1 for all ii from 11 to nn. For example, arrays [1234][1234], [1,2,1][1,2,1], [1,3,2,2,3,1][1,3,2,2,3,1] and [10,100,10][10,100,10] are palindromes, but arrays [1,2][1,2] and [1,2,3,1][1,2,3,1] are not.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1t1001≤t≤100) — the number of test cases.

Next 2t2t lines describe test cases. The first line of the test case contains one integer nn (3n50003≤n≤5000) — the length of aa. The second line of the test case contains nn integers a1,a2,,ana1,a2,…,an (1ain1≤ai≤n), where aiai is the ii-th element of aa.

It is guaranteed that the sum of nn over all test cases does not exceed 50005000 (n5000∑n≤5000).

Output

For each test case, print the answer — "YES" (without quotes) if aa has some subsequence of length at least 33 that is a palindrome and "NO" otherwise.

Example
Input
5
3
1 2 1
5
1 2 2 3 2
3
1 1 2
4
1 2 2 1
10
1 1 2 2 3 3 4 4 5 5
Output
YES
YES
NO
YES
NO
Note

In the first test case of the example, the array aa has a subsequence [1,2,1][1,2,1] which is a palindrome.

In the second test case of the example, the array aa has two subsequences of length 33 which are palindromes: [2,3,2][2,3,2] and [2,2,2][2,2,2].

In the third test case of the example, the array aa has no subsequences of length at least 33 which are palindromes.

In the fourth test case of the example, the array aa has one subsequence of length 44 which is a palindrome: [1,2,2,1][1,2,2,1] (and has two subsequences of length 33 which are palindromes: both are [1,2,1][1,2,1]).

In the fifth test case of the example, the array aa has no subsequences of length at least 33 which are palindromes.

思路:本题利用map判断是否前后有重复数,并且重复数之间要有数字间隔。本题我错误了一次原因是忘记判断三数连续相同的情况。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll nl=1e5+5;
ll a[nl],b[nl];
map<ll,ll>m;
int main(){
    ll t;
    cin>>t;
    while(t--){
        ll n,num=0,i;
        cin>>n;
        m[0]=1;
        for(i=1;i<=n;i++){
            cin>>a[i];
            if(m.count(a[i])){
                if(a[i-1]!=a[i]||a[i]==a[i-2]){
                    num=1;
                }
            }else{
                m[a[i]]=1;
            }
        }
        if(num==1){
            cout<<"YES"<<endl;
        }else{
            cout<<"NO"<<endl;
        }
        m.clear();
    }
}

H - Frog Jumps

There is a frog staying to the left of the string s=s1s2sns=s1s2…sn consisting of nn characters (to be more precise, the frog initially stays at the cell 00). Each character of ss is either 'L' or 'R'. It means that if the frog is staying at the ii-th cell and the ii-th character is 'L', the frog can jump only to the left. If the frog is staying at the ii-th cell and the ii-th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell 00.

Note that the frog can jump into the same cell twice and can perform as many jumps as it needs.

The frog wants to reach the n+1n+1-th cell. The frog chooses some positive integer value dbefore the first jump (and cannot change it later) and jumps by no more than dd cells at once. I.e. if the ii-th character is 'L' then the frog can jump to any cell in a range [max(0,id);i1][max(0,i−d);i−1], and if the ii-th character is 'R' then the frog can jump to any cell in a range [i+1;min(n+1;i+d)][i+1;min(n+1;i+d)].

The frog doesn't want to jump far, so your task is to find the minimum possible value of dd such that the frog can reach the cell n+1n+1 from the cell 00 if it can jump by no more than dd cells at once. It is guaranteed that it is always possible to reach n+1n+1 from 00.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1t1041≤t≤104) — the number of test cases.

The next tt lines describe test cases. The ii-th test case is described as a string ss consisting of at least 11 and at most 21052⋅105 characters 'L' and 'R'.

It is guaranteed that the sum of lengths of strings over all test cases does not exceed 21052⋅105 (|s|2105∑|s|≤2⋅105).

Output

For each test case, print the answer — the minimum possible value of dd such that the frog can reach the cell n+1n+1 from the cell 00 if it jumps by no more than dd at once.

Example
Input
6
LRLRRLL
L
LLR
RRRR
LLLLLL
R
Output
3
2
3
1
7
1
Note

The picture describing the first test case of the example and one of the possible answers:

 

 

In the second test case of the example, the frog can only jump directly from 00 to n+1n+1.

In the third test case of the example, the frog can choose d=3d=3, jump to the cell 33 from the cell 00 and then to the cell 44 from the cell 33.

In the fourth test case of the example, the frog can choose d=1d=1 and jump 55 times to the right.

In the fifth test case of the example, the frog can only jump directly from 00 to n+1n+1.

In the sixth test case of the example, the frog can choose d=1d=1 and jump 22 times to the right.

思路:本题要求青蛙跳一次的最短距离。可以利用数组记录R出现的位置,在用另一个数组记录R和首尾之间的距离,最后输出最大距离即可。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll nl=2e5+5;
ll b[nl],c[nl];
int main(){
    ll n;
    cin>>n;
    while(n--){
        string a;
        cin>>a;
        ll t;
        t=a.size();
        ll i,j=1;
        b[0]=0;
        for(i=0;i<t;i++){
            if(a[i]=='R'){
                b[j]=i+1;
                j++;
            }
        }
        b[j]=t+1;
        for(i=0;i<=j-1;i++){
            c[i]=b[i+1]-b[i];
        }
        sort(c,c+j);
        cout<<c[j-1]<<endl;
    }

}


 
posted @ 2020-06-10 10:31  yyscn  阅读(211)  评论(0)    收藏  举报