第四次练习6.1

A - Dreamoon and Ranking Collection

Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from 11

to 5454

after two more rated contests. It’s amazing!Based on this, you come up with the following problem:There is a person who participated in nn

Codeforces rounds. His place in the first round is a1a1

, his place in the second round is a2a2

, …, his place in the nn

-th round is anan

.You are given a positive non-zero integer xx

.Please, find the largest vv

such that this person can collect all the places from 11

to vv

after xx

more rated contests.In other words, you need to find the largest vv

, such that it is possible, that after xx

more rated contests, for each 1≤i≤v1≤i≤v

, there will exist a contest where this person took the ii

-th place.For example, if n=6n=6

, x=2x=2

and a=[3,1,1,5,7,10]a=[3,1,1,5,7,10]

then answer is v=5v=5

, because if on the next two contest he will take places 22

and 44

, then he will collect all places from 11

to 55

, so it is possible to get v=5v=5

.InputThe first line contains an integer tt

(1≤t≤51≤t≤5

) denoting the number of test cases in the input.Each test case contains two lines. The first line contains two integers n,xn,x

(1≤n,x≤1001≤n,x≤100

). The second line contains nn

positive non-zero integers a1,a2,…,ana1,a2,…,an

(1≤ai≤1001≤ai≤100

).OutputFor each test case print one line containing the largest vv

, such that it is possible that after xx

other contests, for each 1≤i≤v1≤i≤v

, there will exist a contest where this person took the ii

-th place.Example Input 5
6 2
3 1 1 5 7 10
1 100
100
11 1
1 1 1 1 1 1 1 1 1 1 1
1 1
1
4 57
80 60 40 20
Output 5
101
2
2
60

题意

给出n个数,x次补充机会,最多能构成最长的数列(数列从1开始递增)。

思路

只需要构造一个数列初始为0,读取一个数改变为1,遍历遇到0,x递减。输出即可。

代码

#include<iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n,x,i,ni;
        cin>>n>>x;
        int num[1000]={0};
        for(i=1;i<=n;i++){
            cin>>ni;
            num[ni]=1;	//找已知的数字。
        }
        for(i=1;x;i++){
            if(num[i]==0){
                num[i]=1;	//消耗x。
                x--;
            }
        }
        int maxi;
        for(i=1;;i++){
            if(num[i]==0){	//按顺序开始寻找最大值。
                maxi=i-1;
                break;
            }
        }
        cout<<maxi<<endl;
    }
    return 0;
}

B - Dreamoon Likes Permutations

The sequence of mm

integers is called the permutation if it contains all integers from 11

to mm

exactly once. The number mm

is called the length of the permutation.Dreamoon has two permutations p1p1

and p2p2

of non-zero lengths l1l1

and l2l2

.Now Dreamoon concatenates these two permutations into another sequence aa

of length l1+l2l1+l2

. First l1l1

elements of aa

is the permutation p1p1

and next l2l2

elements of aa

is the permutation p2p2

. You are given the sequence aa

, and you need to find two permutations p1p1

and p2p2

. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)InputThe first line contains an integer tt

(1≤t≤100001≤t≤10000

) denoting the number of test cases in the input.Each test case contains two lines. The first line contains one integer nn

(2≤n≤2000002≤n≤200000

): the length of aa

. The second line contains nn

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

(1≤ai≤n−11≤ai≤n−1

).The total sum of nn

is less than 200000200000

.OutputFor each test case, the first line of output should contain one integer kk
the number of ways to divide aa

into permutations p1p1

and p2p2

.Each of the next kk

lines should contain two integers l1l1

and l2l2

(1≤l1,l2≤n,l1+l2=n1≤l1,l2≤n,l1+l2=n

), denoting, that it is possible to divide aa

into two permutations of length l1l1

and l2l2

(p1p1

is the first l1l1

elements of aa

, and p2p2

is the last l2l2

elements of aa

). You can print solutions in any order.Example Input 6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output 2
1 4
4 1
1
4 2
0
0
1
2 10
0

