2020.06.09——习题训练五题解

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 kk distinct positive odd (not divisible by 22) integers or not.
You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤1051≤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 (1≤n,k≤1071≤n,k≤107).OutputFor each test case, print the answer — “YES” (without quotes) if nn can be represented as a sum of kk distinct 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

题意

给出一个数n,要求用k个奇数组成n。

思路

数n与k必须同时满足同是奇数或同是偶数否则无法构成,其次n应该大于k个奇数最小和。k=1,sum=1.k=2,sum=4……k个偶数最小和为k^2.

代码

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--){
        long long n,k;
        cin>>n>>k;
        if(n%2!=k%2||n<k*k)cout<<"NO"<<endl;   //直接找不符合条件的。
        else cout<<"YES"<<endl;
    }
    return 0;
}

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.InputThe first line contains a single integer tt

(1≤t≤1051≤t≤105

) — the number of test cases.Then tt

test cases follow.The first line of each test case contains a single integer nn

(1≤n≤1051≤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

(0≤k≤n0≤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]

(1≤gi[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

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

题意

n个公主选王子嫁,并且相应的公主有希望嫁的,再按照意愿为优先选择的基础,如果按照意愿可以分配正好输出OPTIMAL,否则输出IMPROVE,并找出多余的任意一位公主,输出她的分配。

思路

分别存储公主王子的婚配情况,在存储公主们的意愿。最后遍历输出。

代码

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int peo=1e5+10;
int gm[peo],mm[peo],hope[peo];
int main()
{
    int t;
    cin>>t;
    while(t--){
       int n,k,i,j;
       cin>>n; 	//n个公主
       for(i=1;i<=n;i++){
        cin>>k;		//k个意愿。
        for(j=1;j<=k;j++)cin>>hope[j];
        for(j=1;j<=k;j++){
            if(mm[hope[j]]==0){	//按照意愿检查是否婚配。
                mm[hope[j]]=1;		//改变婚配情况。
                gm[i]=1;
                break;
            }
        }
       }
       int sim=1;	//用来判断是否输出不用再次匹配
       for(i=1;i<=n;i++){
        if(gm[i]==0){		//找到未婚配的公主。
            sim=0;
            cout<<"IMPROVE"<<endl;
            for(j=1;j<=n;j++){
                if(mm[j]==0){
                    cout<<i<<" "<<j<<endl;
                    break;
                }
            }
            break;
        }
       }
       if(sim==1)cout<<"OPTIMAL"<<endl;
       for(i=1;i<=n;i++)gm[i]=mm[i]=hope[i]=0;		//重置数据防止混乱下一个样例。
    }
    return 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.InputThe first line contains a single integer tt

(1≤t≤100)(1≤t≤100)

— the number of testcases.Each testcase consists of one line containing a single integer, xx

(2≤x≤109)(2≤x≤109)

.OutputFor each testcase, output a pair of positive integers aa

and bb

(1≤a,b≤109)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 4

题意

根据所给的数n,找到两个数,这两个数的最小公倍数最大公约数的和为n,输出这两个数。

思路

1.如果是奇数输出1和n-1.
2.如果是偶数输出2和n-2.
特判:n=2时输出1,1.

代码

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--){
        long long x;
        int i;
        cin>>x;
        if(x==2){
            cout<<"1 1"<<endl;
            continue;
        }
        if(x%2==0){
            cout<<"2 "<<x-2<<endl;
            continue;}
        else{
            cout<<"1 "<<x-1<<endl;
    }
    }
    return 0;
}

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

(1≤n≤1051≤n≤105

) — the number of elements in the array aa

.The second line contains nn

space-separated integers a1a1

, a2a2

, ……

, anan

(1≤ai≤1091≤ai≤109

) — the elements of the array aa

.The sum of nn

across the test cases doesn’t exceed 105105

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

题意

长度为n的数组,可以连续复制n次还可以删除任意成员。找到最长的递增子数列。

思路

只需要利用map找到不同成员的种类数输出即可。(相当于复制n次每次只留一个删掉不需要的)

代码

#include<iostream>
#include<cmath>
#include<map>
using namespace std;
map<int,int> m;
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n,i,a;
        cin>>n;
        m.clear();	//初始化同时清除上一个样例的数据。
        for(i=1;i<=n;i++){
            cin>>a;
            if(m[a]==0)m[a]=1;
        }
        cout<<m.size()<<endl;
    }
    return 0;
}

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
You place one figure 2×12×1

(choose some ii

from 11

to nn

and replace aiai

with ai+2ai+2

); then, while all aiai

are greater than zero, replace each aiai

with ai−1ai−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.InputThe first line of the input contains one integer tt

(1≤t≤1001≤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

(1≤n≤1001≤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

(1≤ai≤1001≤ai≤100

), where aiai

is the initial height of the ii

-th column of the Tetris field.OutputFor 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

题意

俄罗斯方块,n列初始高度已知,只能放置宽度为1,长度为2的倍数的方块。是否最后能消除干净。

思路

只需要判断初始高度中有没有奇数偶数不一致的情况。

代码

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n,i,a,sum=0;
        cin>>n;
       for(i=1;i<=n;i++){
        cin>>a;
        if(a%2==0)sum++;
       }
       if(sum==0||sum==n)cout<<"YES"<<endl;
       else cout<<"NO"<<endl;
    }
    return 0;
}

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=an−i−1ai=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.InputThe first line of the input contains one integer tt

(1≤t≤1001≤t≤100

) — the number of test cases.Next 2t2t

lines describe test cases. The first line of the test case contains one integer nn

(3≤n≤50003≤n≤5000

) — the length of aa

. The second line of the test case contains nn

integers a1,a2,…,ana1,a2,…,an

(1≤ai≤n1≤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

(∑n≤5000∑n≤5000

).OutputFor 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

题意

在一个字符串中,能否找到一个长度为3的回文子串

思路

找到不相邻的两个相同的字符。

代码

#include<iostream>
#include<cmath>
using namespace std;
const int N=5010;
int num[N];
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n,i,j;
        cin>>n;
       for(i=1;i<=n;i++){
        cin>>num[i];
       }
       int sim=0;
       for(i=1;i<=n-2;i++){
        for(j=i+2;j<=n;j++){
            if(num[i]==num[j]){
            sim=1;
            break;
        }}
        if(sim==1)break;
       }
       if(sim==1)cout<<"YES"<<endl;
       else cout<<"NO"<<endl;
    }
    return 0;
}

H - Frog Jumps

There is a frog staying to the left of the string s=s1s2…sns=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 dd

before 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,i−d);i−1][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.InputThe first line of the input contains one integer tt

(1≤t≤1041≤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 2⋅1052⋅105

characters ‘L’ and ‘R’.It is guaranteed that the sum of lengths of strings over all test cases does not exceed 2⋅1052⋅105

(∑|s|≤2⋅105∑|s|≤2⋅105

).OutputFor 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

题意

青蛙每个位置标有L,R表示青蛙只能向左和向右跳,每次最多能跳m个长度,求m的最小值

思路

找到最长L有多长,加一输出。

代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--){
       int sum=0,i,maxL=0;
       string s;
       cin>>s;
       for(i=0;i<s.length();i++){
        if(s[i]=='L'){
        sum++;
        maxL=max(maxL,sum);
        }
        else sum=0;
       }
       cout<<maxL+1<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值