Codeforces Round #690 (Div. 3) 题解(A-E2)

Codeforces Round #690 (Div. 3) 题解(A-E2)

A. Favorite Sequence

题目

Polycarp has a favorite sequence a[1…n] consisting of n integers. He wrote it out on the whiteboard as follows:

he wrote the number a1 to the left side (at the beginning of the whiteboard);
he wrote the number a2 to the right side (at the end of the whiteboard);
then as far to the left as possible (but to the right from a1), he wrote the number a3;
then as far to the right as possible (but to the left from a2), he wrote the number a4;
Polycarp continued to act as well, until he wrote out the entire sequence on the whiteboard.
Codeforces Round #690 (Div. 3) 题解(A-E2)
For example, if n=7 and a=[3,1,4,1,5,9,2], then Polycarp will write a sequence on the whiteboard [3,4,5,2,9,1,1].

You saw the sequence written on the whiteboard and now you want to restore Polycarp's favorite sequence.

input & output

Input
The first line contains a single positive integer t (1≤t≤300) — the number of test cases in the test. Then t test cases follow.

The first line of each test case contains an integer n (1≤n≤300) — the length of the sequence written on the whiteboard.

The next line contains n integers b1,b2,…,bn (1≤bi≤109) — the sequence written on the whiteboard.

Output
Output t answers to the test cases. Each answer — is a sequence a that Polycarp wrote out on the whiteboard.

Example
inputCopy
6
7
3 4 5 2 9 1 1
4
9 2 7 1
11
8 4 3 1 2 7 8 7 9 4 2
1
42
2
11 7
8
1 1 1 1 1 1 1 1
outputCopy
3 1 4 1 5 9 2
9 1 2 7
8 2 4 4 3 9 1 7 2 8 7
42
11 7
1 1 1 1 1 1 1 1
Note
In the first test case, the sequence a matches the sequence from the statement. The whiteboard states after each step look like this:

[3]⇒[3,1]⇒[3,4,1]⇒[3,4,1,1]⇒[3,4,5,1,1]⇒[3,4,5,9,1,1]⇒[3,4,5,2,9,1,1].

题意

按照a1 a3 ... a4 a2 (题目描述的数列排列方式) 给出一个数列,求出原先的a1 a2 ... an 这个数列

思路

根据给出数列直接取即可

代码

#include <bits/stdc++.h>
using namespace std;
 
const int maxn = 1000+10;
 
int a[maxn] , b[maxn];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		int n;
		cin>>n;
		for(int i=1;i<=n;i++)
		cin>>a[i];
		int l = 1 , r = n , sum = n , now = 1;
		int f = 1;
		while(sum--)
		{
			if(f)
			b[now++] = a[l] , l++ , f = 0;
			else
			b[now++] = a[r] , r-- , f = 1;
		}
		for(int i=1;i<=n;i++) cout<<b[i]<<" ";
		cout<<"\n";
	} 
} 

B. Last Year's Substring

题目

Polycarp has a string s[1…n] of length n consisting of decimal digits. Polycarp performs the following operation with the string s no more than once (i.e. he can perform operation 0 or 1 time):

Polycarp selects two numbers i and j (1≤i≤j≤n) and removes characters from the s string at the positions i,i+1,i+2,…,j (i.e. removes substring s[i…j]). More formally, Polycarp turns the string s into the string s1s2…si−1sj+1sj+2…sn.
For example, the string s="20192020" Polycarp can turn into strings:

"2020" (in this case (i,j)=(3,6) or (i,j)=(1,4));
"2019220" (in this case (i,j)=(6,6));
"020" (in this case (i,j)=(1,5));
other operations are also possible, only a few of them are listed above.
Polycarp likes the string "2020" very much, so he is wondering if it is possible to turn the string s into a string "2020" in no more than one operation? Note that you can perform zero operations.

input & output

