A. Circle of Students
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n students standing in a circle in some order. The index of the i-th student is pi. It is guaranteed that all indices of students are distinct integers from 1 to n (i. e. they form a permutation).
Students want to start a round dance. A clockwise round dance can be started if the student 2 comes right after the student 1 in clockwise order (there are no students between them), the student 3 comes right after the student 2 in clockwise order, and so on, and the student n comes right after the student n−1 in clockwise order. A counterclockwise round dance is almost the same thing — the only difference is that the student i should be right after the student i−1 in counterclockwise order (this condition should be met for every i from 2 to n).
For example, if the indices of students listed in clockwise order are [2,3,4,5,1], then they can start a clockwise round dance. If the students have indices [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 q independent queries.
Input
The first line of the input contains one integer q (1≤q≤200) — the number of queries. Then q queries follow.
The first line of the query contains one integer n (1≤n≤200) — the number of students.
The second line of the query contains a permutation of indices p1,p2,…,pn (1≤pi≤n), where pi is the index of the i-th student (in clockwise order). It is guaranteed that all pi are distinct integers from 1 to n (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
inputCopy
5
4
1 2 3 4
3
1 3 2
5
1 2 3 5 4
1
1
5
3 2 1 5 4
outputCopy
YES
YES
NO
YES
YES
#include
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
int a[300];
int main()
{
//ios::sync_with_stdio(false);
int tt;
scanf("%d",&tt);
while(tt--)
{
int n;
scanf("%d",&n);
int bz,las;
int ff=0 ,fff=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]!=i+1) ff=1;
if(a[i]!=n-i) fff=1;
if(a[i]==1) bz=i;
if(a[i]==n) las=i;
}
int f=1;
if(fabs(las-bz)==n-1)
{
if((ff==0||fff==0))
{
printf("YES\n");
continue;
}else
{
printf("NO\n");
continue;
}
}
if(fabs(las-bz)!=1) f=0;
if(f)
{
int fr=1;
if(las-bz==1)
{int bas=0;
for(int i=bz;;i--)
{
if(i==-1&&bas==0) bas=n;
if(a[i+bas]!=fr++)
{
f=0;
break;
}
if(fr==n+1) break;
}
}
if(f==1&&bz-las==1)
{ fr=1;
int bas=0;
for(int i=bz;;i++)
{
if(bas==0&&i==n)
{
bas=n;
}
if(a[i-bas]!=fr++)
{
f=0;
break;
}
if(fr==n+1) break;
}
}
}
if(f==0) printf("NO\n");
else printf("YES\n");
}
//system("PAUSE");
}
B. Equal Rectangles
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given 4n sticks, the length of the i-th stick is ai.
You have to create n rectangles, each rectangle will consist of exactly 4 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 a and b is a⋅b.
Your task is to say if it is possible to create exactly n rectangles of equal area or not.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1≤q≤500) — the number of queries. Then q queries follow.
The first line of the query contains one integer n (1≤n≤100) — the number of rectangles.
The second line of the query contains 4n integers a1,a2,…,a4n (1≤ai≤104), where ai is the length of the i-th stick.
Output
For each query print the answer to it. If it is impossible to create exactly n rectangles of equal area using given sticks, print "NO". Otherwise print "YES".
Example
inputCopy
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
outputCopy
YES
YES
NO
YES
YES
#include
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
int f[1000];
int main()
{
//ios::sync_with_stdio(false);
int tt;
scanf("%d",&tt);
while(tt--)
{
int n;
scanf("%d",&n);
map<int,int>mp;
int t=0;
for(int i=1;i<=4*n;i++)
{
int a;
scanf("%d",&a);
mp[a]++;
if(mp[a]%2==0)
{
f[t++]= a;
}
}
//printf("%d\n",t);
if(t<2*n)
{
printf("NO\n");
continue;
}
if(t==2&n==1)
{
printf("YES\n");
continue;
}
sort(f,f+t);
ll bz=f[0]*f[2*n-1];
//printf("%d %d",f[0],f[2*n-1]);
int ff=1;
for(int i=1,h=2*n-2;i<n;h--,i++)
{
if(f[i]*f[h]!=bz)
{
ff=0;
break;
}
}
if(ff==0)
{
printf("NO\n");
}else printf("YES\n");
}
//system("PAUSE");
}
C. Common Divisors
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a consisting of n integers.
Your task is to say the number of such positive integers x such that x 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 a will be [2,4,6,2,10], then 1 and 2 divide each number from the array (so the answer for this test is 2).
Input
The first line of the input contains one integer n (1≤n≤4⋅105) — the number of elements in a.
The second line of the input contains n integers a1,a2,…,an (1≤ai≤1012), where ai is the i-th element of a.
Output
Print one integer — the number of such positive integers x such that x 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
inputCopy
5
1 2 3 4 5
outputCopy
1
inputCopy
6
6 90 12 18 30 18
outputCopy
4
#include
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
ll k[400010];
ll deal(ll x)
{
ll res=2;
if(x==1) return 1;
if(x==2||x==3||x==5||x==7) return 2;
ll i;
for( i=2;i*i<=x;i++)
{
if(x%i==0) res+=2;
}
if((i-1)*(i-1)==x)
{
res--;
}
return res;
}
int main()
{
//ios::sync_with_stdio(false);
//cout<
//cout<
int n;
scanf("%d",&n);
ll ans;
int bz=0;
for(int i=0;i<n;i++)
{
scanf("%lld",&k[i]);
if(k[i]==1)
{
bz=1;
}
}
if(bz==1)
{
printf("1\n");
return 0;
}
ans=k[0];
for(int i=1;i<n;i++)
{
ans=__gcd(ans,k[i]);
if(ans==1) break;
}
//printf("%lld\n",ans);
//printf("%lld\n",deal(7));
printf("%lld\n",deal(ans));
//system("PAUSE");
}
D1. Remove the Substring (easy version)
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
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 s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s1s2…sl−1sr+1sr+2…s|s|−1s|s| (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 200 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 200 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
inputCopy
bbaba
bb
outputCopy
3
inputCopy
baaba
ab
outputCopy
2
inputCopy
abcde
abcde
outputCopy
0
inputCopy
asdfasdf
fasd
outputCopy
3
#include
using namespace std;
int main()
{
ios::sync_with_stdio(false);
string s,t;
cin>>s>>t;
int len1=s.length(),len2=t.length();
if(len1==len2)
{
printf("0\n");
return 0;
}
int ans=0;
for(int i=0;i<len1;i++)
{
for(int j=i;j<len1;j++)
{
int pos=0;
for(int k=0;k<len1;k++)
{
if(k>=i&&k<=j) k=j+1;
if(t[pos]==s[k]) pos++;
if(pos>=len2) break;
}
if(pos==len2)
{
ans=max(ans,j-i+1);
}
}
}
cout<<ans<<endl;
}
D2. Remove the Substring (hard version)
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The only difference between easy and hard versions is the length of the string.
You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).
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 s of maximum possible length such that after removing this substring t will remain a subsequence of s.
If you want to remove the substring s[l;r] then the string s will be transformed to s1s2…sl−1sr+1sr+2…s|s|−1s|s| (where |s| is the length of s).
Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Input
The first line of the input contains one string s consisting of at least 1 and at most 2⋅105 lowercase Latin letters.
The second line of the input contains one string t consisting of at least 1 and at most 2⋅105 lowercase Latin letters.
It is guaranteed that t is a subsequence of s.
Output
Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.
Examples
inputCopy
bbaba
bb
outputCopy
3
inputCopy
baaba
ab
outputCopy
2
inputCopy
abcde
abcde
outputCopy
0
inputCopy
asdfasdf
fasd
outputCopy
3
#include
using namespace std;
const int N=200010;
int l[N],r[N];
int main()
{
ios::sync_with_stdio(false);
string s,t;
cin>>s>>t;
int l1=s.length(),l2=t.length();
if(l1==l2)
{
printf("0\n");
return 0;
}
int pos=0;
for(int i=0;i<l1;i++)
{
if(s[i]==t[pos])
{
l[pos]=i;
pos++;
}
if(pos>=l2) break;
}
pos=l2-1;
for(int i=l1-1;i>=0;i--)
{
if(s[i]==t[pos])
{
r[pos]=i;
pos--;
}
if(pos<0) break;
}
int ans=max(l1-r[l2-1]-1,l[0]);
ans=max(l1-1-l[l2-1],ans);
ans=max(ans,r[0]);
for(int i=0;i<l1;i++)
{
ans=max(ans,r[i+1]-l[i]-1);
}
cout<<ans<<endl;
}
E. Boxers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n boxers, the weight of the i-th boxer is ai. Each of them can change the weight by no more than 1 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 ai will find the maximum possible number of boxers in a team.
It is possible that after some change the weight of some boxer is 150001 (but no more).
Input
The first line contains an integer n (1≤n≤150000) — the number of boxers. The next line contains n integers a1,a2,…,an, where ai (1≤ai≤150000) is the weight of the i-th boxer.
Output
Print a single integer — the maximum possible number of people in a team.
Examples
inputCopy
4
3 2 4 1
outputCopy
4
inputCopy
6
1 1 1 4 4 4
outputCopy
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 1 can be increased by one (get the weight of 2), one boxer with a weight of 4 can be reduced by one, and the other can be increased by one (resulting the boxers with a weight of 3 and 5, respectively). Thus, you can get a team consisting of boxers with weights of 5,4,3,2,1.
#include
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
int a[150010];
int main()
{
//ios::sync_with_stdio(false);
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
int t=0;
map<int,int>mp;
for(int i=0;i<n;i++)
{
if(a[i]>1&&mp[a[i]-1]==0)
{
t++;
mp[a[i]-1]=1;
}else if(mp[a[i]]==0)
{
t++;
mp[a[i]]=1;
}else if(mp[a[i]+1]==0)
{
t++;
mp[a[i]+1]=1;
}
}
printf("%d\n",t);
//system("PAUSE");
}
F1. Complete the Projects (easy version)
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 r units.
Some very rich customers asked him to complete some projects for their companies. To complete the i-th project, Polycarp needs to have at least ai units of rating; after he completes this project, his rating will change by bi (his rating will increase or decrease by bi) (bi can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer.
Is it possible to complete all the projects? Formally, write a program to check if such an order of the projects exists, that Polycarp has enough rating before starting each project, and he has non-negative rating after completing each project.
In other words, you have to check that there exists such an order of projects in which Polycarp will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project.
Input
The first line of the input contains two integers n and r (1≤n≤100,1≤r≤30000) — the number of projects and the initial rating of Polycarp, respectively.
The next n lines contain projects, one per line. The i-th project is represented as a pair of integers ai and bi (1≤ai≤30000, −300≤bi≤300) — the rating required to complete the i-th project and the rating change after the project completion.
Output
Print "YES" or "NO".
Examples
inputCopy
3 4
4 6
10 -2
8 -1
outputCopy
YES
inputCopy
3 5
4 -5
4 -2
1 3
outputCopy
YES
inputCopy
4 4
5 2
5 -3
2 1
4 -2
outputCopy
YES
inputCopy
3 10
10 0
10 -10
30 0
outputCopy
NO
Note
In the first example, the possible order is: 1,2,3.
In the second example, the possible order is: 2,3,1.
In the third example, the possible order is: 3,1,4,2.
#include
using namespace std;
struct node
{
int a,b;
int bz;
}v[110];
int vis[110];
int cmp(node x,node y)
{
if(x.a!=y.a )return x.a <y.a;
return x.b >y.b;
}
int cmp2(node x,node y)
{
return x.a +x.b>y.a+y.b;
}
int main()
{
int n,r;
scanf("%d%d",&n,&r);
for(int i=0;i<n;i++)
{
scanf("%d%d",&v[i].a,&v[i].b );
v[i].bz=0;
}
sort(v,v+n,cmp);
int now=r; int f=0;
for(int i=0;i<n;i++)
{
if(now>=v[i].a&&v[i].b>=0)
{
now+=v[i].b;
v[i].bz=1;
}else if(v[i].b>=0)
{
f=1;
break;
}
}
sort(v,v+n,cmp2);
for(int i=0;i<n&&f==0;i++)
{
if(v[i].bz ==0)
{
if(v[i].a>now )
{
f=1;
break;
}
if(now+v[i].b<0)
{
f=1;
break;
}
now+=v[i].b;
}
}
if(f)
{
printf("NO\n");
}else printf("YES\n");
}
F2. Complete the Projects (hard version)
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 r units.
Some very rich customers asked him to complete some projects for their companies. To complete the i-th project, Polycarp needs to have at least ai units of rating; after he completes this project, his rating will change by bi (his rating will increase or decrease by bi) (bi 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 n and r (1≤n≤100,1≤r≤30000) — the number of projects and the initial rating of Polycarp, respectively.
The next n lines contain projects, one per line. The i-th project is represented as a pair of integers ai and bi (1≤ai≤30000, −300≤bi≤300) — the rating required to complete the i-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
inputCopy
3 4
4 6
10 -2
8 -1
outputCopy
3
inputCopy
5 20
45 -6
34 -15
10 34
1 27
40 -45
outputCopy
5
inputCopy
3 2
300 -300
1 299
1 123
outputCopy
3
#include
using namespace std;
struct node
{
int a;
int b;
};
int cmp(node a,node b)
{
if(a.a!=b.a) return a.a<b.a;
return a.b>b.b;
}
int cmp2(node a,node b)
{
return a.a>b.a;
}
int main()
{
int n,r;
scanf("%d%d",&n,&r);
vector<node> pos,neg;
for(int i=0;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
if(b>=0)
{
pos.push_back(node{a,b});
}else
{
neg.push_back(node{a,b});
}
}
sort(pos.begin(),pos.end(),cmp);
int ans=0;int now=r;
for(int i=0;i<pos.size();i++)
{
if(now>=pos[i].a)
{
ans++;
now+=pos[i].b;
}else break;
}
priority_queue<int,vector<int>,greater<int> > que;
for(int i=0;i<neg.size();i++)
{
neg[i].a+=neg[i].b;
neg[i].a=max(neg[i].a,0);
}
sort(neg.begin(),neg.end(),cmp2);
for(int i=0;i<neg.size();i++)
{
now+=neg[i].b;
que.push(neg[i].b);
if(now<neg[i].a)
{
now-=que.top();
que.pop();
}
}
ans+=que.size();
printf("%d\n",ans);
}
第一次补完所有题,happy~~~