1. 程式人生 > >CF-Lyft Level 5 Challenge 2018 - Final Round (Open Div. 2) A,B,C

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;
	}
	
}