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

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
void solve(){
	int a,b; cin>>a>>b;
	string s; cin>>s;
	for(int i=0; i<s.size(); i++){
		if(s[i] == '?'){
			s[i] = s[s.size()-i-1];
		}
	}
	a -= count(s.begin(),s.end(),'0');
	b -= count(s.begin(),s.end(),'1');
	int l = 0, r = s.size()-1;
	while(l<=r){
		if(l == r){
			if(s[l] == '?'){
				if(a >= 1){
					s[l] = '0'; a-=1;
				} else if(b >= 1){
					s[l] = '1'; b-=1;
				}
			}
		} else {
			if(s[l] != s[r]){ //两个不同数字 
				cout<<-1<<endl; return ;
			} else if(s[l] == '?' && s[r] == '?'){
				if(a > 1){
					s[l] = s[r] = '0'; a -= 2;
				} else if(b > 1){
					s[l] = s[r] = '1'; b -= 2;
				}
			}
		}
		l++,r--;
	}
	string t = s;
	reverse(t.begin(),t.end());
	if(s==t && a==0 && b==0){
		cout<<s<<endl; return ;
	} else {
		cout<<-1<<endl; return ;
	}
}
int main(){
	int t; cin>>t;
	while(t--){
		solve();
	}
	return 0;
}


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

在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;

void solve() {
	int n;
	cin >> n;
	n += 2;
	long long del = -1;	//用于记录x是否被找到
	long long sum = 0;

	vector<long long>a(n);
	set<long long>s;	//前缀和每次更新时,便插入当前的元素
	
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	sort(a.begin(), a.end());
	
	bool flag = false;
	for (int i = 0; i < n; ++i) {
		sum += a[i];
		s.insert(a[i]);
		if (i == n - 3 && sum == a[n - 2]) {
			flag = true;
			//弹出前n项和 和 x,之后直接进行输出
			a.pop_back();
			a.pop_back();
			break;
		}
		else if (i == n - 2 && sum > a.back() && s.find(sum - a.back()) != s.end()) {
			flag = true;
			del = sum - a[n - 1];
			a.pop_back();	//弹出前n+1项的和
			break;
		}
	}
	if (del != -1 && flag) {
		bool al = false;
		for (auto x : a) {
			if (!al && x == del) {//遇到与x相同的值则不输出(此过程只进行一次)
				al = true;
				continue;
			}
			cout << x << ' ';
		}
		cout << endl;
	}
	else if (flag) {
		for (auto x : a)
			cout << x << ' ';
		cout << endl;
	}
	else {
		cout << -1 << endl;
	}
}

int main() {
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

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
在这里插入图片描述

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

void solve() {
	int n, l, r, s, dis;
	cin >> n >> l >> r >> s;
	dis = r - l + 1;
	vector<int>ans(n + 1, -1);
	vector<bool>vis(n + 1, false);

	if (s < dis * (1 + dis) / 2 || s > dis * (2 * n + 1 - dis) / 2) {
		cout << -1 << endl;
		return;
	}

	int now = dis * (1 + dis) / 2;	//当前得到的前(r - l + 1)项的和
	int cnt = 0;	//计数,now与s的差平均分配了几次
	int left = 0;	//记录now与s的不能被平均分配的差
	while (now + (cnt + 1 ) * dis <= s && dis + (cnt + 1) <= n) {
		++cnt;
	}
	left = s - (now + cnt * dis);

	for (int i = l, num; i <= r; ++i) {
		num = (i - l + 1) + cnt;
		if (i <= r - left);
		else
			num += 1;

		if (!vis[num]) {
			ans[i] = num;
			vis[num] = true;
		}
	}

	int index = 1;
	for (int i = 1; i <= n; ++i) {
		if (!vis[i]) {
			while (index >= l && index <= r)
				++index;

			ans[index] = i;
			++index;
		}
	}

	for (int i = 1; i <= n; ++i)
		cout << ans[i] << ' ';
	cout << endl;
	return;
}

int main() {
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值