zoj 3041 City Selection yy的题

本文介绍了一种解决城市选址问题的高效算法。该算法旨在避开工业污染源,特别是考虑到风向因素导致的污染物扩散方向。通过对工厂位置进行预处理,并采用数据结构优化查找过程,实现了快速筛选出不受污染的城市候选位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

City Selection

Time Limit: 5 Seconds      Memory Limit: 32768 KB

The government decided to build some cities in a newly developping area. Now they had N different locations to select from, while there were M factories in this newly developping area.

The area was blowing northwest wind, so if we choose a location that located on the southeast quadrant of one of the factory (including the boundary), the fog from the factory would pollute the city heavily.

Now it's your job to choose all the city locations that will not get pollution by the factories.

Input

The first line of a input block contains the numbers NM (1 <= NM <= 200000). Line 2 ~ N + 1 will each contain two integers(xy) the location of a city. Line N + 2 ~ M + N + 1 will each contains two integers(xy) the location of a factory. The x and y co-ordinates of the locations will be between -1000000000 and 1000000000, inclusive. There're no more than 10 test cases in the input data.

Output

The first line of your output block should be an integer K represent the number of city locations which the government can choose from. The next K lines should contain the co-ordinates of the cities. The co-ordinates should be sorted in ascending order of x co-ordinates, and in case of tie, ascending order of y co-ordinates.

Sample Input

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

Sample Output

1
1 3


题意:输入n,m,  再输入n个城市坐标,在输入m个工厂的坐标。  因为吹西北风的缘故,在工厂的 东南方向会受到污染,包括正南方向 和正东方向,还有自己本身的点都是会收到污染的。 然后判断有几个城市不收污染,输出坐标。

做法:预处理m个工厂,从左到右,不断把点的 y 更新为包括自己以及左边所有点的最大值。  这样直接搜索城市 city的x 坐标最近的工厂就可以了。那个工厂的y就是最高的限制。

#include <stdio.h>
#include <string> 
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
struct build
{
	int x,y;
};
int cmp(build a,build b)
{
	if(a.x!=b.x)
	return a.x<b.x;
	return a.y>b.y;//////////////////////
}
int cmp2(build a,build b)
{
	if(a.x!=b.x)
	return a.x<b.x;
	return a.y<b.y;//////////////////////
}
int max__(int a,int b)
{
	return a>b?a:b;
}
build city[200010],fact[200010];
map<int ,int>my;
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	//scanf("%d%d",&n,&m);
	{
		my.clear();
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&city[i].x,&city[i].y);
		}

		for(int i=0;i<m;i++)//gongchang
		{
			scanf("%d%d",&fact[i].x,&fact[i].y);
		}
		sort(fact,fact+m,cmp); 
		int maxx=fact[0].y;
		my[fact[0].x]=maxx;
		for(int i=1;i<m;i++)
		{
			if(fact[i].x!=fact[i-1].x)
				maxx=my[fact[i].x]=max__(fact[i].y,maxx);
		}
		
		int k=0;//cheng
		for(int i=0;i<n;i++)// keyongcheng
		{
			map<int,int>::iterator it=my.upper_bound(city[i].x);
			if(it==my.begin())
			{
				city[k].x=city[i].x;
				city[k++].y=city[i].y;
				continue;
			}
			it--;
			if((it->second)<city[i].y)
			{
				city[k].x=city[i].x;
				city[k++].y=city[i].y;
			}
		}

		printf("%d\n",k);
		sort(city,city+k,cmp2);
		for(int i=0;i<k;i++)
		{
			printf("%d %d\n",city[i].x,city[i].y);
		}

	}
	return 0;
}
 /*
 //up zhuyi begin
 city  fact
 3 3
0 1
-2 2
1 3
-2 2
2 0
4 4


3 1
1 2
2 2
2 1

1 1
*/






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值