CodeforcesRound#668

Codeforces Round #668 Editorial

A

题目链接
A题
A. Permutation Forgery
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
A permutation of length 𝑛 is an array consisting of 𝑛 distinct integers from 1 to 𝑛 in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (𝑛=3 but there is 4 in the array).

Let 𝑝 be any permutation of length 𝑛. We define the fingerprint 𝐹(𝑝) of 𝑝 as the sorted array of sums of adjacent elements in 𝑝. More formally,

𝐹(𝑝)=sort([𝑝1+𝑝2,𝑝2+𝑝3,…,𝑝𝑛−1+𝑝𝑛]).
For example, if 𝑛=4 and 𝑝=[1,4,2,3], then the fingerprint is given by 𝐹(𝑝)=sort([1+4,4+2,2+3])=sort([5,6,5])=[5,5,6].

You are given a permutation 𝑝 of length 𝑛. Your task is to find a different permutation 𝑝′ with the same fingerprint. Two permutations 𝑝 and 𝑝′ are considered different if there is some index 𝑖 such that 𝑝𝑖≠𝑝′𝑖.

Input
Each test contains multiple test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤668). Description of the test cases follows.

The first line of each test case contains a single integer 𝑛 (2≤𝑛≤100) — the length of the permutation.

The second line of each test case contains 𝑛 integers 𝑝1,…,𝑝𝑛 (1≤𝑝𝑖≤𝑛). It is guaranteed that 𝑝 is a permutation.

Output
For each test case, output 𝑛 integers 𝑝′1,…,𝑝′𝑛 — a permutation such that 𝑝′≠𝑝 and 𝐹(𝑝′)=𝐹(𝑝).

We can prove that for every permutation satisfying the input constraints, a solution exists.

If there are multiple solutions, you may output any.

Example
inputCopy
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
outputCopy
2 1
1 2 5 6 3 4
3 1 5 2 4
Note
In the first test case, 𝐹(𝑝)=sort([1+2])=[3].

And 𝐹(𝑝′)=sort([2+1])=[3].

In the second test case, 𝐹(𝑝)=sort([2+1,1+6,6+5,5+4,4+3])=sort([3,7,11,9,7])=[3,7,7,9,11].

And 𝐹(𝑝′)=sort([1+2,2+5,5+6,6+3,3+4])=sort([3,7,11,9,7])=[3,7,7,9,11].

In the third test case, 𝐹(𝑝)=sort([2+4,4+3,3+1,1+5])=sort([6,7,4,6])=[4,6,6,7].

And 𝐹(𝑝′)=sort([3+1,1+5,5+2,2+4])=sort([4,6,7,6])=[4,6,6,7].

题意:给你个全排列数组a,知道其a[i]+a[i+1]的值。改变顺序后使得所有a[i]+a[i+1]的值仍不变。输出其中一种方法

题解:保持相邻位置值不变即可

代码:

#include <iostream>
#include <algorithm>
using namespace std;
int a[200],b[200],p[200],ans[200];
int t,n;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n;i++)
			cin>>a[i];
		// for(int i=1;i<n;i++)
		// 	b[i]=a[i]+a[i+1];
		for(int i=1;i<=n/2;i++)
			swap(a[i],a[n-i+1]);
		for(int i=1;i<=n;i++)
			cout<<a[i]<<" ";
		cout<<endl;
	}
	return 0;
}

B

B题

B. Array Cancellation
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You’re given an array 𝑎 of 𝑛 integers, such that 𝑎1+𝑎2+⋯+𝑎𝑛=0.

In one operation, you can choose two different indices 𝑖 and 𝑗 (1≤𝑖,𝑗≤𝑛), decrement 𝑎𝑖 by one and increment 𝑎𝑗 by one. If 𝑖<𝑗 this operation is free, otherwise it costs one coin.

How many coins do you have to spend in order to make all elements equal to 0?

Input
Each test contains multiple test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤5000). Description of the test cases follows.

The first line of each test case contains an integer 𝑛 (1≤𝑛≤105) — the number of elements.

The next line contains 𝑛 integers 𝑎1,…,𝑎𝑛 (−109≤𝑎𝑖≤109). It is given that ∑𝑛𝑖=1𝑎𝑖=0.

