CCCherry

导航

新生训练赛002题解

 

J 新年快乐!!!

I 十进制中的二进制:

题目
hkhv学长最近对二进制数很感兴趣,喜欢一切0和1组成的数。现在有一个十进制整数n,问你1到n之间有多少个数是只有0和1组成的类似二进制的数,输出他们的个数。
Input
输入数据包含一个数n (1 <= n <=10^9).
Output
输出1到n中类似二进制的数的个数.
Sample Input
10
Sample Output
2
Hint
对于n = 10,1 和 10是类似二进制的数.

思路

一开始直接暴力,后来发现是多组输入,打表。或者用dfs(努力学习中。。。)

 //美丽曲线hhh
1
#include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int main() 5 { 6 int a,s,b[1000],r=0; 7 for(int i=0;i<=1;i++) 8 { 9 for(int j=0;j<=1;j++) 10 { 11 for(int k=0;k<=1;k++) 12 { 13 for(int l=0;l<=1;l++) 14 { 15 for(int m=0;m<=1;m++) 16 { 17 for(int n=0;n<=1;n++) 18 { 19 for(int o=0;o<=1;o++) 20 { 21 for(int p=0;p<=1;p++) 22 { 23 for(int q=0;q<=1;q++) 24 { 25 int t=q+p*10+o*100+n*1000+m*10000+l*100000+k*1000000+j*10000000+i*100000000; 26 b[r++]=t; 27 } 28 } 29 } 30 } 31 } 32 } 33 } 34 } 35 } 36 while(scanf("%d",&a)!=EOF) 37 { 38 s=0; 39 for(int i=1;i<r;i++) 40 { 41 if(b[i]<=a) 42 s++; 43 } 44 45 if(a==1000000000) 46 s++; 47 cout<<s<<endl; 48 } 49 return 0; 50 51 }

 

 

H Perfect String

题目
A string is called beautiful if no two consecutive characters are equal. For example, “ababcb”, “a” and “abab” are beautiful strings, while “aaaaaa”, “abaa” and “bb” are not.
Ahcl wants to construct a beautiful string. He has a string s, consisting of only characters ‘a’, ‘b’, ‘c’ and ‘?’. Ahcl needs to replace each character ‘?’ with one of the three characters ‘a’, ‘b’ or ‘c’, such that the resulting string is beautiful. Please help him!
More formally, after replacing all characters ‘?’, the condition si≠si+1 should be satisfied for all 1≤i≤|s|−1, where |s| is the length of the string s.
Input
The first line contains positive integer t (1≤t≤1000) — the number of test cases. Next t lines contain the descriptions of test cases.
Each line contains a non-empty string s consisting of only characters ‘a’, ‘b’, ‘c’ and ‘?’.
It is guaranteed that in each test case a string s has at least one character ‘?’. The sum of lengths of strings s in all test cases does not exceed 105.
Output
For each test case given in the input print the answer in the following format:
If it is impossible to create a beautiful string, print “-1” (without quotes);
Otherwise, print the resulting beautiful string after replacing all ‘?’ characters. If there are multiple answers, you can print any of them.
Example
Input
3
a???cb
a??bbc
a?b?c
Output
ababcb
-1
acbac
Note
In the first test case, all possible correct answers are “ababcb”, “abcacb”, “abcbcb”, “acabcb” and “acbacb”. The two answers “abcbab” and “abaabc” are incorrect, because you can replace only ‘?’ characters and the resulting string must be beautiful.
In the second test case, it is impossible to create a beautiful string, because the 4-th and 5-th characters will be always equal.
In the third test case, the only answer is “acbac”.

思路:

先判断非 ?部分是否是Perfect String,再将 ?变成与前后不同的字母

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 int main()
 5 {
 6     int n,l;
 7     string m="abc";
 8     string s;
 9     cin>>n;
10     while(n--)
11     {
12         int w=0;
13         cin>>s;
14         l=s.size();
15         for(int i=0;i<l-1;i++)
16         {
17             if(s[i]!='?'&&s[i]==s[i+1])//判断是否有相邻子母相同 
18             {
19                 w=1;
20             }
21         }
22         if(w==1)
23         {
24             cout<<-1<<endl;
25             continue;
26          } 
27         else
28         {
29             for(int i=0;i<l;i++)
30             {
31                 if(s[i]=='?')
32                 {
33                     for(int j=0;j<3;j++)
34                     {
35                         if(m[j]!=s[i-1]&&m[j]!=s[i+1])//?等于与前后均不同的字母 
36                             s[i]=m[j];
37                     }
38                 }
39             }
40             cout<<s<<endl;    
41         }
42         
43     }
44     return 0;
45 }

