Codeforces Round #604 (Div.2)

Codeforces Round #604 (Div.2)

2019/12/5 22:35——2019/12/6 00:35

Codeforces Round #604 (Div.2)

A. Beautiful String

time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output

题目描述

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.
Examples
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”.

解题思路

我的想法是如果已经确定了的字母中有连续重复的字母,那么一定无解,否则必然有解;
构造一个字符串sign=“abca”, 从s[1]开始如果s[i]为‘?’那么s[i]=sign[s[i-1]-‘a’+1];如果此时和s[i+1]重复了,那么就令s[i]=sign[s[i-1]-‘a’+2];
注意:可能s[0]是一个‘?’这是只需让s[0]成为一个和s[1]不相等的字符即可。

AC代码

#include 
using namespace std;
string str="abcabc";
int main()
{
	int n;
	string s;
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		getline(cin,s);
		int l=s.length();
		int flag=0;
		for(int i=0;i<l-1;i++)
		{
			if(s[i]==s[i+1] &&s[i]!='?') 
			{
				printf("-1\n");
				flag=1;
				break;
			}
		}
		if(flag) continue;
		if(s[0]=='?')
		{
			s[0]='a';
			if(s[0]==s[1]) s[0]++;
		}
		for(int i=1;i<l;i++)
		{
			if(s[i]=='?')
			{
				s[i]=str[s[i-1]-'a'+1];
				if(s[i]==s[i+1]) s[i]++;
				if(s[i]=='d') s[i]='a';
			}
		}
		cout<<s<<endl;
	} 
	return 0;
}

 
 

B. Beautiful Numbers

time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output

题目描述

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.

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

解题思路

我们可以用一个数组index标记每个数字i的位置,如果能构成这样的一个序列那么1和m一定满足1到m中的最大位置与最小位置之差等于m-1(因为他们都是连续的的,数字每增加一,位置也增加一)
再用另一个数组sign标记结果,如果能构造出这样的数列那么sign就为1,否则为0,最后输出即可

AC代码

#include 
using namespace std;
int t,n,p[200100];
int index[200100];
int sign[200100];
int ma,mi;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		memset(index,0,sizeof(index));
		memset(sign,0,sizeof(sign));
		ma=0;mi=200100;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&p[i]);
			if(p[i]<=200000) index[p[i]]=i;
		}
		for(int i=1;i<=n;i++)
		{
			ma=max(ma,index[i]);
			if(mi>index[i]) mi=index[i];
			sign[i]=1;
			if(ma-i+1!=mi) sign[i]=0;
		}
		for(int i=1;i<=n;i++) printf("%d",sign[i]);
		printf("\n");
	}
	return 0;
}

 
 

C. Beautiful Regional Contest

time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output

题目描述

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

Help the jury distribute the gold, silver and bronze medals. Let their numbers be g, s and b, 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 > 0, s > 0 and
    b > 0);
    the number of gold medals must be strictly less than the number of silver and the number of bronze (that is, g < s and g < b, but there are no requirements between s and b);
  • 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 + b should not exceed half of all participants (for example, if n = 21, then you can award a maximum of 10 participants, and if n = 26, then you can award a maximum of 13 participants).