题意

给出一组长度为n的数列找到长度l1,l2(l1+l2=n)的子数组l1,l2含有从1递增到l1,l2的数组。

思路

先找到数列中最大的数ma,那么两次检查。
1.前ma与后n-ma是否符合。
2.前n-ma与后ma是否符合。
3.注意当2*ma=n,情况重复。

代码

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=2e5+5;
int a[maxn],vis[maxn],ans[5][5];
int t,cnt,n,ma;
bool check(int l1,int l2){
    for(int i=1;i<=n;i++) vis[i]=0;
    for(int i=1;i<=l1;i++) vis[a[i]]=1;
    for(int i=1;i<=l1;i++){
        if(vis[i]==0) return 0;
    }
    for(int i=1;i<=n;i++) vis[i]=0;
    for(int i=l1+1;i<=n;i++) vis[a[i]]=1;
    for(int i=1;i<=l2;i++){
        if(vis[i]==0){
            return 0;
        }
    }
    return 1;
}
int main()
{
    cin>>t;
    while(t--){
    cin>>n;
        ma=-1;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            ma=max(ma,a[i]);
        }
        cnt=0;
        if(check(ma,n-ma)){
            cnt++;
            ans[cnt][1]=ma;
            ans[cnt][2]=n-ma;
        }
        if(ma*2!=n&&check(n-ma,ma)){
            cnt++;
            ans[cnt][1]=n-ma;
            ans[cnt][2]=ma;
        }
        printf("%d\n",cnt);
        for(int i=1;i<=cnt;i++){
            printf("%d %d\n",ans[i][1],ans[i][2]);
        }
    }
    return 0;
}

C - Exercising Walk

Alice has a cute cat. To keep her cat fit, Alice wants to design an exercising walk for her cat! Initially, Alice’s cat is located in a cell (x,y)(x,y)

of an infinite grid. According to Alice’s theory, cat needs to move: exactly aa

steps left: from (u,v)(u,v)

to (u−1,v)(u−1,v)

; exactly bb

steps right: from (u,v)(u,v)

to (u+1,v)(u+1,v)

; exactly cc

steps down: from (u,v)(u,v)

to (u,v−1)(u,v−1)

; exactly dd

steps up: from (u,v)(u,v)

to (u,v+1)(u,v+1)

. Note that the moves can be performed in an arbitrary order. For example, if the cat has to move 11

step left, 33

steps right and 22

steps down, then the walk right, down, left, right, right, down is valid.Alice, however, is worrying that her cat might get lost if it moves far away from her. So she hopes that her cat is always in the area [x1,x2]×[y1,y2][x1,x2]×[y1,y2]

, i.e. for every cat’s position (u,v)(u,v)

of a walk x1≤u≤x2x1≤u≤x2

and y1≤v≤y2y1≤v≤y2

holds.Also, note that the cat can visit the same cell multiple times.Can you help Alice find out if there exists a walk satisfying her wishes?Formally, the walk should contain exactly a+b+c+da+b+c+d

unit moves (aa

to the left, bb

to the right, cc

to the down, dd

to the up). Alice can do the moves in any order. Her current position (u,v)(u,v)

should always satisfy the constraints: x1≤u≤x2x1≤u≤x2

, y1≤v≤y2y1≤v≤y2

. The staring point is (x,y)(x,y)

.You are required to answer tt

test cases independently.InputThe first line contains a single integer tt

(1≤t≤1031≤t≤103

) — the number of testcases. The first line of each test case contains four integers aa

, bb

, cc

, dd

(0≤a,b,c,d≤1080≤a,b,c,d≤108

, a+b+c+d≥1a+b+c+d≥1

).The second line of the test case contains six integers xx

, yy

, x1x1

, y1y1

, x2x2

, y2y2

