新生训练赛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。

 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 - 由你来决定怎么颁奖

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

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和3堆,当3堆等于2堆时,若1堆有剩余,将1分配给2和3,同时吃2和3;若2堆还未等于三堆时,1堆已经为0,则吃2和3;

 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 #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 using namespace std;
 3 int main()
 4 {
 5     
 6     int t,n,x,a[100005],l,r=0;
 7     
 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) cout<<"1";
24             else cout<<"0";
25         }
26         cout<<endl;    
27     }
28     return 0;
29 }

猜你喜欢

转载自www.cnblogs.com/wsytj/p/12153470.html
今日推荐