Codeforces Round #690 (Div. 3) A-E1

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.
The beginning of the result looks like this (of course, if n≥4).
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
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].

这里比较简单直接放代码

#include<bits/stdc++.h>
using namespace std;
 
int a[305];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	int n,x;
	cin>>n;
	for(int i=0;i<n;++i)
	{
		scanf("%d",&a[i]);
	}
	for(int i=0;i<n;++i)
	{
		if(i)
		printf(" ");
		if(i%2==0)
		printf("%d",a[i/2]);
		else 
		printf("%d",a[n-1-i/2]);
 
	}
	printf("\n");
	}
	getchar();
	getchar();
	return 0;
}

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

这题可以说很难受了,string的API记错了,earse(int s, int length)以为是从第一个参数删到第二个参数的位置,第二个参数是长度,关键是我样例和自己试的几个例子都过了,无语。


#include<bits/stdc++.h>
using namespace std;
 
int a[305];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	int n,x;
	cin>>n;
	string s;
	cin>>s;
	bool flag=false;
	if(s=="2020")
		flag=true;
	else 
	{
		for(int i=0;i<n;++i)
		{
			string tmp=s;
			if(n-4>=0)
			tmp.erase(i,n-4);
			if(tmp=="2020")
			flag=true;
		}
	}
	if(flag)
	cout<<"YES";
	else 
	cout<<"NO";
	cout<<endl;
	}
	getchar();
	getchar();
	return 0;
}

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
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
input
4
1
5
15
50
output
1
5
69
-1

c也比较简单,从个位依次从9到1赋值就可以了

#include<bits/stdc++.h>
using namespace std;
 
int a[15];
int main(){
    int t;
    cin>>t;
    while(t--)
    {
        int x;
        cin>>x;
        if(x>45)
        cout<<"-1";
        else 
        {
        int cnt=0;
        int k=9;
        while(x>k)
        {
            a[cnt++]=k;
            x-=k;
            --k;
        }
        a[cnt]=x;
        for(int i=cnt;i>=0;--i)
        {
            cout<<a[i];
        }
        }
        cout<<endl;
    }
    getchar();
    getchar();
}

D. Add to Neighbour and Remove

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
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.

这题没做出来有点亏,主要是发现合并后的数列,其每个值都是其中连续子串的和,知道这一规律后就不难了

代码

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

int a[3005];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		int sum=0,ans=0;
		for(int i=0;i<n;++i)
		scanf("%d",&a[i]),sum+=a[i];
		for(int i=n;i>=1;--i)
		{
			if(sum%i==0)
			{
				int v=sum/i;
				int tmp=0;
				bool flag=true;
				for(int j=0;j<n;++j)
				{
					tmp+=a[j];
					if(tmp>v)
					{
					flag=false;
					break;
					}
					if(tmp==v)
					tmp=0;
				}
				if(flag)
				{
				ans=n-i;
				break;
				}
			}
		}
		cout<<ans<<endl;
	}
	getchar();
	getchar();
	return 0;
}

E1. Close Tuples (easy version)

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
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
input
4
4
1 2 4 3
4
1 1 1 1
1
1
10
5 6 1 3 2 9 8 1 2 4
output
2
4
0
15

虽然wa了几发好歹做出来了

题意

在给出的数列中找出三个(数字可能有重复)使得他们中最大的数和最小的数的差值<=2,问一共能找出几对

思路

随便找肯定不行,要有一定顺序,因此可以把最小的先定下来,由于顺序对解决问题没有影响,所以可以先排序,然后遍历,a【i】作为三个数中最小的那个,然后再取一个p2,找到第一个大于2的位置,那么其中p2-i-1都可以作为另外两个被选取得数,因此只要从p2-i-1中选两个就行了,通过排列组合的知识,个数为(p2-i-1)*(p2-i-2)/2;把每种情况加起来就行了。
因为是顺序排序的,因此P2在前一种情况下累加就好了。

代码

#include<bits/stdc++.h>
using namespace std;
 
int a[(int)2e5+5];
int main()
{
	#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

int a[(int)2e5+5];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	int n,x;
	cin>>n;
	for(int i=0;i<n;++i)
	scanf("%d",&a[i]);
	sort(a,a+n);
	ll p2=0;
	ll ans=0;
	for(long long i=0;i<n;++i)
	{
		while(p2<=n-1&&i<=n-1&&a[p2]-a[i]<=2)
			p2++;
		if(p2-i>=2)
		ans=ans+(p2-i-1)*(p2-i-2)/2;
	}
	cout<<ans<<endl;
	}
	getchar();
	getchar();
	return 0;
}
}

这里提醒了我一个点,就是运算中,根据优先级优先进行运算,但其数据类型,根据参与运算的数来决定。比如这里我的(p2-i-1)*(p2-i-2),会爆int,即使我ans是long long但是运算时乘法先算,所以还是会爆。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值