(−108≤x1≤x≤x2≤108−108≤x1≤x≤x2≤108

, −108≤y1≤y≤y2≤108−108≤y1≤y≤y2≤108

).OutputFor each test case, output “YES” in a separate line, if there exists a walk satisfying her wishes. Otherwise, output “NO” in a separate line. You can print each letter in any case (upper or lower).Example Input 6
3 2 2 2
0 0 -2 -2 2 2
3 1 4 1
0 0 -1 -1 1 1
1 1 1 1
1 1 1 1 1 1
0 0 0 1
0 0 0 0 0 1
5 1 1 1
0 0 -100 -100 0 100
1 1 5 1
0 0 -100 -100 100 0
Output Yes
No
No
Yes
Yes
Yes

题意

给出的abcd分别代表一个初始点(x,y)左右下上移动的距离,但是移动过程与结果不能超过所给的(x1,y1)(x2,y2)范围,问所给范围是否成立。

思路

1.过程:判断x,y是否能移动如果存在x=x1=x2,代表x无法移动即使a=b,如果可以求出实际值。
2.结果:如果可移动abcd已经得到了最终实际的值判断结果是否越界。

代码

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int t,a,b,c,d,x,y,x1,y1,x2,y2;
    cin>>t;
    while(t--){
        cin>>a>>b>>c>>d;
        cin>>x>>y;
        cin>>x1>>y1;
        cin>>x2>>y2;
        int minx,miny;
        if(x>x1||x<x2){
        minx=min(a,b);
        a-=minx;
        b-=minx;
        }
        if(y>y1||y<y2){
            miny=min(c,d);
            c-=miny;
            d-=miny;
        }
        if(x-x1<a||x2-x<b||y-y1<c||y2-y<d){
            cout<<"No"<<endl;
        }
        else cout<<"Yes"<<endl;
    }
}

D - Composite Coloring

A positive integer is called composite if it can be represented as a product of two positive integers, both greater than 11

. For example, the following numbers are composite: 66

, 44

, 120120

, 2727

. The following numbers aren’t: 11

, 22

, 33

, 1717

, 9797

.Alice is given a sequence of nn

composite numbers a1,a2,…,ana1,a2,…,an

.She wants to choose an integer m≤11m≤11

and color each element one of mm

colors from 11

to mm

so that: for each color from 11

to mm

there is at least one element of this color; each element is colored and colored exactly one color; the greatest common divisor of any two elements that are colored the same color is greater than 11

, i.e. gcd(ai,aj)>1gcd(ai,aj)>1

for each pair i,ji,j

if these elements are colored the same color. Note that equal elements can be colored different colors — you just have to choose one of mm

colors for each of the indices from 11

to nn

.Alice showed already that if all ai≤1000ai≤1000

then she can always solve the task by choosing some m≤11m≤11

.Help Alice to find the required coloring. Note that you don’t have to minimize or maximize the number of colors, you just have to find the solution with some mm

from 11

to 1111

.InputThe first line contains a single integer tt

(1≤t≤10001≤t≤1000

) — the number of test cases. Then the descriptions of the test cases follow.The first line of the test case contains a single integer nn

(1≤n≤10001≤n≤1000

) — the amount of numbers in a sequence aa

.The second line of the test case contains nn

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

(4≤ai≤10004≤ai≤1000

).It is guaranteed that the sum of nn

over all test cases doesn’t exceed 104104

.OutputFor each test case print 22

lines. The first line should contain a single integer mm

(1≤m≤111≤m≤11

) — the number of used colors. Consider colors to be numbered from 11

to mm

. The second line should contain any coloring that satisfies the above conditions. Print nn

integers c1,c2,…,cnc1,c2,…,cn

(1≤ci≤m1≤ci≤m

), where cici

is the color of the ii

-th element. If there are multiple solutions then you can print any of them. Note that you don’t have to minimize or maximize the number of colors, you just have to find the solution with some mm

from 11

to 1111

.Remember that each color from 11

to mm

