1. 程式人生 > 其它 >CF 1462 簡單的題解

CF 1462 簡單的題解

技術標籤:codeforces演算法codeforces

  • A. Favorite Sequence
  • B. Last Year’s Substring
  • C. Unique Number
  • D. Add to Neighbour and Remove
  • E1. Close Tuples (easy 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)
睡覺咯