It is guaranteed that the sum of 𝑛 over all test cases does not exceed 105.

Output
For each test case, print the minimum number of coins we have to spend in order to make all elements equal to 0.

Example
inputCopy
7
4
-3 5 -3 1
2
1 -1
4
-3 2 -3 4
4
-1 1 1 -1
7
-5 7 -6 -4 17 -13 4
6
-1000000000 -1000000000 -1000000000 1000000000 1000000000 1000000000
1
0
outputCopy
3
0
4
1
8
3000000000
0
Note
Possible strategy for the first test case:

Do (𝑖=2,𝑗=3) three times (free), 𝑎=[−3,2,0,1].
Do (𝑖=2,𝑗=1) two times (pay two coins), 𝑎=[−1,0,0,1].
Do (𝑖=4,𝑗=1) one time (pay one coin), 𝑎=[0,0,0,0].

题意:给你个数组,要将数组还原为全为0,每次可选一个数+1,另一个数-1,保证和为0。输出最小消耗硬币。如果i<j && a[i]-1 ,a[j]+1,这种不消耗硬币

题解:小于0数,直接加入,大于0数用于抵消

代码:

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int a[maxn];
int t,n;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		ll ans = 0;
		ll res = 0;
		// res 0 5
		// ans 0 
 		for(int i=1;i<=n;i++)
		{
			if(a[i]<0)
			{
				if(a[i]+res>=0)
					res+=a[i];
				else ans+=abs(a[i]+res),res=0;
			}
			else
			{
				res+=a[i];
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

C题

C题
C. Balanced Bitstring
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A bitstring is a string consisting only of the characters 0 and 1. A bitstring is called 𝑘-balanced if every substring of size 𝑘 of this bitstring has an equal amount of 0 and 1 characters (𝑘2 of each).

You are given an integer 𝑘 and a string 𝑠 which is composed only of characters 0, 1, and ?. You need to determine whether you can make a 𝑘-balanced bitstring by replacing every ? characters in 𝑠 with either 0 or 1.

A string 𝑎 is a substring of a string 𝑏 if 𝑎 can be obtained from 𝑏 by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

Input
Each test contains multiple test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤104). Description of the test cases follows.

The first line of each test case contains two integers 𝑛 and 𝑘 (2≤𝑘≤𝑛≤3⋅105, 𝑘 is even) — the length of the string and the parameter for a balanced bitstring.

The next line contains the string 𝑠 (|𝑠|=𝑛). It is given that 𝑠 consists of only 0, 1, and ?.

It is guaranteed that the sum of 𝑛 over all test cases does not exceed 3⋅105.

Output
For each test case, print YES if we can replace every ? in 𝑠 with 0 or 1 such that the resulting bitstring is 𝑘-balanced, or NO if it is not possible.

Example
inputCopy
9
6 4
100110
3 2
1?1
3 2
1?0
4 4
???
7 4
1?0??1?
10 10
11??11??11
4 2
1??1
4 4
?0?0
6 2
???00
outputCopy
YES
YES
NO
YES
YES
NO
NO
YES
NO
Note
For the first test case, the string is already a 4-balanced bitstring.

For the second test case, the string can be transformed into 101.

For the fourth test case, the string can be transformed into 0110.

For the fifth test case, the string can be transformed into 1100110.

题意:平衡子串,使得每个k子串的0,1数目相同,?可以取代一个

题解:

代码:

#include <iostream>
using namespace std;
int t,n,k;
string s;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>k;
		cin>>s;
		int zer=0,one=0;
		bool chk=true;
		for(int i=0;i<k;i++)
		{
			int tmp=-1;
			for(int j=i;j<n;j+=k)
			{
				if(s[j]!='?')
				{
					if(tmp!=-1 && s[j]-'0'!=tmp)
					{
						chk=false;
						break;
					}
					tmp = s[j]-'0';
				}
			}
			if(tmp!=-1)
			{
				(tmp==0?zer:one)++;
			}
		}
		if(max(zer,one)>k/2)
			chk=false;
		cout<<(chk ? "YES\n":"NO\n");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值