Codeforces Round #713 (Div. 3)
C. A-B Palindrome
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a string s consisting of the characters ‘0’, ‘1’, and ‘?’. You need to replace all the characters with ‘?’ in the string s by ‘0’ or ‘1’ so that the string becomes a palindrome and has exactly a characters ‘0’ and exactly b characters ‘1’. Note that each of the characters ‘?’ is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i]=t[n?i+1] is true for all i (1≤i≤n).
For example, if s=“01???0”, a=4 and b=4, then you can replace the characters ‘?’ in the following ways:
“01011010”;
“01100110”.
For the given string s and the numbers a and b, replace all the characters with ‘?’ in the string s by ‘0’ or ‘1’ so that the string becomes a palindrome and has exactly a characters ‘0’ and exactly b characters ‘1’.
Input
The first line contains a single integer t (1≤t≤104). Then t test cases follow.
The first line of each test case contains two integers a and b (0≤a,b≤2?105, a+b≥1).
The second line of each test case contains the string s of length a+b, consisting of the characters ‘0’, ‘1’, and ‘?’.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2?105.
Output
For each test case, output:
“-1”, if you can’t replace all the characters ‘?’ in the string s by ‘0’ or ‘1’ so that the string becomes a palindrome and that it contains exactly a characters ‘0’ and exactly b characters ‘1’;
the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
inputCopy
9
4 4
01???0
3 3
???
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
outputCopy
01011010
-1
0
-1
0110
-1
111
1001
0101010
D. Corrupted Array
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a number n and an array b1,b2,…,bn+2, obtained according to the following algorithm:
some array a1,a2,…,an was guessed;
array a was written to array b, i.e. bi=ai (1≤i≤n);
The (n+1)-th element of the array b is the sum of the numbers in the array a, i.e. bn+1=a1+a2+…+an;
The (n+2)-th element of the array b was written some number x (1≤x≤109), i.e. bn+2=x; The
array b was shuffled.
For example, the array b=[2,3,7,12,2] it could be obtained in the following ways:
a=[2,2,3] and x=12;
a=[3,2,7] and x=2.
For the given array b, find any array a that could have been guessed initially.
Input
The first line contains a single integer t (1≤t≤104). Then t test cases follow.
The first line of each test case contains a single integer n (1≤n≤2?105).
The second row of each test case contains n+2 integers b1,b2,…,bn+2 (1≤bi≤109).
It is guaranteed that the sum of n over all test cases does not exceed 2?105.
Output
For each test case, output:
“-1”, if the array b could not be obtained from any array a;
n integers a1,a2,…,an, otherwise.
If there are several arrays of a, you can output any.
Example
inputCopy
4
3
2 3 7 12 2
4
9 1 7 1 6 5
5
18 2 2 3 2 9 2
3
2 6 9 2 1
outputCopy
2 3 7
-1
2 2 2 3 9
1 2 6
E. Permutation by Sum
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A permutation is a sequence of n integers from 1 to n, in which all the numbers occur exactly once. For example, [1], [3,5,2,1,4], [1,3,2] are permutations, and [2,3,2], [4,3,1], [0] are not.
Polycarp was given four integers n, l, r (1≤l≤r≤n) and s (1≤s≤n(n+1)2) and asked to find a permutation p of numbers from 1 to n that satisfies the following condition:
s=pl+pl+1+…+pr.
For example, for n=5, l=3, r=5, and s=8, the following permutations are suitable (not all options are listed):
p=[3,4,5,2,1];
p=[5,2,4,3,1];
p=[5,2,1,3,4].
But, for example, there is no permutation suitable for the condition above for n=4, l=1, r=1, and s=5.
Help Polycarp, for the given n, l, r, and s, find a permutation of numbers from 1 to n that fits the condition above. If there are several suitable permutations, print any of them.
Input
The first line contains a single integer t (1≤t≤500). Then t test cases follow.
Each test case consist of one line with four integers n (1≤n≤500), l (1≤l≤n), r (l≤r≤n), s (1≤s≤n(n+1)2).
It is guaranteed that the sum of n for all input data sets does not exceed 500.
Output
For each test case, output on a separate line:
n integers — a permutation of length n that fits the condition above if such a permutation exists;
-1, otherwise.
If there are several suitable permutations, print any of them.
Example
inputCopy
5
5 2 3 5
5 3 4 1
3 1 2 4
2 2 2 2
2 1 1 3
outputCopy
1 2 3 4 5
-1
1 3 2
1 2
-1
總結
以上是生活随笔為你收集整理的Codeforces Round #713 (Div. 3)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces Round #71
- 下一篇: 2021 年百度之星·程序设计大赛 -