关闭

Educational Codeforces Round 27 A—D 题解

标签: acmcodeforces
584人阅读 评论(0) 收藏 举报
分类:

这里是传送门

A. Chess Tourney
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 winsregardless of the results of the drawing?

Input

The first line contains one integer n (1 ≤ n ≤ 100).

The second line contains 2·n integers a1, a2, ... a2n (1 ≤ ai ≤ 1000).

Output

If it's 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, then print "YES". Otherwise print "NO".

Examples
input
2
1 3 2 4
output
YES
input
1
3 3
output
NO
给定一些等级,问有没有必胜的可能。

存一个数组里升序排序,前一个人的最后一项小于后面人的最小的项就有必胜的可能。


代码实现:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))
#pragma comment(linker,"/STACK:1024000000,1024000000")

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e5+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};

int main()
{
	int a[1005],b[1005],i,j,k,n;
	while(cin>>n)
	{
		for(i=0;i<2*n;i++)
		{
			cin>>a[i];
		}
		sort(a,a+2*n);
		if(a[n-1]!=a[n])
		cout<<"YES"<<endl;
		else
		cout<<"NO"<<endl;
	}
	return 0;
}


B. Luba And The Ticket
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

You are given a string consisting of 6 characters (all characters are digits from 0 to 9) — this string denotes Luba's ticket. The ticket can start with the digit 0.

Output

Print one number — the minimum possible number of digits Luba needs to replace to make the ticket lucky.

Examples
input
000000
output
0
input
123456
output
2
input
111000
output
1
Note

In the first example the ticket is already lucky, so the answer is 0.

In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required.

In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.


给定你6个数,问最少需要变几个数才能满足前三个数等于后三个数。

模拟,存两个数组排好序。总共4种情况,逐一判断即可。


代码实现:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))
#pragma comment(linker,"/STACK:1024000000,1024000000")

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e5+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};

char map[10];
int a[5],b[5];

int main()
{
	int i,j,k,ans1,ans2;
	while(~scanf("%s",map))
	{
		ans1=ans2=0;
		for(i=0;i<3;i++)
		{
			ans1+=map[i]-'0';
			a[i]=map[i]-'0';
		}
		for(i=3;i<6;i++)
		{
			ans2+=map[i]-'0';
			b[i-3]=map[i]-'0';
		}
		sort(a,a+3);
		sort(b,b+3);
		
		if(ans1>ans2)
		{
			for(i=0;i<3;i++)
			swap(a[i],b[i]); 
		} 
		int c=fabs(ans2-ans1);
		if(c==0)
		cout<<"0"<<endl;
		else
		{
			if(c<=9-a[0]||c<=b[2])
			cout<<"1"<<endl;
			else if(c<=18-a[0]-a[1]||c<=b[2]+b[1]||c<=9-a[0]+b[2])
			cout<<"2"<<endl;
			else
			cout<<"3"<<endl;
		}
	}
	return 0;
}


C. Two TVs
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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?

Input

The first line contains one integer n (1 ≤ n ≤ 2·105) — the number of shows.

Each of the next n lines contains two integers li and ri (0 ≤ li < ri ≤ 109) — starting and ending time of i-th show.

Output

If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).

Examples
input
3
1 2
2 3
4 5
output
YES
input
4
1 2
2 3
2 3
1 2
output
NO
有一堆节目清单,有两台电视机,若前一个节目结束时间跟后一个节目的开始时间相同的话,不能使用同一台电视机播放,问最后能否看完全部节目。

还是模拟,先将节目时间排好序,两个电视依次判断即可。


代码实现:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))
#pragma comment(linker,"/STACK:1024000000,1024000000")

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=2e5+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
struct node{
	int l,r;
}p[maxn];

int cmp(node a,node b)
{
	if(a.l==b.l)
	return a.r<b.r;
	
	return a.l<b.l; 
}

int main()
{
	int n,i,j,k,flag;
	while(cin>>n)
	{
		for(i=0;i<n;i++)
		{
			cin>>p[i].l>>p[i].r;
		}
		sort(p,p+n,cmp);
		flag=1;
		int temp1=p[0].r,temp2=-1;
		for(i=1;i<n;i++)
		{
			if(p[i].l<=temp1)
			{
				if(p[i].l<=temp2)
				{
					flag=0;
					break;
				}
				else
				temp2=p[i].r;
			}
			else
			temp1=p[i].r;
		}
		if(flag)
		cout<<"YES"<<endl;
		else
		cout<<"NO"<<endl;
	}
	return 0;
}


D. Driving Test
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  1. Polycarp changes the speed of his car to specified (this event comes with a positive integer number);
  2. Polycarp's car overtakes the other car;
  3. Polycarp's car goes past the "speed limit" sign (this sign comes with a positive integer);
  4. Polycarp's car goes past the "overtake is allowed" sign;
  5. Polycarp's car goes past the "no speed limit";
  6. 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?

Input

The first line contains one integer number n (1 ≤ n ≤ 2·105) — number of events.

Each of the next n lines starts with integer t (1 ≤ t ≤ 6) — the type of the event.

An integer s (1 ≤ s ≤ 300) follows in the query of the first and the third type (if it is the query of first type, then it's new speed of Polycarp's car, if it is the query of third type, then it's new speed limit).

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

Output

Print the minimal number of road signs Polycarp should say he didn't notice, so that he would make no rule violations from his point of view.

Examples
input
11
1 100
3 70
4
2
3 120
5
3 120
6
1 150
4
3 300
output
2
input
5
1 100
3 200
2
4
5
output
0
input
7
1 20
2
6
4
6
6
2
output
2
Note

In the first example Polycarp should say he didn't notice the "speed limit" sign with the limit of 70 and the second "speed limit" sign with the limit of 120.

In the second example Polycarp didn't make any rule violation.

In the third example Polycarp should say he didn't notice both "no overtake allowed" that came after "overtake is allowed" sign.

给出n次操作,你可以忽略 可以超车,不可以超车,速度最大限制以及无速度限制这些操作,问使得超车以及改变速度合法的前提下最少需要忽略多少次操作,每次超车可以覆盖前面所有的超车和不超车,速度最大限制和无速度限制一样。

又是模拟,按题目模拟即可。

代码实现:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))
#pragma comment(linker,"/STACK:1024000000,1024000000")

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int map[maxn];

int main()
{
	int n,ans,top,v,no,i,j,k,x,y,z;
	while(cin>>n)
	{
		ans=top=no=0;
		mset(map,0);
		map[0]=inf;
		for(i=1;i<=n;i++)
		{
			cin>>x;
			if(x==1)
			cin>>v;
			if(x==2)
			{
				ans+=no;
				no=0;
			}
			if(x==3)
			{
				cin>>y;
				map[++top]=y;
			}
			if(x==4)
			no=0;
			if(x==5)
			{
				map[++top]=inf;
			}
			if(x==6)
			no++;
			while(map[top]<v)
			{
				ans++;
				top--;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:34170次
    • 积分:1582
    • 等级:
    • 排名:千里之外
    • 原创:127篇
    • 转载:1篇
    • 译文:0篇
    • 评论:13条
    联系方式
    欢迎谈论交流:1245985209
    博客专栏
    最新评论