【CodeForces】Round #579 (Div. 3)……

只做了6个题……

目录

A. Circle of Students 【传送门】

B. Equal Rectangles 【传送门】

C. Common Divisors 【传送门】

D2. Remove the Substring (hard version) 【传送门】

E. Boxers 【传送门】

F2. Complete the Projects (hard version) 【传送门】


A. Circle of Students 【传送门】

There are nn students standing in a circle in some order. The index of the ii-th student is pipi. It is guaranteed that all indices of students are distinct integers from 11 to nn (i. e. they form a permutation).

Students want to start a round dance. A clockwise round dance can be started if the student 22 comes right after the student 11 in clockwise order (there are no students between them), the student 33 comes right after the student 22 in clockwise order, and so on, and the student nncomes right after the student n−1n−1 in clockwise order. A counterclockwise round dance is almost the same thing — the only difference is that the student ii should be right after the student i−1i−1 in counterclockwise order (this condition should be met for every ii from 22 to nn).

For example, if the indices of students listed in clockwise order are [2,3,4,5,1][2,3,4,5,1], then they can start a clockwise round dance. If the students have indices [3,2,1,4][3,2,1,4] in clockwise order, then they can start a counterclockwise round dance.

Your task is to determine whether it is possible to start a round dance. Note that the students cannot change their positions before starting the dance; they cannot swap or leave the circle, and no other student can enter the circle.

You have to answer qq independent queries.

Input

The first line of the input contains one integer qq (1≤q≤2001≤q≤200) — the number of queries. Then qq queries follow.

The first line of the query contains one integer nn (1≤n≤2001≤n≤200) — the number of students.

The second line of the query contains a permutation of indices p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n), where pipi is the index of the ii-th student (in clockwise order). It is guaranteed that all pipi are distinct integers from 11 to nn (i. e. they form a permutation).

Output

For each query, print the answer on it. If a round dance can be started with the given order of students, print "YES". Otherwise print "NO".

Example

input

5
4
1 2 3 4
3
1 3 2
5
1 2 3 5 4
1
1
5
3 2 1 5 4

output

YES
YES
NO
YES
YES

题目大意:给出n个学生,他们可以顺时针坐在一起,也可以逆时针坐在一起,问给出的排列是否满足这种关系。
分析:只需要判断某个数字相邻的数字是否等于它自身加一就可以了,其中i==1或者i==n,a[i]==1或a[i]==n时另行考虑。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
#define MAXN 250
#define mem(a,b) memset(a,b,sizeof(a))
#define sscc ios::sync_with_stdio(false)
int a[MAXN];
 
int main(){
	sscc;
	int T;
	cin >> T;
	while(T--){
		int n;
		mem(a,0);
//		int a[MAXN];
		cin >> n;
		for(int i=1;i<=n;i++)
			cin >> a[i];
		bool flag=1;
		for(int i=1;i<=n;i++){
			if(i==1){
				if(a[i]==1){
					if(a[n]!=n&&a[i+1]!=n){
						
						//cout << a[n] << endl;
						flag=0;
						break;
					}
				}else if(a[i]==n){
					if(a[n]!=1&&a[i+1]!=1){
						flag=0;
						break;
					}
				}else{
					if(a[n]!=a[i]+1&&a[i+1]!=a[i]+1){
						flag=0;
						break;
					}
				}
			}else if(i==n){
				if(a[i]==1){
					if(a[1]!=n&&a[i-1]!=n){
						flag=0;
						break;
					}
				}else if(a[i]==n){
					if(a[1]!=1&&a[i-1]!=1){
						flag=0;
						break;
					}
				}else{
					if(a[1]!=a[i]+1&&a[i-1]!=a[i]+1){
						flag=0;
						break;
					}
				}
			}else{
				if(a[i]==1){
					if(a[i-1]!=n&&a[i+1]!=n){
						flag=0;
						break;
					}
				}else if(a[i]==n){
					if(a[i-1]!=1&&a[i+1]!=1){
						flag=0;
						break;
					}
				}else{
					if(a[i-1]!=a[i]+1&&a[i+1]!=a[i]+1){
						flag=0;
						break;
					}
				}
			}
		}
		if(flag)
			cout << "YES\n";
		else
			cout << "NO\n";
	}
	return 0;
}

