CF-Lyft Level 5 Challenge 2018 - Final Round (Open Div. 2) A,B,C

A. The King's Race

题目链接:http://codeforces.com/contest/1075/problem/A

题目大意:一个棋盘,(1,1)(n,n)分别一个点,然后给出一个目标点的坐标,问谁先到(一次可以走八个方向)

水题,直接输出:

int main()
{
	std::ios::sync_with_stdio(false); 
	ll n;
	while(cin>>n)
	{
		ll x,y;
		cin>>x>>y;
		ll wi=x-1+y-1;
		ll bl=n-x+n-y;
		if(wi>bl)
			cout<<"Black"<<endl;
		else
			cout<<"White"<<endl;
	}
}

B. Taxi drivers and Lyft

题目链接:http://codeforces.com/contest/1075/problem/B

题目大意:n个乘客,m个司机,然后给出n+m个人的位置,然后给出n+m个人的身份(0是乘客,1是司机)

然后根据就近原则,输出每个司机能运多少个乘客;

分析:将它的身份和位置储存到一个结构体中,然后排序,遍历乘客和司机,然后按照位置分配

//#pragma comment(linker, "/STACK:1024000000,1024000000") 
 
#include<stdio.h>
#include<string.h>  
#include<math.h>  
#include<stdlib.h>

//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<fstream>  
#include<iostream>  
#include<algorithm>  
using namespace std;  
 
#define ll long long  
//#define max(a,b) (a)>(b)?(a):(b) 
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// ˮӡ 
//std::ios::sync_with_stdio(false); 
const int MAXN=2e5+5; 
const ll INF=1e18;  
const ll mod=998244353;  

struct node{
	int x,iscar;
	int ans;
}p[MAXN];

bool cmp(node a,node b)
{
	if(a.iscar==b.iscar)
		return a.x<b.x;
	return a.iscar>b.iscar;
}

int main()
{
	std::ios::sync_with_stdio(false); 
	int n,m;
	while(cin>>n>>m)
	{//n个乘客,m个司机
		clean(p,0);
		for(int i=1;i<=n+m;++i)
			cin>>p[i].x;
		for(int i=1;i<=n+m;++i)
			cin>>p[i].iscar;
		sort(p+1,p+n+m+1,cmp);
		int j=1;//从第一个司机开始
		for(int i=m+1;i<=n+m;++i)//遍历乘客
		{
			while(p[j+1].x<p[i].x&&p[j+1].x!=p[m+1].x)//找到最近的一个司机
				++j;
			if(p[j+1].x==p[m+1].x)//后面没有司机了
				p[j].ans++;
			else//司机中间有一个乘客
			{
				int upl=abs(p[i].x-p[j].x),downl=abs(p[i].x-p[j+1].x);
				if(upl<=downl)
					p[j].ans++;
				else
					p[++j].ans++;
			}
		}
		for(int i=1;i<=m;++i)
			cout<<p[i].ans<<" ";
		cout<<endl;
	}
}

C. The Tower is Going Home

题目链接:http://codeforces.com/contest/1075/problem/C

题目大意:给出一个1e9*1e9的棋盘,然后从(1,1)出发,到y=1e9的位置即可;

途中有一些障碍,横向的障碍是a~b 位于y=c处,纵向的障碍则是完全在x=a处有一个墙,

我们要尽量少的穿过墙壁到达y=1e9这一层,输出最小的穿过墙壁数量

分析,这个可以看做一个封闭的矩形,如果对于每个竖着的墙,我们找到与它构成的封闭的矩形,然后得出穿过的次数,遍历所有的竖墙,找出最少的封闭的矩形即可

//#pragma comment(linker, "/STACK:1024000000,1024000000") 
 
#include<stdio.h>
#include<string.h>  
#include<math.h>  
#include<stdlib.h>

#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<fstream>  
#include<iostream>  
#include<algorithm>  
using namespace std;  
 
#define ll long long  
//#define max(a,b) (a)>(b)?(a):(b) 
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// ˮӡ 
//std::ios::sync_with_stdio(false); 
const int MAXN=1e5+5; 
const ll INF=1e18;  
const ll mod=998244353;  

int wallx[MAXN],wally[MAXN];

bool cmp(int a,int b)
{
	return a<b;
}

void intt()
{
	clean(wallx,0);
	clean(wally,0);
}

int main()
{
	std::ios::sync_with_stdio(false); 
	int n,m;
	while(cin>>n>>m)
	{
		intt();
		for(int i=0;i<n;++i)//
			cin>>wallx[i];
		wallx[n++]=1e9;//边界位置存一个竖墙
		sort(wallx,wallx+n);//从小到大排序
		int k=0;
		for(int i=0;i<m;++i)//输入竖墙
		{
			int x1,x2,y;
			cin>>x1>>x2>>y;
			if(x1==1)//只有从1开始的才是封闭的
				wally[k++]=x2;
		}
		sort(wally,wally+k);//排序
		int ans=1e9;
		for(int i=0,j=0;i<n;++i)//遍历竖墙
		{
			for(;j<k&&wally[j]<wallx[i];++j);//不符合要求的横墙跳过
			ans=min(ans,k-j+i);//刷新ans
		}
		cout<<ans<<endl;
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/84137626
今日推荐