ZOJ 3041 City Selection(好排序)

題目鏈接:ZOJ 3041 City Selection

題意:有N個城市座標和M個工廠座標,在以工廠爲原點的第四象限的點都會受到污染。即圖中畫線區域


思路:把工廠和城市的座標一起排序,再比較y座標。


AC代碼:


#include <stdio.h>
#include <algorithm>
#include <set>
using namespace std;
const int maxn=200010;

struct node{
	int x,y;
	int flag;
};
struct node p[maxn*2],tmp,ans[maxn];

bool cmp1(node a,node b){
	if(a.x!=b.x)
		return a.x<b.x;
	if(a.y!=b.y)
		return a.y>b.y;
	return a.flag<b.flag;
}

bool cmp2(node a,node b){
	if(a.x!=b.x)
		return a.x<b.x;
	if(a.y!=b.y)
		return a.y<b.y;
}

int main()
{
	int n,m,i,j,count;
	while(scanf("%d%d",&n,&m)!=EOF){
		int cnt=0;
		for(i=0;i<n;i++){
			scanf("%d%d",&tmp.x,&tmp.y);
			tmp.flag=1;
			p[cnt++]=tmp;
		}
		for(i=0;i<m;i++){
			scanf("%d%d",&tmp.x,&tmp.y);
			tmp.flag=0;
			p[cnt++]=tmp;
		}
		sort(p,p+cnt,cmp1);
		tmp.x=-1000000010;
		tmp.y=-1000000010;
		count=0;
		for(i=0;i<cnt;i++){
			if(p[i].flag==0 && p[i].y>=tmp.y)
				tmp.y=p[i].y;
			if(p[i].flag==1 && p[i].y>tmp.y)
				ans[count++]=p[i];
		}
		sort(ans,ans+count,cmp2);
		printf("%d\n",count);
		for(i=0;i<count;i++){
			printf("%d %d\n",ans[i].x,ans[i].y);
		}
	}
	return 0;
}
/*
7 3
-3 3
0 1
-2 2
1 3
4 2
3 4
5 5

-2 2
2 0
4 4

5 3
0 1
-2 2
1 4
1 3
4 4

-2 2
2 0
4 3

1 1
0 0
-1 0
*/



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章