G 0011:

题目
Alex likes to play with one and zero!One day he gets an empty string.So our cute boy wants to add one and zero in it. Every time he will add ‘01’in the string at any position and then get a new string.For example:if the string is “01” now ,he can get “0101” or “0011,Now give you a string that is Alex has get,you need to answer whether the string is legal?
Input
First is a integer n(n<=100)
Next contains n lines .Every line is a string whose legth is no more than 1000.
Output
For each case output “YES” in a single line if it’s legal.
Or you need to output “NO”;
Sample Input
3
0101
0110
0011
Sample Output
YES
NO
YES
Hint

思路

  判断0和1数量是否相同及是否有1比0先出现;

 

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 int main()
 5 {
 6     int n;
 7     char a[1005];
 8     cin>>n;
 9     while(n--)
10     {
11         cin>>a;
12         int l=strlen(a);
13         int ans=0;
14         int f=0;
15         if(a[0]=='1'||a[l-1]=='0') //判断开头是否出现1或末尾出现0 
16         {
17             cout<<"NO"<<endl;
18             continue;
19         }
20         else
21         {
22             for(int i=0;i<l;i++)//判断01是否成对 
23             {
24                 if(a[i]=='0') 
25                     ans++;
26                 else 
27                     ans--;
28                 if (ans<0) 
29                     f=1;
30             }
31         }
32         if(ans!=0||f==1) 
33             cout<<"NO"<<endl;
34         else 
35             cout<<"YES"<<endl;
36     }
37     return 0;
38 }

 

E - 由你来决定怎么颁奖

题目

So the Beautiful Regional Contest (BeRC) has come to an end! nn students took part in the contest. The final standings are already known: the participant in the ii -th place solved pipi problems. Since the participants are primarily sorted by the number of solved problems, then p1p2pnp1≥p2≥⋯≥pn .

Help the jury distribute the gold, silver and bronze medals. Let their numbers be gg , ss and bb , respectively. Here is a list of requirements from the rules, which all must be satisfied:

  • for each of the three types of medals, at least one medal must be awarded (that is, g>0g>0 , s>0s>0 and b>0b>0 );
  • the number of gold medals must be strictly less than the number of silver and the number of bronze (that is, g<sg<s and g<bg<b , but there are no requirements between ss and bb );
  • each gold medalist must solve strictly more problems than any awarded with a silver medal;
  • each silver medalist must solve strictly more problems than any awarded a bronze medal;
  • each bronze medalist must solve strictly more problems than any participant not awarded a medal;
  • the total number of medalists g+s+bg+s+b should not exceed half of all participants (for example, if n=21n=21 , then you can award a maximum of 1010 participants, and if n=26n=26 , then you can award a maximum of 1313 participants).

The jury wants to reward with medals the total maximal number participants (i.e. to maximize g+s+bg+s+b ) so that all of the items listed above are fulfilled. Help the jury find such a way to award medals.

Input

The first line of the input contains an integer tt (1t100001≤t≤10000 ) — the number of test cases in the input. Then tt test cases follow.

The first line of a test case contains an integer nn (1n41051≤n≤4⋅105 ) — the number of BeRC participants. The second line of a test case contains integers p1,p2,,pnp1,p2,…,pn (0pi1060≤pi≤106 ), where pipi is equal to the number of problems solved by the ii -th participant from the final standings. The values pipi are sorted in non-increasing order, i.e. p1p2pnp1≥p2≥⋯≥pn .

The sum of nn over all test cases in the input does not exceed 41054⋅105 .

Output

Print tt lines, the jj -th line should contain the answer to the jj -th test case.

The answer consists of three non-negative integers g,s,bg,s,b .

  • Print g=s=b=0g=s=b=0 if there is no way to reward participants with medals so that all requirements from the statement are satisfied at the same time.
  • Otherwise, print three positive numbers g,s,bg,s,b — the possible number of gold, silver and bronze medals, respectively. The sum of g+s+bg+s+b should be the maximum possible. If there are several answers, print any of them.

