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

思路

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

思路

  此题参考了孙晨曦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 }

原文地址:https://www.cnblogs.com/wsytj/p/12153470.html

时间: 2024-11-01 13:33:59

新生训练赛002题解的相关文章

郑州大学2018新生训练赛第十场题解

比赛(补题)地址:http://222.22.65.164/problemset.php 题号为:4305 -- 4309 总述:这次新生赛难度偏于平和,但涵盖方面甚广,其中一道签到题是c语言题,并且有两道是hdu一百题的原题,一道简单的最小生成树,唯一"有些难度"的应该是一道数论题(毕竟本来自己就是搞数学的).   A.沙漠骆驼 这是一道经典的递推问题,原型为HDU 2044的"一只小蜜蜂-".思路很简单,以第5个沙丘为例,到达第五个沙丘的方式有两种:从第3个向

Contest1592 - 2018-2019赛季多校联合新生训练赛第二场(部分题解)

D 10248 修建高楼 D 传送门 题干 题目描述 C 市有一条东西走向的"市河".C 市的市长打算在"市河"的其中一条岸边自东往西的 n 个位置(可以将这 n 个位置看成在一条直线上,且位置不会重叠)依次建造高楼. C 市的设计部门设计了 T 个方案供市长挑选(方案编号为 1 到 T).每个方案都提供了建造的每幢高楼的高度,自东向西依次为 h1,h2,h3,-,hn-1,hn.每幢楼房的高度在 1 到 n 之间(包括 1 和 n),且各不相同. 市长在挑选设计方

2018-2019赛季多校联合新生训练赛第六场(2018/12/15)补题题解

A 价钱统计(基础编程能力) 这个考点还是比较个性的,怎么四舍五入 解法 常规的讲如果四舍五入整数位的话,那么只需要在后面加个0.5然后强制转换一下就可以了 这个却要我们保留一位小数的四舍五入,那该怎么做呢 实际上我们只需要把这个数乘以10然后加0.5,强制转换后再除以10就可以了 代码 #include <bits/stdc++.h> using namespace std; double trans(double a) { a*=10; a+=0.5; a=int(a); a/=10; r

2018.12.2 中国石油大学第一次新生训练赛题解

整理人: 周翔 A题:李继朋 B题:李继朋 H题:魏斯博 原文地址:https://www.cnblogs.com/QLU-ACM/p/10057831.html

2018-2019赛季多校联合新生训练赛第八场(2018/12/22)补题题解

感慨 这次有点感冒,昏迷程度比较大中途还溜了 感谢 感谢qut的同学的帮助!!! A 小X与三角形(数学) 公式 两边的和-两边的差-1 因为边最小得大于两边的差,边最大得小于两边的和所以说求得是一个开区间内元素的个数 代码 #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll a,

2016年NK冬季训练赛 民间题解

A题 水题,考察对行的读入和处理,注意使用long long #include <iostream> #include <cstring> #include <cstdio> using namespace std; int main() { char ch; long long a, sum=0, Min; char S[200]; while(cin.getline(S, 100)) { Min = 1e9; a = 0; for(int i = 0; i <

2017光棍节新生训练赛

Description There are some students in a class, Can you help teacher find the highest student . Input There are some cases. The first line contains an integer t, indicate the cases; Each case have an integer n ( 1 ≤ n ≤ 100 ) , followed n students' h

迎接2019多校联合新生训练赛(2018/12/31)

A 新年礼物(数学) 这个题之前cf div2刚刚考过应该都会吧.就是把左边界×2遍历一下就可以了 代码 #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll l,r,sum=0; cin>>l>>r; for(ll i=l;i<=r;) i*=2,su

周一训练赛题解

这次完全是水题大集合啊,希望大家A的开心: 前两个题是我找的,后两个是陶叔找的,另外因为我的偷懒,下面所有的代码都是陶叔亲自写的,十分感谢陶叔: 陶叔暑假为了大家的集训,牺牲了很多自己宝贵的时间,大家接下来要好好训练啊!!!! 废话少说,进入正题: Problem A      SPOJ QUEST5 签到题: 将所有的边按照右端点排个序,然后每次选择没有钉住的点,然后把这之后的所有与它相交的边全去掉: 代码: #include <cstdio> #include <cstring>