CF 1462 简单的题解

  • A. Favorite Sequence
  • B. Last Year’s Substring
  • C. Unique Number
  • D. Add to Neighbour and Remove
  • E1. Close Tuples (easy version)
  • E2. Close Tuples (hard version)

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
input
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
output
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].

题意
给你一个序列,前后分别读入。

思路
定义l,r,while(l<=r) 等于的时候特判一下就行

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double esp = 1e-6;
const double PI = acos(-1);
const int N = 1e5 + 5;

int n,m; 
int a[N]; 

void solve(){	
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	int l = 1, r = n;
	while(l<=r){
		if(l==r) printf("%d ",a[l]);
		else printf("%d %d ",a[l],a[r]);
		l++,r--;
	}
	puts("");
}

int main(){
	int t = 1;
	scanf("%d",&t);
	while(t--){
		solve();
	}
	return 0;
}
/*
5 0 3 0 4
5 5 3 5 4
*/ 

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

Example
input
6
8
20192020
8
22019020
4
2020
5
20002
6
729040
6
200200
output
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.

题意
给你一个字符串,你可以将中间的一部分去掉,只能操作一次,问你能不能搞出"2020"。

思路
前后特判一下就好

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double esp = 1e-6;
const double PI = acos(-1);
const int N = 1e5 + 5;
 
int n,m; 
int a[N]; 
string s;
void solve(){	
	scanf("%d",&n);
	cin>>s;
	if(s[0]=='2'&&s[1]=='0'&&s[n-2]=='2'&&s[n-1]=='0'){
		puts("YES");
		return;
	}
	if(s[0]=='2'&&s[n-3]=='0'&&s[n-2]=='2'&&s[n-1]=='0'){
		puts("YES");
		return;
	}
	if(s[n-4]=='2'&&s[n-3]=='0'&&s[n-2]=='2'&&s[n-1]=='0'){
		puts("YES");
		return;
	}
	if(s[0]=='2'&&s[1]=='0'&&s[2]=='2'&&s[3]=='0'){
		puts("YES");
		return;
	}
	if(s[0]=='2'&&s[1]=='0'&&s[2]=='2'&&s[n-1]=='0'){
		puts("YES");
		return;
	}
	puts("NO");
}
 
int main(){
	int t = 1;
	scanf("%d",&t);
	while(t--){
		solve();
	}
	return 0;
}
/*
5 0 3 0 4
5 5 3 5 4
*/ 

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

题意
给你一个数,求最小的整数满足数字和等于这个数,且没有重复的数,如果没有输出-1

思路
打表,很快就能找出规律,因为只有45个数有值,所以开个数组记录下就行。

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double esp = 1e-6;
const double PI = acos(-1);
const int N = 1e5 + 5;

int n,m; 
int a[N]; 
int vis[55]={0,1,2,3,4,5,6,7,8,9,19,29,39,49,59,69,79,89,189,289,389,489,589,689,789,1789,2789,3789,4789,5789,6789,16789,26789,36789,46789,56789,156789,256789,356789,456789,1456789,2456789,3456789,13456789,23456789,123456789,-1,-1,-1,-1,-1};
string s;
void solve(){	
	scanf("%d",&m);
	printf("%d\n",vis[m]);
}

int main(){
	int t = 1;
	scanf("%d",&t);
	while(t--){
		solve();
	}
	return 0;
}
/*
5 0 3 0 4
5 5 3 5 4
*/ 

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
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
input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
output
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.

题意
你可以将序列中的数向两个相邻的数依附,即加上这个数,然后删了这个数,求序列数字都相同的最小步数。

思路
从结果来考虑,我们将序列中最大的数到所有的和所有能整除和的数判断一边就行,模拟看下对不对,对就输出
3000的数据量,n^2随便过!

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double esp = 1e-6;
const double PI = acos(-1);
const int N = 1e5 + 5;

int n,m,x; 
int a[N]; 
string s;
void solve(){	
	scanf("%d",&n);
	int maxx = 0,sum = 0;
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		a[i]=x;
		sum += x;
		maxx = max(maxx, x);
	}
	for(int i=maxx;i<=sum;i++){
		if(sum%i==0){
			bool flag = true;
			for(int j=1;j<=n;j++){
				if(a[j]<i){
					int summ = 0;
					for(int k=j;k<=n;k++,j++){
						summ+=a[k];
						if(summ == i) break;
						if(summ > i){
							flag = false;
							break;
						} 
					}
				} 
			}
			if(flag){
				printf("%d\n",n-sum/i);
				return;
			}
		}
	}
}

int main(){
	int t = 1;
	scanf("%d",&t);
	while(t--){
		solve();
	}
	return 0;
}
/*
5 0 3 0 4
5 5 3 5 4
*/ 

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

思路
基础组合数…

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double esp = 1e-6;
const double PI = acos(-1);
const int N = 2e5 + 5;
int n,m,x; 
int a[N]; 
int vis[N],v[N];
string s;
void solve(){	
	scanf("%d",&n);
	for(int i=0;i<=n+5;i++) vis[i]=v[i]=0;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		a[i];
		vis[a[i]]++;
	}
	ll res = 0;
	for(int i=1;i<=n;i++){
		if(!v[a[i]]){
			ll t1 = vis[a[i]];
			ll t2 = vis[a[i]+1];
			ll t3 = vis[a[i]+2];
			res += t1 * t2 * t3; 
			res += t1*(t1-1)*t2/2;
			res += t2*(t2-1)*t1/2;
			res += t1*(t1-1)*t3/2;
			res += t3*(t3-1)*t1/2;
			res += t1*(t1-1)*(t1-2)/6;
		}
		v[a[i]]=1;
	}
	printf("%lld\n",res);
}

int main(){
	int t = 1;
	scanf("%d",&t);
	while(t--){
		solve();
	}
	return 0;
}
/*
5 0 3 0 4
5 5 3 5 4
*/ 

E2. Close Tuples (hard version)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 +7;
const double esp = 1e-6;
const double PI = acos(-1);
const int N = 2e5 + 10;
int n,m,k; 
int a[N];
ll f[N]; 
ll qpow(ll a,ll b){
	ll ans = 1,base = a;
	while(b){
		if(b&1) ans = ans * base % mod;
		base = base * base % mod;
		b>>=1; 
	}
	return ans;
}
void init(){
	f[0]=1;
	for(int i=1;i<=2e5;i++){
		f[i]=f[i-1]*i%mod;
	} 
}
ll cal(ll n,ll m){
	if(n<m) return 0; 
 	return 1ll*f[n]*qpow(f[m],mod-2)%mod*qpow(f[n-m],mod-2)%mod;
}
void solve(){	
	scanf("%d %d %d",&n,&m,&k);
	for(int i=1;i<=n;i++) 
		scanf("%d",&a[i]);
	sort(a,a+n+1);
	ll res = 0;
	int l =1;
	for(int i=1;i<=n;i++){
		while(a[i]-a[l]>k) l++;
		//cout<<l<<" "<<i<<" "<<m-1<<endl; 
		res += cal(i-l,m-1);
		res%=mod; 
	}
	printf("%lld\n",res);
}

int main(){
	int t = 1;
	init();
	scanf("%d",&t);
	while(t--){
		solve();
	}
	return 0;
}
/*
5 0 3 0 4
5 5 3 5 4
*/ 
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值