Example

Input
5
12
5 4 4 3 2 2 1 1 1 1 1 1
4
4 3 2 1
1
1000000
20
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
32
64 64 63 58 58 58 58 58 37 37 37 37 34 34 28 28 28 28 28 28 24 24 19 17 17 17 17 16 16 16 16 11
Output
1 2 3
0 0 0
0 0 0
2 5 3
2 6 6

Note

In the first test case, it is possible to reward 11 gold, 22 silver and 33 bronze medals. In this case, the participant solved 55 tasks will be rewarded with the gold medal, participants solved 44 tasks will be rewarded with silver medals, participants solved 22 or 33 tasks will be rewarded with bronze medals. Participants solved exactly 11 task won't be rewarded. It's easy to see, that in this case, all conditions are satisfied and it is possible to reward participants in this way. It is impossible to give more than 66 medals because the number of medals should not exceed half of the number of participants. The answer 11 , 33 , 22 is also correct in this test case.

In the second and third test cases, it is impossible to reward medals, because at least one medal of each type should be given, but the number of medals should not exceed half of the number of participants.

思路

 

我的思路是先用桶排算出各成绩(解出问题数)的人数,然后分配奖牌,但是代码太繁琐,后来参考了dalao的题解,发现直接分配更简洁;

 

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int t,n,a[1000005];
 6     cin>>t;
 7     while(t--)
 8     {
 9         cin>>n;
10         int g=0, s=0, b=0;
11         int m=n/2;
12         for(int i=1;i<=n;i++)
13         {
14             cin>>a[i];
15         }
16         int i=1;
17         while(a[i]==a[1]&&i<=m)
18         {
19             i++;
20             g++;
21         }
22          int j=i;
23         while(a[i]==a[j]&&i<=m)
24         {
25             i++;
26             s++;
27         }    
28         while(s<=g&&i<=m)
29         {
30             int k=i;
31             while(a[i]==a[k]&&i<=m)
32             {
33                 s++;
34                 i++;
35             }
36         }
37         while(a[i]!=a[m+1]&&i<=m)
38         {
39             b++;
40             i++;
41         }
42         if(g>=s||g>=b)
43         {
44             cout<<"0 0 0\n";
45             continue;
46         }
47         cout<<g<<" "<<s<<" "<<b<<" "<<endl;
48     }
49     return 0;
50 }

 

 

 

 

D - Eat Candies

题目
You have three piles of candies: red, green and blue candies:
the first pile contains only red candies and there are r candies in it,
the second pile contains only green candies and there are g candies in it,
the third pile contains only blue candies and there are b candies in it.
Each day Tanya eats exactly two candies of different colors. She is free to choose the colors of eaten candies: the only restriction that she can’t eat two candies of the same color in a day.
Find the maximal number of days Tanya can eat candies? Each day she needs to eat exactly two candies.
Input
The first line contains integer t (1≤t≤1000) — the number of test cases in the input. Then t test cases follow.
Each test case is given as a separate line of the input. It contains three integers r, g and b (1≤r,g,b≤108) — the number of red, green and blue candies, respectively.
Output
Print t integers: the i-th printed integer is the answer on the i-th test case in the input.
Example
Input
6
1 1 1
1 2 1
4 1 1
7 4 10
8 1 4
8 2 8
Output
1
2
2
10
5
9
Note
In the first example, Tanya can eat candies for one day only. She can eat any pair of candies this day because all of them have different colors.
In the second example, Tanya can eat candies for two days. For example, she can eat red and green candies on the first day, and green and blue candies on the second day.
In the third example, Tanya can eat candies for two days. For example, she can eat red and green candies on the first day, and red and blue candies on the second day. Note, that two red candies will remain uneaten.

思路

  方法一:算是找规律吧,若最大堆大于另外两堆之和,那么结果等于较小两堆之和;否则等于三堆之和的一半(向下取整)。

 

 1 #include<iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 int main()
 5 {
 6     
 7     int n,r,g,b;
 8     cin>>n;
 9     while(n--)
10     {
11         cin>>r>>g>>b;
12         if(r>g) swap(r,g);
13         if(b>g) swap(b,g);
14         int t=r+b-g;
15         if(t<=0) 
16             cout<<r+b<<endl;
17         else 
18             cout<<(r+g+b)/2<<endl;
19     }
20 }

 