Input
The first line contains a positive integer t (1≤t≤1000) — number of test cases in the test. Then t test cases follow.

The first line of each test case contains an integer n (4≤n≤200) — length of the string s. The next line contains a string s of length n consisting of decimal digits. It is allowed that the string s starts with digit 0.

Output
For each test case, output on a separate line:

"YES" if Polycarp can turn the string s into a string "2020" in no more than one operation (i.e. he can perform 0 or 1 operation);
"NO" otherwise.
You may print every letter of "YES" and "NO" in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

Example
inputCopy
6
8
20192020
8
22019020
4
2020
5
20002
6
729040
6
200200
outputCopy
YES
YES
YES
NO
NO
NO
Note
In the first test case, Polycarp could choose i=3 and j=6.

In the second test case, Polycarp could choose i=2 and j=5.

In the third test case, Polycarp did not perform any operations with the string.

题意

从一个字符串中 经过0次或者1次操作(去除字符串中连续的字串)获取串 "2020"

思路

只有
2020在开头
2020在结尾
202XXX0
20XXX20
2XXX020
这几种情况,枚举每种情况即可。

代码

#include <bits/stdc++.h>
using namespace std;
 
 
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		string s;
		cin>>s;
		int len = s.size();
		if(len<4)
		{
			cout<<"NO\n";
			continue;
		}
		else
		{
			if(s[0]=='2' && s[1]=='0' && s[2]=='2' && s[3] =='0')
			{
				cout<<"YES\n";
				continue;
			}
			else if(s[len-4]=='2' && s[len-3]=='0' && s[len-2]=='2' && s[len-1] == '0')
			{
				cout<<"YES\n";
				continue;
			}
			else if(s[0]=='2' && s[len-3]=='0' && s[len-2]=='2' && s[len-1] == '0')
			{
				cout<<"YES\n";
				continue;
			}
			else if(s[0]=='2' && s[1]=='0' && s[len-2]=='2' && s[len-1] == '0')
			{
				cout<<"YES\n";
				continue;
			}
			else if(s[0]=='2' && s[1]=='0' && s[2]=='2' && s[len-1] == '0')
			{
				cout<<"YES\n";
				continue;
			}
			else
			cout<<"NO\n";
		}
	} 
} 

C. Unique Number

题目

You are given a positive number x. Find the smallest positive integer number that has the sum of digits equal to x and all digits are distinct (unique).

input & output

Input
The first line contains a single positive integer t (1≤t≤50) — the number of test cases in the test. Then t test cases follow.

Each test case consists of a single integer number x (1≤x≤50).

Output
Output t answers to the test cases:

if a positive integer number with the sum of digits equal to x and all digits are different exists, print the smallest such number;
otherwise print -1.
Example
inputCopy
4
1
5
15
50
outputCopy
1
5
69
-1

题意

求出最小的一个各位数之和等于x 且各位数均不相同的数。

思路

枚举所有情况,吧最小的那个放在前面即可。

代码

#include <bits/stdc++.h>
using namespace std;
 
 
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int x;
		cin>>x;
		if(x<=9)
		cout<<x<<"\n";
		else
		{
			if(x<=17)
			cout<<x-9<<""<<9<<"\n";
			else if(x<=24)
			cout<<x-17<<""<<8<<""<<9<<"\n";
			else if(x<=30)
			cout<<x-24<<""<<7<<""<<8<<""<<9<<"\n";
			else if(x<=35)
			cout<<x-30<<""<<6<<""<<7<<""<<8<<""<<9<<"\n";
			else if(x<=39)
			cout<<x-35<<""<<5<<""<<6<<""<<7<<""<<8<<""<<9<<"\n";
			else if(x<=42)
			cout<<x-39<<""<<4<<""<<5<<""<<6<<""<<7<<""<<8<<""<<9<<"\n";
			else if(x<=44)
			cout<<x-42<<""<<3<<""<<4<<""<<5<<""<<6<<""<<7<<""<<8<<""<<9<<"\n";
			else if(x<=45)
			cout<<x-44<<""<<2<<""<<3<<""<<4<<""<<5<<""<<6<<""<<7<<""<<8<<""<<9<<"\n";
			else 
			cout<<-1<<"\n";
		}
	} 
} 

