1105 - Coffee Central

Is it just a fad or is it here to stay? You're not sure, but the steadily increasing number of coffee shops that are opening in your hometown has certainly become quite a draw. Apparently, people have become so addicted to coffee that apartments that are close to many coffee shops will actually fetch higher rents.

This has come to the attention of a local real-estate company. They are interested in identifying the most valuable locations in the city in terms of their proximity to large numbers of coffee shops. They have given you a map of the city, marked with the locations of coffee shops. Assuming that the average person is willing to walk only a fixed number of blocks for their morning coffee, you have to find the location from which one can reach the largest number of coffee shops. As you are probably aware, your hometown is built on a square grid layout, with blocks aligned on north-south and east-west axes. Since you have to walk along streets, the distance between intersections (ab) and (cd ) is a - c| + | b - d|.

Input 

The input contains several test cases. Each test case describes a city. The first line of each test case contains four integers dxdyn, and q. These are the dimensions of the city grid dx x dy (1$ \le$dxdy$ \le$1000), the number of coffee shops n (0$ \le$n$ \le$5 . 105), and the number of queries q (1$ \le$q$ \le$20). Each of the next n lines contains two integers xi and yi (1$ \le$xi$ \le$dx, 1$ \le$yi$ \le$dy); these specify the location of the i-th coffee shop. There will be at most one coffee shop per intersection. Each of the next q lines contains a single integer m (0$ \le$m$ \le$106), the maximal distance that a person is willing to walk for a cup of coffee.

The last test case is followed by a line containing four zeros.

Output 

For each test case in the input, display its case number. Then display one line per query in the test case. Each line displays the maximum number of coffee shops reachable for the given query distance m followed by the optimal location. For example, the sample output shows that 3 coffee shops are within query distance 1 of the optimal location (3, 4), 4 shops are within query distance 2 of optimal location (2, 2), and 5 shops are within query distance 4 of optimal location (3, 1). If there are multiple optimal locations, pick the location that is furthest south (minimal positive integer y-coordinate). If there is still a tie, pick the location furthest west (minimal positive integer x-coordinate).

Follow the format of the sample output.

Sample Input 

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

Sample Output 

Case 1:
3 (3,4)
4 (2,2)
5 (3,1)





#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string>
using namespace std;
#define MP make_pair
const int MaxN=500009;
int dx,dy,N,Q,d;
int X[MaxN],Y[MaxN];
int Sum[2020][2020];
void Init()
{
	for(int i=1;i<=N;i++)
		cin>>X[i]>>Y[i];
}

void Inc(int _x1,int _y1,int _x2,int _y2)
{
	int x1=_x1+_y1+10;
	int y1=_y1-_x1+1010;
	int x2=_x2+_y2+10;
	int y2=_y2-_x2+1010;
	if(x1<10)
		x1=9;
	if(y1<10)
		y1=9;
	if(x2>2010)
		x2=2010;
	if(y2>2010)
		y2=2010;
	Sum[x2][y2]++;
	Sum[x1-1][y1-1]++;
	Sum[x1-1][y2]--;
	Sum[x2][y1-1]--;
}

int FindAns(int _x,int _y)
{
	int x=_x+_y+10;
	int y=_y-_x+1010;
	return Sum[x][y];
}

void Adjust()
{
	for(int i=2010;i>=0;i--)
		for(int j=2010;j>=0;j--)
			Sum[i][j]=Sum[i][j+1]+Sum[i+1][j]+Sum[i][j]-Sum[i+1][j+1];
	pair<int,pair<int,int>>Ans=MP(100,MP(-1,-1));
	for(int i=1;i<=dx;i++)
		for(int j=1;j<=dy;j++)
			if(MP(-FindAns(i,j),MP(j,i))<Ans)
				Ans=MP(-FindAns(i,j),MP(j,i));
	cout<<-Ans.first<<" ("<<Ans.second.second<<","<<Ans.second.first<<")\n";
}

void Solve()
{
	for(int i=1;i<=Q;i++)
	{
		cin>>d;
		for(int x=0;x<=2011;x++)
			for(int y=0;y<=2011;y++)
				Sum[x][y]=0;
		for(int j=1;j<=N;j++)
			Inc(X[j],Y[j]-d,X[j],Y[j]+d);
		Adjust();
	}
}


int main()
{
	int Case=0;
	while(cin>>dx>>dy>>N>>Q&&(dx||dy||N||Q))
	{
		Init();
		cout<<"Case "<<++Case<<":\n";
		Solve();
	}
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值