1
Berland annual chess tournament is coming!
Organizers have gathered 2·n chess players who should be divided into two teams with n people each. The first team is sponsored by BerOil and the second team is sponsored by BerMobile. Obviously, organizers should guarantee the win for the team of BerOil.
Thus, organizers should divide all 2·n players into two teams with n people each in such a way that the first team always wins.
Every chess player has its rating ri. It is known that chess player with the greater rating always wins the player with the lower rating. If their ratings are equal then any of the players can win.
After teams assignment there will come a drawing to form n pairs of opponents: in each pair there is a player from the first team and a player from the second team. Every chess player should be in exactly one pair. Every pair plays once. The drawing is totally random.
Is it possible to divide all 2·n players into two teams with n people each so that the player from the first team in every pair wins regardless of the results of the drawing?
分数列,使一个中每一个数比另外一个中任意一数大,输出可否.
#include<bits/stdc++.h>
using namespace std;
int main()
{int m,a[210],i;
cin>>m;
for(i=1;i<=2*m;i++)
scanf("%d",&a[i]);
sort(a+1,a+2*m+1);
if(a[m]==a[m+1])
{cout<<"NO";return 0;
}
else cout<<"YES";
}
2
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky.
The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits.
换数,使前三个的和等于后三个,求最小次数.
#include<bits/stdc++.h>
using namespace std;
int main() {
int m,a[7],b[7],i,sum1=0,sum2=0,sum;
string s;
cin>>s;
for(i=0; i<=5; i++) {
if(i<=2)a[i+1]=s[i]-'0',sum1+=a[i+1];
else b[i-2]=s[i]-'0',sum2+=b[i-2];
}
sort(a+1,a+4);
sort(b+1,b+4);
if(sum1>sum2) {
sum=sum1-sum2;
int cnt=0;
for(i=4; i<=6; i++)
a[i]=9-b[i-3];
sort(a+1,a+7);
for(i=6; i>=1; i--) {
sum-=a[i];
cnt++;
if(sum<=0) {
cout<<cnt;
return 0;
}
}
} else if(sum1==sum2) {
cout<<0;
return 0;
} else if(sum1<sum2) {
sum=sum2-sum1;
int cnt=0;
for(i=4; i<=6; i++)
b[i]=9-a[i-3];
sort(b+1,b+7);
for(i=6; i>=1; i--) {
sum-=b[i];
cnt++;
if(sum<=0) {
cout<<cnt;
return 0;
}
}
}
}
3
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment li and ends at moment ri.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can’t watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
两台电视看节目,问能否都看到.方法:出现三个播出时间有交集就不行.
#include<bits/stdc++.h>
#define N 200010
using namespace std;
struct gg{
long long int x;long long int y;
}a[N];
bool cmp(gg a,gg b)
{return a.x<b.x;
}
int main()
{long long int m,i,ed1=-999,ed2=-999,aa,bb;
cin>>m;
for(i=1;i<=m;i++)
scanf("%d%d",&a[i].x,&a[i].y);
sort(a+1,a+m+1,cmp);
for(i=1;i<=m;i++)
{if(i==1)ed1=a[i].y;
else
if(a[i].x<=ed1)
{if(a[i].x>ed2)ed2=a[i].y;
else {cout<<"NO";return 0;}
}
else if(a[i].x>ed1)
{if(a[i].x<=ed2)
{ed1=a[i].y;
}
else
{if(ed1<=ed2)
ed1=a[i].y;
else
ed2=a[i].y;
}
}
}
cout<<"YES";
}
4
Polycarp has just attempted to pass the driving test. He ran over the straight road with the signs of four types.
speed limit: this sign comes with a positive integer number — maximal speed of the car after the sign (cancel the action of the previous sign of this type);
overtake is allowed: this sign means that after some car meets it, it can overtake any other car;
no speed limit: this sign cancels speed limit if any (car can move with arbitrary speed after this sign);
no overtake allowed: some car can’t overtake any other car after this sign.
Polycarp goes past the signs consequentially, each new sign cancels the action of all the previous signs of it’s kind (speed limit/overtake). It is possible that two or more “no overtake allowed” signs go one after another with zero “overtake is allowed” signs between them. It works with “no speed limit” and “overtake is allowed” signs as well.
In the beginning of the ride overtake is allowed and there is no speed limit.
You are given the sequence of events in chronological order — events which happened to Polycarp during the ride. There are events of following types:
Polycarp changes the speed of his car to specified (this event comes with a positive integer number);
Polycarp’s car overtakes the other car;
Polycarp’s car goes past the “speed limit” sign (this sign comes with a positive integer);
Polycarp’s car goes past the “overtake is allowed” sign;
Polycarp’s car goes past the “no speed limit”;
Polycarp’s car goes past the “no overtake allowed”;
It is guaranteed that the first event in chronological order is the event of type 1 (Polycarp changed the speed of his car to specified).
After the exam Polycarp can justify his rule violations by telling the driving instructor that he just didn’t notice some of the signs. What is the minimal number of signs Polycarp should say he didn’t notice, so that he would make no rule violations from his point of view?
此题较烦.一个车行驶,它超车或改变速度有提示,遇到每种路标有提示能否超车,或限速xx,或不限速度,输出这辆车开车司机最少没注意到的路标数(他在途中各种违规)
分类搞搞就行,但要注意连续出现限速牌或禁止超车的情况,司机可能好几个没看到,因此限速牌的情况还要用个栈.
#include<bits/stdc++.h>
#define N 200010
using namespace std;
int main() {
int n,i,tp[N],speed=0,maxspd=999999,ret=0,cnt=0;
stack <int> sp;
bool flag=0;//flag chaoche flag1 huansu
//freopen("in.txt","r",stdin);
cin>>n;
for(i=1; i<=n; i++) {
scanf("%d",&tp[i]);
if(tp[i]==1) {
scanf("%d",&speed);
while(sp.empty()==0&&speed>sp.top())
{ret++;sp.pop();
}
}
else if(tp[i]==2) {
ret+=cnt;
cnt=0;
} else if(tp[i]==3) {
scanf("%d",&maxspd);
if(maxspd<speed) {
ret++;//cout<<ret;
} else {
sp.push(maxspd);
}
} else if(tp[i]==4) {
cnt=0;
} else if(tp[i]==5)
{maxspd=999999;
while(sp.empty()==0)
sp.pop();
}
else if(tp[i]==6) {
cnt++;
}
}
cout<<ret;
}