D. Add to Neighbour and Remove

题目

Polycarp was given an array of a[1…n] of n integers. He can perform the following operation with the array a no more than n times:

Polycarp selects the index i and adds the value ai to one of his choice of its neighbors. More formally, Polycarp adds the value of ai to ai−1 or to ai+1 (if such a neighbor does not exist, then it is impossible to add to it).
After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.

For example, if Polycarp has an array a=[3,1,6,6,2], then it can perform the following sequence of operations with it:

Polycarp selects i=2 and adds the value ai to (i−1)-th element: a=[4,6,6,2].
Polycarp selects i=1 and adds the value ai to (i+1)-th element: a=[10,6,2].
Polycarp selects i=3 and adds the value ai to (i−1)-th element: a=[10,8].
Polycarp selects i=2 and adds the value ai to (i−1)-th element: a=[18].
Note that Polycarp could stop performing operations at any time.

Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all ai are equal to each other).

input & output

Input
The first line contains a single integer t (1≤t≤3000) — the number of test cases in the test. Then t test cases follow.

The first line of each test case contains a single integer n (1≤n≤3000) — the length of the array. The next line contains n integers a1,a2,…,an (1≤ai≤105) — array a.

It is guaranteed that the sum of n over all test cases does not exceed 3000.

Output
For each test case, output a single number — the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).

Example
inputCopy
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
outputCopy
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):

[3,1,6,6,2] −→−−−−−−−i=4, add to left [3,1,12,2] −→−−−−−−−−i=2, add to right [3,13,2] −→−−−−−−−−i=1, add to right [16,2] −→−−−−−−−i=2, add to left [18]. All elements of the array [18] are the same.

In the second test case of the example, the answer can be constructed like this (just one way among other ways):

[1,2,2,1] −→−−−−−−−−i=1, add to right [3,2,1] −→−−−−−−−i=3, add to left [3,3]. All elements of the array [3,3] are the same.

In the third test case of the example, Polycarp doesn't need to perform any operations since [2,2,2] contains equal (same) elements only.

In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):

[6,3,2,1] −→−−−−−−−−i=3, add to right [6,3,3] −→−−−−−−−i=3, add to left [6,6]. All elements of the array [6,6] are the same.

题意

给你一个数组,每次操作可以选择数组中的一项,加上他左边一项或者右边一项的值,然后把加上的那一项去掉,问得到一个所有数据项都相等的数组的最小操作个数是几次。

思路

先求出数组的和,从n到1枚举和可以整除的情况(n是sum最大可以整除的情况),如果可以整除的话就考虑当前的数组可不可以通过题目所给操作全都变成sum/n这个数,如果可以,那么当前的n-i就是最小操作次数,因为每次减少一个数都需要一次操作,从n到1的话找到第一个就是最小值

代码

#include <bits/stdc++.h>
using namespace std;
#define xiaobai false
#define plog if(xiaobai) clog
 
const int maxn = 3000+100;
int a[maxn];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		long long sum = 0;
		cin>>n;
		for(int i=1;i<=n;i++) cin>>a[i] , sum += a[i];
		int ans = 0 , flag = 0;
		for(int i=n;i>=1;i--)
		{
			if(sum % i == 0)
			{
//				cout<<sum/i<<endl;
				long long cnt = 0;
				for(int j=1;j<=n;j++)
				{
					cnt += a[j];
					plog<<"before: "<<cnt<<endl;
					if(cnt==sum/i)
					cnt=0;
					plog<<"after: "<<cnt<<endl;
				}
				if(cnt == 0)
				{
					ans = n - i;
					flag = 1;
				}
			}
			if(flag)
			break;
		}
		cout<<ans<<"\n";
	}
} 