B. Equal Rectangles 【传送门】

You are given 4n4n sticks, the length of the ii-th stick is aiai.

You have to create nn rectangles, each rectangle will consist of exactly 44 sticks from the given set. The rectangle consists of four sides, opposite sides should have equal length and all angles in it should be right. Note that each stick can be used in only one rectangle. Each stick should be used as a side, you cannot break the stick or use it not to the full length.

You want to all rectangles to have equal area. The area of the rectangle with sides aa and bb is a⋅ba⋅b.

Your task is to say if it is possible to create exactly nn rectangles of equal area or not.

You have to answer qq independent queries.

Input

The first line of the input contains one integer qq (1≤q≤5001≤q≤500) — the number of queries. Then qq queries follow.

The first line of the query contains one integer nn (1≤n≤1001≤n≤100) — the number of rectangles.

The second line of the query contains 4n4n integers a1,a2,…,a4na1,a2,…,a4n (1≤ai≤1041≤ai≤104), where aiai is the length of the ii-th stick.

Output

For each query print the answer to it. If it is impossible to create exactly nn rectangles of equal area using given sticks, print "NO". Otherwise print "YES".

Example

input

5
1
1 1 10 10
2
10 5 2 10 1 1 2 5
2
10 5 1 10 5 1 1 1
2
1 1 1 1 1 1 1 1
1
10000 10000 10000 10000

output

YES
YES
NO
YES
YES

题目大意:给出4*n个边,判断是否能二对二组成相等矩形。
分析:给所有边从小到大排序,从第一个(i)开始,若a[i]!=a[i+2]则不能组成矩形输出“NO”,然后头尾相乘,如果两两相对不等于a[1]*a[4*n]则不成立,输出“NO”

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
#define MAXN 500
#define mem(a,b) memset(a,b,sizeof(a))
#define sscc ios::sync_with_stdio(false)
typedef unsigned long long ull;
 
//set<ull> a;
ull r[MAXN];
 
int main(){
	sscc;
	int T;
	cin >> T;
	while(T--){
		mem(r,0);
		int nn;
		cin >> nn;
		int n = 4*nn;
		for(int i=1;i<=n;i++)
			cin >> r[i];
		sort(r+1,r+1+n);
		bool flag=false;
		for(int i=1;i<=n;i+=2)
			if(r[i]!=r[i+1]){
				flag=true;
				break;
			}
		if(flag){
			cout << "NO\n";
			continue;
		}
		int temp=n;
		int ans=r[1]*r[temp];
		temp--;
		for(int i=2;i<=temp;i++){
			if(r[i]*r[temp]!=ans){
				flag=true;
				continue;
			}
			temp--;
		}
		if(flag)
			cout << "NO\n";
		else
			cout << "YES\n";
	}
	return 0;
}
 
/*
1
56
7 1 1 7 7 7 7 1 1 7 1 1 1 7 1 7 7 7 7 7 1 1 1 1 7 7 7 1 1 7 7 7 7 7 7 7 1 7
 1 7 7 1 1 1 1 7 1 7 7 7 7 1 7 7 7 7 7 1 1 1 1 1 7 1 1 7 1 7 1 7 7 7 7 7 1 
 1 1 1 7 1 1 7 1 7 1 1 7 7 7 1 1 1 1 7 1 1 7 1 1 1 1 7 1 7 1 7 7 7 1 7 7 1 
 1 7 7 7 7 1 1 1 1 1 7 7 1 7 1 1 7 1 7 1 7 1 1 1 1 7 1 7 1 1 7 1 1 1 7 1 1 
 1 7 7 1 1 7 1 7 7 1 7 7 7 1 1 7 1 1 7 1 1 1 7 7 7 1 1 7 7 1 1 1 1 1 7 7 7 
 7 7 7 7 1 1 7 7 7 7 1 1 1 7 7 1 7 1 7 1 7 1 7 1 1 7 7 1 1 1 7 1 7 7 7 1 1 7
NO
*/

C. Common Divisors 【传送门】