should be used at least once. Any two elements of the same color should not be coprime (i.e. their GCD should be greater than 11

).Example Input 3
3
6 10 15
2
4 9
23
437 519 865 808 909 391 194 291 237 395 323 365 511 497 781 737 871 559 731 697 779 841 961
Output 1
1 1 1
2
2 1
11
4 7 8 10 7 3 10 7 7 8 3 1 1 5 5 9 2 2 3 3 4 11 6

题意

给出一组数最小公约数不是1可以然为一种颜色最多11种

思路

利用map键指向记录不同公约数颜色种类。

代码

#include<iostream>
#include<cmath>
#include<map>
using namespace std;
map<int,int>mp;
int main()
{
    int t;
    cin>>t;
    while(t--){
        int num[1005],a,n,i,j;
        cin>>n;
        int coun=0;
        mp.clear();
        for(i=1;i<=n;i++){
            cin>>a;
            for(j=2;j<=sqrt(a);j++){
                if(a%j==0){
                    if(mp[j])num[i]=mp[j];
                    else {
                        coun++;
                        num[i]=coun;
                        mp[j]=coun;
                    }
                    break;
                }
            }
        }
        cout<<coun<<endl;
        for(i=1;i<=n;i++){
            cout<<num[i]<<" ";
        }
        cout<<endl;
    }
}

E - K-th Beautiful String
For the given integer nn

(n>2n>2

) let’s write down all the strings of length nn

which contain n−2n−2

letters ‘a’ and two letters ‘b’ in lexicographical (alphabetical) order.Recall that the string ss

of length nn

is lexicographically less than string tt

of length nn

, if there exists such ii

(1≤i≤n1≤i≤n

), that si<tisi<ti

, and for any jj

(1≤j<i1≤j<i

) sj=tjsj=tj

. The lexicographic comparison of strings is implemented by the operator < in modern programming languages.For example, if n=5n=5

the strings are (the order does matter): aaabb aabab aabba abaab ababa abbaa baaab baaba babaa bbaaa It is easy to show that such a list of strings will contain exactly n⋅(n−1)2n⋅(n−1)2

strings.You are given nn

(n>2n>2

) and kk

(1≤k≤n⋅(n−1)21≤k≤n⋅(n−1)2

). Print the kk

-th string from the list.InputThe input contains one or more test cases.The first line contains one integer tt

(1≤t≤1041≤t≤104

) — the number of test cases in the test. Then tt

test cases follow.Each test case is written on the the separate line containing two integers nn

and kk