E2. Close Tuples (hard version)

题目

This is the hard version of this problem. The only difference between the easy and hard versions is the constraints on k and m. In this version of the problem, you need to output the answer by modulo 109+7.

You are given a sequence a of length n consisting of integers from 1 to n. The sequence may contain duplicates (i.e. some elements can be equal).

Find the number of tuples of m elements such that the maximum number in the tuple differs from the minimum by no more than k. Formally, you need to find the number of tuples of m indices i1<i2<…<im, such that

max(ai1,ai2,…,aim)−min(ai1,ai2,…,aim)≤k.
For example, if n=4, m=3, k=2, a=[1,2,4,3], then there are two such triples (i=1,j=2,z=4 and i=2,j=3,z=4). If n=4, m=2, k=1, a=[1,1,1,1], then all six possible pairs are suitable.

As the result can be very large, you should print the value modulo 109+7 (the remainder when divided by 109+7).

input & output

Input
The first line contains a single integer t (1≤t≤2⋅105) — the number of test cases. Then t test cases follow.

The first line of each test case contains three integers n, m, k (1≤n≤2⋅105, 1≤m≤100, 1≤k≤n) — the length of the sequence a, number of elements in the tuples and the maximum difference of elements in the tuple.

The next line contains n integers a1,a2,…,an (1≤ai≤n) — the sequence a.

It is guaranteed that the sum of n for all test cases does not exceed 2⋅105.

Output
Output t answers to the given test cases. Each answer is the required number of tuples of m elements modulo 109+7, such that the maximum value in the tuple differs from the minimum by no more than k.

Example
inputCopy
4
4 3 2
1 2 4 3
4 2 1
1 1 1 1
1 1 1
1
10 4 3
5 6 1 3 2 9 8 1 2 4
outputCopy
2
6
1
20

题意

给三个数n,m,k和长度为n的数组,要在长度为n的数组中找出一个m元组,使得这个元组中的最大值和最小值之差小于等于k 统计所有符合这个条件的元组的个数

思路

类似滑动窗口的一个思路,用一个双端队列,先对数组排序,每次先从队尾push进去一个值,如果队尾,即刚push进去的这个值 - 队首, 就pop掉队首的那个数,一直到满足条件为止,因为数列是有序的,所以可以这样操作。 然后用组合数学排列数统计答案,每次统计的是C(q.size()-1,m-1) 这样统计的目的是假设最后一位固定,从前面的几个数中取,就不会重复取,否则会重复取。由于此题带有模数,所以使用费马小定理求a的逆元是a^(p-2) 除以一个数等于乘以这个数的逆元,故求出。

代码

#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
#define xiaobai false
#define plog if(xiaobai) clog
 
const ll MOD = 1e9+7;
const int MAXN= 2e5+5;
 
const int maxn=2e5+5;
const ll mod=1e9+7;
ll fac[maxn],inv[maxn];
ll pow_mod(ll a,ll n)
{
    ll ret =1;
    while(n)
    {
        if(n&1) ret=ret*a%mod;
          a=a*a%mod;
          n>>=1;
    }
    return ret;
}
void init()
{
    fac[0]=1;
    for(int i=1;i<maxn;i++)
    {
        fac[i]=fac[i-1]*i%mod;
    }
}
ll Cc(ll x, ll y)
{
    return fac[x]*pow_mod(fac[y]*fac[x-y]%mod,mod-2)%mod;
}
 