The jury wants to reward with medals the total maximal number participants (i.e. to maximize g + 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 t ( 1 ≤ t ≤ 10000) — the number of test cases in the input. Then t test cases follow.

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

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

Output

Print t lines, the j-th line should contain the answer to the j-th test case.

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

Print g = 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,b — the possible number of gold, silver and bronze medals, respectively. The sum of g + s + b should be the maximum possible. If there are several answers, print any of them.

Examples
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 1 gold, 2 silver and 3 bronze medals. In this case, the participant solved 5 tasks will be rewarded with the gold medal, participants solved 4 tasks will be rewarded with silver medals, participants solved 2 or 3 tasks will be rewarded with bronze medals. Participants solved exactly 1 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 6 medals because the number of medals should not exceed half of the number of participants. The answer 1, 3, 2 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.

解题思路

对于本题我们先求出最多有多少人获奖,首先要满足获奖人数不超过总人数的一半,我们用一个变量mid=n/2
第二个条件是获奖的人解决的问题严格比不获奖的人多,这时只需要判断p[mid]是否等于p[mid+1]如果相等那么获奖人数就要继续减少,即mid–;再考虑金牌,因为金牌人数要严格少于银牌和铜牌,而银牌和铜牌数量关系没有要求,所以我们可以先确定金牌的人数,在确定银牌人数,最后用总获奖人数减去他们得到铜牌人数,如果铜牌人数比金牌人数多,那么就是合理的答案。
对于金牌人数当然是越少越好了,因为他不会影响总获奖人数(后面的人全发银牌铜牌就行了),所以用first表示金牌,默认first=1,如果p[first]=p[first+1]那么first就增大直到金牌的解题数严格大于银牌,用second表示银牌,对于银牌要比金牌严格多,那么划分银牌与铜牌的位置最靠前就是second=first*2+1,此时刚好银牌比金牌多一个,只需再满足银牌解题数严格比铜牌多即可,同理如果p[second]=p[second+1]则second递增;

AC代码

#include 
using namespace std;
int t,n,p[400100];
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%d",&p[i]);
		int mid=n/2;
		while(p[mid]==p[mid+1]) mid--;
		if(mid<=4) 
		{
			printf("0 0 0\n");
			continue;
		}
		int first=1;
		while(p[first]==p[first+1]) first++;
		int second=2*first+1;
		while(p[second]==p[second+1]) second++;
		second=second-first;
		int third=mid-first-second;
		if(third<=first || second<=first) 
		{
			printf("0 0 0\n");
			continue;
		}
		printf("%d %d %d\n",first,second,third);
	}
	return 0;
}

 
 

D. Beautiful Sequence

time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output

题目描述

An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to 1. More formally, a sequence s1,s2, … ,sn is beautiful if |si−si+1|=1 for all 1 ≤ i ≤ n−1.

Trans has a numbers 0, b numbers 1, c numbers 2 and d numbers 3. He wants to construct a beautiful sequence using all of these a + b + c + d numbers.

However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?

Input

The only input line contains four non-negative integers a, b, c and d (0< a+b+c+d ≤ 105).

Output

If it is impossible to construct a beautiful sequence satisfying the above constraints, print “NO” (without quotes) in one line.

Otherwise, print “YES” (without quotes) in the first line. Then in the second line print a + b + c + d integers, separated by spaces — a beautiful sequence. There should be a numbers equal to 0, b numbers equal to 1, c numbers equal to 2 and d numbers equal to 3.

If there are multiple answers, you can print any of them.

Examples
input
 2 2 2 1
output
 YES
 0 1 0 1 2 3 2
input
 1 2 3 4
output
 NO
input
 2 2 2 3
output
 NO
Note

In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to 1. Also, there are exactly two numbers, equal to 0, 1, 2 and exactly one number, equal to 3.

It can be proved, that it is impossible to construct beautiful sequences in the second and third tests.

解题思路

等待更新

AC代码

等我补完题一定第一时间更新

 
 

E. Beautiful Mirrors

time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output

题目描述

Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror “Am I beautiful?”. The i-th mirror will tell Creatnx that he is beautiful with probability pi/100 for all 1 ≤ i ≤ n.

Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:

  • The i-th mirror tells Creatnx that he is beautiful. In this case, if i=n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
  • In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again.

You need to calculate the expected number of days until Creatnx becomes happy.

This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q ≢≡ 0(mod M). Output the integer equal to p⋅q−1mod M. In other words, output such an integer x that 0≤ x < M and x⋅q ≡ p(mod M).

Input

The first line contains one integer n (1 ≤ n ≤ 2⋅105) — the number of mirrors.

The second line contains n integers p1,p2, … , pn (1≤ pi ≤100).

Output

Print the answer modulo 998244353 in a single line.

Examples
input
 1
 50
output
 2
input
 3
 10 20 50