You are given an array aa consisting of nn integers.

Your task is to say the number of such positive integers xx such that xx divides each number from the array. In other words, you have to find the number of common divisors of all elements in the array.

For example, if the array aa will be [2,4,6,2,10][2,4,6,2,10], then 11 and 22 divide each number from the array (so the answer for this test is 22).

Input

The first line of the input contains one integer nn (1≤n≤4⋅1051≤n≤4⋅105) — the number of elements in aa.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤10121≤ai≤1012), where aiai is the ii-th element of aa.

Output

Print one integer — the number of such positive integers xx such that xx divides each number from the given array (in other words, the answer is the number of common divisors of all elements in the array).

Examples

input

5
1 2 3 4 5

output

1

input

6
6 90 12 18 30 18

output

4

题目大意:给出n个数,求共同公因数的个数。

分析:求出最大公约数,然后最大公约数的能被整除的因数的个数,即是最后的答案。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
#define MAXN 4*100000+5
#define INF 1<<30
#define mem(a,b) memset(a,b,sizeof(a))
#define sscc ios::sync_with_stdio(false) 
typedef unsigned long long ull;
 
int main(){
	sscc;
	int n;
	cin >> n;
	ull x;
	cin >> x;
	for(ull i=2;i<=n;i++){
		ull y;
		cin >> y;
		x=__gcd(x,y); 
	}
	set<ull> s;
	for(ull i=1;i*i<=x;i++){
		if(x%i==0){
			s.insert(i);
			s.insert(x/i);
		}
	}
	cout << s.size() << endl;
	return 0;
}

D2. Remove the Substring (hard version) 【传送门】

The only difference between easy and hard versions is the length of the string.

You are given a string ss and a string tt, both consisting only of lowercase Latin letters. It is guaranteed that tt can be obtained from ss by removing some (possibly, zero) number of characters (not necessary contiguous) from ss without changing order of remaining characters (in other words, it is guaranteed that tt is a subsequence of ss).

For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".

You want to remove some substring (contiguous subsequence) from ss of maximum possible length such that after removing this substring tt will remain a subsequence of ss.

If you want to remove the substring s[l;r]s[l;r] then the string ss will be transformed to s1s2…sl−1sr+1sr+2…s|s|−1s|s|s1s2…sl−1sr+1sr+2…s|s|−1s|s| (where |s||s| is the length of ss).

Your task is to find the maximum possible length of the substring you can remove so that tt is still a subsequence of ss.

Input

The first line of the input contains one string ss consisting of at least 11 and at most 2⋅1052⋅105 lowercase Latin letters.

The second line of the input contains one string tt consisting of at least 11 and at most 2⋅1052⋅105 lowercase Latin letters.

It is guaranteed that tt is a subsequence of ss.

Output

Print one integer — the maximum possible length of the substring you can remove so that tt is still a subsequence of ss.

Examples

input

bbaba
bb

output

3

input

baaba
ab

output

2

input

abcde
abcde

output

0

input

asdfasdf
fasd

output

3

题目大意:求在主串中去掉的“最大区间和”最后等于字串

分析:我最开始以位是kmp……,结果是简单贪心。从头扫一遍主串,把相等的字母存储在数组a,倒着扫一遍主串,把相同的字母存储在数组b。然后ans=max(b[0],(int)s.size()-1-(int)c.size()),max里面的两个分别代表着前面与后面要去掉的区间…取最大的一块儿,接着通过一个循环判断中间的区间。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
#define MAXN 2*100000+5
#define INF 1<<30
#define mem(a,b) memset(a,b,sizeof(a))
#define sscc ios::sync_with_stdio(false) 
typedef unsigned long long ull;
 
int main(){
	sscc;
	string s,c;
	cin >> s >> c;
	int a[MAXN],b[MAXN];
	mem(a,0);
	mem(b,0);
	int j=0;
	for(int i=0;i<s.size();i++){
		if(j>=c.size())
			break;
		if(s[i]==c[j]){
			a[j]=i;
			j++;
		}
	}
	j=c.size()-1;
	for(int i=s.size()-1;i>=0;i--){
		if(j<0)
			break;
		if(s[i]==c[j]){
			b[j]=i;
			j--;
		}
	}
	int len=c.size();
	int ans=0;
	ans=max(b[0],(int)s.size()-a[len-1]-1);
	for(int i=0;i<len-1;i++){
		ans=max(ans,b[i+1]-a[i]-1);
	}
	cout << ans << endl;
	return 0;
}