ll a[MAXN]; 
int main()
{
//	deque<ll>q;
	init();
	int t;
	cin>>t;
	while(t--)
	{
		deque<ll>q;
		int n,m,k;
		cin>>n>>m>>k;
		ll ans = 0;
		for(int i=1;i<=n;i++) cin>>a[i];
		sort(a+1,a+1+n);
		for(int i=1;i<=n;i++) plog<<a[i]<<" ";
		plog<<endl;
		if(m==1)
		{
			cout<<n<<"\n";
			continue;
		}
		for(int i=1;i<=n;i++)
		{
			q.push_back(a[i]);
			while(!q.empty() && q.back() - q.front() > k) //如果最后面的-前面的>k 就pop前面的
			q.pop_front();
			if(q.size() >= m)
			{
				ans += Cc(q.size()-1,m-1); // 假设最后一位是结尾,固定后可以去重
				ans %= MOD; 
			}
		}
		cout<<ans<<"\n";
	}
} 

E1. Close Tuples (easy version)

题目

This is the easy version of this problem. The only difference between easy and hard versions is the constraints on k and m (in this version k=2 and m=3). Also, in this version of the problem, you DON'T NEED to output the answer by modulo.

You are given a sequence a of length n consisting of integers from 1 to n. The sequence may contain duplicates (i.e. some elements can be equal).

Find the number of tuples of m=3 elements such that the maximum number in the tuple differs from the minimum by no more than k=2. Formally, you need to find the number of triples of indices i<j<z such that

max(ai,aj,az)−min(ai,aj,az)≤2.
For example, if n=4 and a=[1,2,4,3], then there are two such triples (i=1,j=2,z=4 and i=2,j=3,z=4). If n=4 and a=[1,1,1,1], then all four possible triples are suitable.

input & output

Input
The first line contains a single integer t (1≤t≤2⋅105) — the number of test cases. Then t test cases follow.

The first line of each test case contains an integer n (1≤n≤2⋅105) — the length of the sequence a.

The next line contains n integers a1,a2,…,an (1≤ai≤n) — the sequence a.

It is guaranteed that the sum of n for all test cases does not exceed 2⋅105.

Output
Output t answers to the given test cases. Each answer is the required number of triples of elements, such that the maximum value in the triple differs from the minimum by no more than 2. Note that in difference to the hard version of the problem, you don't need to output the answer by modulo. You must output the exact value of the answer.

Example
inputCopy
4
4
1 2 4 3
4
1 1 1 1
1
1
10
5 6 1 3 2 9 8 1 2 4
outputCopy
2
4
0
15

题意

上面那个题的m变成3 k变成2 没有mod数

思路

由于没有mod数 所以不能使用上面那个阶乘的求法,需要换一个求法,用朴素求法即可。

代码

#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
#define xiaobai false
#define plog if(xiaobai) clog
 
const int MAXN= 2e5+5;
 
const int maxn=2e5+5;
 
long long cal( long long n, long long m) {
     long long i, a, b, p;
     if (n<m) {
         i=m;
         m=n;
         n=i;
     }
     p=1;
     a=n-m<m?n-m:m;
     b=n-m>m?n-m:m;
     for (i=1; i<=a; i++)
         p+=(p*b/i);
     return p;
}
 
ll a[MAXN];
int main()
{
//	deque<ll>q;
	int t;
	cin>>t;
	while(t--)
	{
		deque<ll>q;
		int n,m,k;
		cin>>n;
		m = 3;
		k = 2;
		ll ans = 0;
		for(int i=1;i<=n;i++) cin>>a[i];
		sort(a+1,a+1+n);
		for(int i=1;i<=n;i++) plog<<a[i]<<" ";
		plog<<endl;
		for(int i=1;i<=n;i++)
		{
			q.push_back(a[i]);
			while(!q.empty() && q.back() - q.front() > k) //如果最后面的-前面的>k 就pop前面的
			q.pop_front();
			if(q.size() >= m)
			{
				ans += cal(q.size()-1,m-1); // 假设最后一位是结尾,固定后可以去重
			}
		}
		cout<<ans<<"\n";
	}
} 
上一篇:棋子游戏


下一篇:Codeforces Round #625 (Div. 2) A ~ D