output
 112
Note

In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2.

解题思路

等待更新

AC代码

等我补完题一定第一时间更新

 
 

F. Beautiful Bracket Sequence (easy version)

time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output

题目描述

This is the easy version of this problem. The only difference is the limit of n - the length of the input string. In this version, 1 ≤ n ≤ 2000. The hard version of this challenge is not offered in the round for the second division.

Let’s define a correct bracket sequence and its depth as follow:

  • An empty string is a correct bracket sequence with depth 0.
  • If “s” is a correct bracket sequence with depth d then “(s)” is a correct bracket sequence with depth d+1.
  • If “s” and “t” are both correct bracket sequences then their concatenation “st” is a correct bracket sequence with depth equal to the maximum depth of s and t.
    For a (not necessarily correct) bracket sequence s, we define its depth as the maximum depth of any correct bracket sequence induced by removing some characters from s (possibly zero). For example: the bracket sequence s="())(())" has depth 2, because by removing the third character we obtain a correct bracket sequence “()(())” with depth 2.

Given a string a consists of only characters ‘(’, ‘)’ and ‘?’. Consider all (not necessarily correct) bracket sequences obtained by replacing all characters ‘?’ in a by either ‘(’ or ‘)’. Calculate the sum of all the depths of all these bracket sequences. As this number can be large, find it modulo 998244353.

Hacks in this problem in the first division can be done only if easy and hard versions of this problem was solved.

Input

The only line contains a non-empty string consist of only ‘(’, ‘)’ and ‘?’. The length of the string is at most 2000.

Output

Print the answer modulo 998244353 in a single line.

Examples
input
 ??
output
 1
input
 (?(?))
output
 9
Note

In the first test case, we can obtain 4 bracket sequences by replacing all characters ‘?’ with either ‘(’ or ‘)’:

“((”. Its depth is 0;
“))”. Its depth is 0;
“)(”. Its depth is 0;
“()”. Its depth is 1.
So, the answer is 1 = 0 + 0 + 0 + 1.

In the second test case, we can obtain 4 bracket sequences by replacing all characters ‘?’ with either ‘(’ or ‘)’:

“(((())”. Its depth is 2;
“()()))”. Its depth is 2;
“((()))”. Its depth is 3;
“()(())”. Its depth is 2.
So, the answer is 9 = 2 + 2 + 3 + 2.

解题思路

等待更新

AC代码

#include 
#define MOD 998244353
using namespace std;
long long quickpow(int x, int n) //快速幂
{
    long long ans = 1, t = x;
    while (n) 
    {
        if (n & 1) ans = (long long) ans * t % MOD;
        t = (long long) t * t % MOD;
        n >>= 1;
    }
    return ans;
}
int main() {
    string s;
    getline(cin,s);
    int n = s.length();
    vector<vector<int>> dp(n, vector<int>(n));
    vector<int> f(n + 1);
    for (int i = 0; i < n; i++) {
        f[i + 1] = f[i];
        f[i + 1] += s[i] == '?';
    }
    for (int len = 2; len <= n; len++) {
        for (int i = 0; i < n - len + 1; i++) {
            int j = i + len - 1;
            if (s[i] != '(') {
                dp[i][j] += dp[i + 1][j];
                dp[i][j] %= MOD;
            }
            if (s[j] != ')') {
                dp[i][j] += dp[i][j - 1];
                dp[i][j] %= MOD;
            }
            if (s[i] != '(' && s[j] != ')') {
                dp[i][j] -= dp[i + 1][j - 1];
                dp[i][j] += MOD;
                dp[i][j] %= MOD;
            }
            if (s[i] != ')' && s[j] != '(') {
                dp[i][j] += dp[i + 1][j - 1];
                dp[i][j] %= MOD;
                dp[i][j] += fpow(2, f[j] - f[i + 1]);
                dp[i][j] %= MOD;
            }
        }
    }
    cout << dp[0][n - 1] << "\n";
    return 0;
}

 
 

你可能感兴趣的:(C/C++,codeforces)