E. Boxers 【传送门】

There are nn boxers, the weight of the ii-th boxer is aiai. Each of them can change the weight by no more than 11 before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number.

It is necessary to choose the largest boxing team in terms of the number of people, that all the boxers' weights in the team are different (i.e. unique).

Write a program that for given current values ​aiai will find the maximum possible number of boxers in a team.

It is possible that after some change the weight of some boxer is 150001150001 (but no more).

Input

The first line contains an integer nn (1≤n≤1500001≤n≤150000) — the number of boxers. The next line contains nn integers a1,a2,…,ana1,a2,…,an, where aiai (1≤ai≤1500001≤ai≤150000) is the weight of the ii-th boxer.

Output

Print a single integer — the maximum possible number of people in a team.

Examples

input

Copy

4
3 2 4 1

output

Copy

4

input

Copy

6
1 1 1 4 4 4

output

Copy

5

Note

In the first example, boxers should not change their weights — you can just make a team out of all of them.

In the second example, one boxer with a weight of 11 can be increased by one (get the weight of 22), one boxer with a weight of 44 can be reduced by one, and the other can be increased by one (resulting the boxers with a weight of 33 and 55, respectively). Thus, you can get a team consisting of boxers with weights of 5,4,3,2,15,4,3,2,1.

题目大意:有三种情况,分别是数字加一,减一,或不变。问到最后有多少个不同的数字。

分析:扫一遍数组,开一个标记数组,若该点有值标记为true,ans++

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
#define MAXN 150000+5
#define INF 1<<30
#define mem(a,b) memset(a,b,sizeof(a))
#define sscc ios::sync_with_stdio(false) 
typedef unsigned long long ull;
 
int main(){
	sscc;
	int n;
	int a[MAXN];
	bool vis[MAXN];
	mem(a,0);
	mem(vis,false);
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> a[i];
	}
	sort(a+1,a+n+1);
	int ans=0;
	for(int i=1;i<=n;i++){
		int w=a[i];
		if(w-1>0&&vis[w-1]==0){
			ans++;
			vis[w-1]=1;
		}else if(vis[w]==0){
			ans++;
			vis[w]=1;
		}else if(vis[w+1]==0){
			ans++;
			vis[w+1]=1;
		}
	}
	cout << ans << endl;
	return 0;
}

F2. Complete the Projects (hard version) 【传送门】

The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version.

Polycarp is a very famous freelancer. His current rating is rr units.

Some very rich customers asked him to complete some projects for their companies. To complete the ii-th project, Polycarp needs to have at least aiai units of rating; after he completes this project, his rating will change by bibi (his rating will increase or decrease by bibi) (bibi can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer.

Polycarp can choose the order in which he completes projects. Furthermore, he can even skip some projects altogether.

To gain more experience (and money, of course) Polycarp wants to choose the subset of projects having maximum possible size and the order in which he will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project.

Your task is to calculate the maximum possible size of such subset of projects.

Input

The first line of the input contains two integers nn and rr (1≤n≤100,1≤r≤300001≤n≤100,1≤r≤30000) — the number of projects and the initial rating of Polycarp, respectively.

The next nn lines contain projects, one per line. The ii-th project is represented as a pair of integers aiai and bibi (1≤ai≤300001≤ai≤30000, −300≤bi≤300−300≤bi≤300) — the rating required to complete the ii-th project and the rating change after the project completion.

Output

Print one integer — the size of the maximum possible subset (possibly, empty) of projects Polycarp can choose.

Examples

input

Copy

3 4
4 6
10 -2
8 -1

output

Copy

3

input

Copy

5 20
45 -6
34 -15
10 34
1 27
40 -45

output

Copy

5

input

Copy

3 2
300 -300
1 299
1 123

output

Copy

3

分析:不会!!!!!!!!我还是太嫩了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值