(3≤n≤105,1≤k≤min(2⋅109,n⋅(n−1)2)3≤n≤105,1≤k≤min(2⋅109,n⋅(n−1)2)

.The sum of values nn

over all test cases in the test doesn’t exceed 105105

.OutputFor each test case print the kk

-th string from the list of all described above strings of length nn

. Strings in the list are sorted lexicographically (alphabetically).Example Input 7
5 1
5 2
5 8
5 10
3 1
3 2
20 100
Output aaabb
aabab
baaba
bbaaa
abb
bab
aaaaabaaaaabaaaaaaaa

题意

对于所给的n,其中有2个b,n-2为a。根据字典序,找到第k种。

思路

注意第二个b的位置从倒数第二个位置开始,共有1,2,3……,其中没种的第x个第二个b所在倒数第x个。

代码

#include<iostream>
#include<string>
using namespace std;
int main()
{
    int t,n,i,j,k;
    cin>>t;
    while(t--){
        cin>>n>>k;
        for(i=1;i<=n;i++){
            if(k<=i) break;
            k-=i;
        }
        i=n-i;
        k=n-k+1;
        for(j=1;j<=n;j++){
            if(j==i||j==k)cout<<'b';
            else cout<<'a';
        }
        cout<<endl;
    }
}
                  
 

F - Carousel

The round carousel consists of nn

figures of animals. Figures are numbered from 11

to nn

in order of the carousel moving. Thus, after the nn

-th figure the figure with the number 11

follows. Each figure has its own type — the type of the animal corresponding to this figure (the horse, the tiger and so on). The type of animal of the ii

-th figure equals titi
在这里插入图片描述

. The example of the carousel for n=9n=9

and t=[5,5,1,15,1,5,5,1,1]t=[5,5,1,15,1,5,5,1,1]

. You want to color each figure in one of the colors. You think that it’s boring if the carousel contains two different figures (with the distinct types of animals) going one right after another and colored in the same color.Your task is to color the figures in such a way that the number of distinct colors used is the minimum possible and there are no figures of the different types going one right after another and colored in the same color. If you use exactly kk

distinct colors, then the colors of figures should be denoted with integers from 11

to kk

.InputThe input contains one or more test cases.The first line contains one integer qq

(1≤q≤1041≤q≤104

) — the number of test cases in the test. Then qq

test cases follow. One test case is given on two lines.The first line of the test case contains one integer nn

(3≤n≤2⋅1053≤n≤2⋅105

) — the number of figures in the carousel. Figures are numbered from 11

to nn

in order of carousel moving. Assume that after the nn

-th figure the figure 11

goes.The second line of the test case contains nn

integers t1,t2,…,tnt1,t2,…,tn

(1≤ti≤2⋅1051≤ti≤2⋅105

), where titi

is the type of the animal of the ii

-th figure.The sum of nn

over all test cases does not exceed 2⋅1052⋅105

.OutputPrint qq

answers, for each test case print two lines.In the first line print one integer kk

— the minimum possible number of distinct colors of figures.In the second line print nn

integers c1,c2,…,cnc1,c2,…,cn

(1≤ci≤k1≤ci≤k

), where cici

is the color of the ii

-th figure. If there are several answers, you can print any.Example Input 4
5
1 2 1 2 2
6
1 2 2 1 2 2
5
1 2 1 2 3
3
10 10 10
Output 2
1 2 1 2 2
2
2 1 2 1 2 1
3
2 3 2 3 1
1
1 1 1

题意

有个环形旋转木马(n后面是1),有不同种类的动物,两两之间颜色不能相同,但是相同的动物颜色可以相同也可以不同。

思路

1.特判全是一个种类全部输出1。
2.若为偶数可用1,2交替。
3.奇数寻找中间是否有相同的相邻改为一种颜色。
如果前面不存在判断第n个是否与n-1,1相同种类。

代码

#include<iostream>
const int N = 2e5 + 10;
using namespace std;
int main()
{
   int s;
   cin>>s;
   while(s--){
    int n,i,j;
    cin>>n;
    int t[N],c[N];
    int num1=0;
    for(i=1;i<=n;i++){
        cin>>t[i];
        if(t[i]==t[1])num1++;
    }
    if(num1==n){
        cout<<"1"<<endl;
        for(i=1;i<=n-1;i++){
            cout<<"1 ";
        }
        cout<<"1"<<endl;
        continue;
    }
    c[1]=0;
    for(i=2;i<=n-1;i++){
        c[i]=c[i-1]^1;		//0,1交替
    }
    if(n%2!=0){
        for(i=2;i<=n-1;i++){
            if(t[i]==t[i-1]){		//找到合并的地方。
              for(j=i;j<=n-1;j++){
                c[j]^=1;
              }
              break;
            }
        }
    }
    int co=2;
    if(c[n-1]==c[1]){
        c[n]=c[1]^1;
    }
    else{
        if(t[n]!=t[n-1]&&t[n]!=t[1]){
            co=3;
            c[n]=2;
        }
        else{
            if (t[n] == t[1]) c[n] = c[1];
            else c[n] = c[n-1];
        }
    }
    cout<<co<<endl;
    for(i=1;i<n;i++){cout<<c[i]+1<<" ";}
    cout<<c[n]+1<<endl;
   }
}
                 
                        
            
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值