方法二:模拟分配过程,升序排列三堆,先吃1和3堆,当3堆等于2堆时,若1堆有剩余,将1分配给2和3,同时吃2和3;若2堆还未等于三堆时,1堆已经为0,则吃2和3;

 
 1 #include<iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 using namespace std;
 5 int main()
 6 {
 7     int n,r,g,b,a[3];
 8     cin>>n;
 9     while(n--)
10     {
11         for(int i=0;i<3;i++)
12         {
13             cin>>a[i];
14         }
15         sort(a,a+3);
16         if(a[2]-a[1]>=a[0])
17         {
18             cout<<a[0]+min(a[1],a[2])<<endl;
19         }
20         else
21         {
22             a[0]=a[0]-a[2]+a[1];
23             cout<<a[2]+a[0]/2<<endl;
24         }
25     }
26 }

B - Is it beautiful?

题目
You are given a permutation p=[p1,p2,…,pn] of integers from 1 to n. Let’s call the number m (1≤m≤n) beautiful, if there exists two indices l,r (1≤l≤r≤n), such that the numbers [pl,pl+1,…,pr] is a permutation of numbers 1,2,…,m.
For example, let p=[4,5,1,3,2,6]. In this case, the numbers 1,3,5,6 are beautiful and 2,4 are not. It is because:
if l=3 and r=3 we will have a permutation [1] for m=1;
if l=3 and r=5 we will have a permutation [1,3,2] for m=3;
if l=1 and r=5 we will have a permutation [4,5,1,3,2] for m=5;
if l=1 and r=6 we will have a permutation [4,5,1,3,2,6] for m=6;
it is impossible to take some l and r, such that [pl,pl+1,…,pr] is a permutation of numbers 1,2,…,m for m=2 and for m=4.
You are given a permutation p=[p1,p2,…,pn]. For all m (1≤m≤n) determine if it is a beautiful number or not.
Input
The first line contains the only integer t (1≤t≤1000)  — the number of test cases in the input. The next lines contain the description of test cases.
The first line of a test case contains a number n (1≤n≤2⋅105) — the length of the given permutation p. The next line contains n integers p1,p2,…,pn (1≤pi≤n, all pi are different) — the given permutation p.
It is guaranteed, that the sum of n from all test cases in the input doesn’t exceed 2⋅105.
Output
Print t lines — the answers to test cases in the order they are given in the input.
The answer to a test case is the string of length n, there the i-th character is equal to 1 if i is a beautiful number and is equal to 0 if i is not a beautiful number.
Example
Input
3
6
4 5 1 3 2 6
5
5 3 1 2 4
4
1 4 3 2
Output
101011
11111
1001
Note
The first test case is described in the problem statement.
In the second test case all numbers from 1 to 5 are beautiful:
if l=3 and r=3 we will have a permutation [1] for m=1;
if l=3 and r=4 we will have a permutation [1,2] for m=2;
if l=2 and r=4 we will have a permutation [3,1,2] for m=3;
if l=2 and r=5 we will have a permutation [3,1,2,4] for m=4;
if l=1 and r=5 we will have a permutation [5,3,1,2,4] for m=5.

思路

  就是找到1~m所对应位置的区间,然后判断区间长度是否等于m;

  (此题参考了孙晨曦dalao的题解,学到了一个新方法,输入数组时直接标记了数字的位置,炒鸡方便hhh!!!)

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 int main()
 5 {
 6     
 7     int t,n,x,a[200005],l,r=0;
 8     cin>>t;
 9     while(t--)
10     {
11         cin>>n;
12         r=0;
13         l=200005;
14         for(int i=1;i<=n;i++)
15         {
16             cin>>x;
17             a[x]=i;
18         }
19         for(int i=1;i<=n;i++)
20         {
21             l=min(l,a[i]);
22             r=max(r,a[i]);
23             if(r-l+1==i)
24                              cout<<"1";
25                 else 
26                             cout<<"0";
27         }
28         cout<<endl;    
29     }
30     return 0;
31 }              

 

 

posted on 2020-01-05 20:19  CCCherry  阅读(269)  评论(0编辑  收藏  举报