程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Uva 12537 Radiation

Uva 12537 Radiation

編輯:C++入門知識

Nuclear power plants (NPP) are a blessing and curse of modern civilization. NPPs have some risks but still it is one of the cheapest ways to produce electricity in the developed world. In this problem we will discuss a situation related to two nuclear plants, which are not far away from each other.                Figure 1: Two Nuclear Power Plants. Houses at (81, 49) and (77,33) are at high risk from both the plants.       We will describe the entire scenario in a flat land, so two-dimensional Cartesian coordinate system is used to denote each location. Letsassume that the coordinate of the two nuclear power plants are (ax, ay) and (bx, by). Houses that are located within distance R1 (inclusive) of the power plant at (ax, ay) are under high risk of radiation. Similarly, houses that are located within distance R2 (inclusive) of the power plant at (bx, by) are under high risk of radiation. So the authorities of power plant 1 and power plant 2 distribute special protective equipments to the houses that are within radius (inclusive) R1 and R2 of the respective power plants. As a result each of the houses that are endangered by both the plants actually receive two sets of equipments to protect their house, however only one set is enough for full protection. Houses that are outside the high-risk area are under low risk of radiation but they do not receive any protective equipment due to budget constraints. However, each owner of the houses that have two sets of protective equipments gives away one set of equipment to the owner of a house that has none. Still, some houses in the low-risk area remain un-protected. Given the location of the houses and the values of  ax, ay, bx, by and possible values of R1 and R2 your job is to find out the number of houses that are without protective equipments for each pair of values of R1 and R2.       Input   The input file contains at most 3 test cases. The description of each test case is given below:       A test case starts with a line containing a positive integer N (0 < N ≤ 200000) that denotes the number of houses that are under either low risk or high risk of radiation. Each of the next N lines contains two integers xi, yi (0 ≤ xi, yi ≤ 20000) that denotes the coordinate of the i-thhouse. No two houses are at the same location. The next line contains five integers ax, ay, bx, by and q (0 ≤ ax, ay, bx, by ≤ 20000, 0 <q ≤ 20000). The meaning of ax, ay, bx and by are given in the problem statement. Here q denotes the total number of query. Each of the next q lines contains two integers, which denote the values of R1 and R2 (0 < R1, R2 ≤ 13000) respectively.       A line containing a single zero terminates input. This line should not be processed.       Output For each test case produce q+1 lines of output.  The first line is the serial of output. For each query (given value of R1 and R2) determine how many houses in the low risk region remains without protective equipment. You may consider using faster IO as judge input file is large.       Sample Input                      |              Output for Sample Input 11   95 75   27 6   93 5   124 13   34 49   65 61   81 49   77 33   110 50   91 22   110 25   57 42 97 36 2   31 25   25 25   0   Case 1:   2   2       Note: First query in the sample input corresponds to Figure 1.      題意:   n   給定n個點的坐標   圓1的坐標 圓2的坐標 詢問次數       圓1的半徑 圓2的半徑   問:對於每個詢問,求出(不在圓上的點 - 在2圓重合 部分的點 )  //注意當答案<0 輸出0       思路:首先對題意轉化,可以看成是求 n - (在圓1上的點)-(在圓2上的點)   因為所有點是固定的,所以 (在圓1的點) =>   DIS( 點,圓心1) <= R1   只要求出所有滿足上述不等式點的個數即可       把所有點按 (到圓心1的距離)小到大排序,存在p1數組中,再把p1中有相同dis的點去重後存在k1數組中,k1.num 就表示 距離<=k1.dis的點有 k1.num個   然後二分找到k1.dis <= R1 的最大的k1.num ,就是(在圓1上的點)       對圓2上的點相同操作

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cctype>
#include <queue>
#include <stdlib.h>
#include <cstdlib>
#include <math.h>
#include <set>
#include <vector>
#define inf 2000000
#define N 200200
using namespace std;

struct node{
	int x,y;
	int dis;
	bool operator<(const node& a)const {return a.dis>dis;}
}p1[N],p2[N],r1,r2;
struct kk{
	int dis,num;
}k1[N],k2[N];

int kn1,kn2;
int R1,R2,sum1,sum2,n;

int DIS(node a,node b){return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}
int erfen1(int l,int r,int d){
	if(l==r-1 && k1[l].dis<=d && k1[r].dis>d )
		return k1[l].num;
	int mid=(l+r)>>1;
	if(k1[mid].dis>d)return erfen1(l,mid,d);
	if(k1[mid].dis<d)return erfen1(mid,r,d);
	if(k1[mid].dis==d)return k1[mid].num;

}
int erfen2(int l,int r,int d){
	if(l==r-1 && k2[l].dis<=d && k2[r].dis>d )
		return k2[l].num;
	int mid=(l+r)>>1;
	if(k2[mid].dis>d)return erfen2(l,mid,d);
	if(k2[mid].dis<d)return erfen2(mid,r,d);
	if(k2[mid].dis==d)return k2[mid].num;

}

void quchong(){//p數組去重得到k數組
	int i;
	for(i=1;i<=n;i++)
	{
		if(p1[i].dis==p1[i-1].dis)k1[kn1].num++;
		else
		{
			kn1++;
			k1[kn1].dis=p1[i].dis;
			k1[kn1].num=1+k1[kn1-1].num;
		}

		if(p2[i].dis==p2[i-1].dis)k2[kn2].num++;
		else
		{
			kn2++;
			k2[kn2].dis=p2[i].dis;
			k2[kn2].num=1+k2[kn2-1].num;

		}
	}
}
int main(){
	int i,j,query,Cas=1;
	p1[0].dis=p2[0].dis=-1; //去重邊界
	while(scanf("%d",&n),n){
		for(i=1;i<=n;i++)scanf("%d%d",&p1[i].x,&p1[i].y),p2[i]=p1[i];

		scanf("%d %d %d %d %d",&r1.x,&r1.y,&r2.x,&r2.y,&query);
		for(i=1;i<=n;i++)
			p1[i].dis=DIS(p1[i],r1),	p2[i].dis=DIS(p2[i],r2);

		sort(p1+1,p1+n+1);
		sort(p2+1,p2+n+1);

		kn1=kn2=0;
		quchong();

		k1[0].dis=k2[0].dis=-1; //二分需要的邊界條件
		k1[0].num=k2[0].num=0;
		k1[kn1+1].dis=k2[kn2+1].dis=inf; //二分需要的邊界條件

		printf("Case %d:\n",Cas++);
		while(query--)
		{
			scanf("%d %d",&R1,&R2);
			sum1=erfen1(1,kn1,R1*R1);
			sum2=erfen2(1,kn2,R2*R2);
			int ans=n-sum1-sum2;
			if(ans<0)ans=0;
			printf("%d\n",ans);
		}

	}

	return 0;
}
/*


11
95 75
27 6
93 5
124 13
34 49
65 61
81 49
77 33
110 50
91 22
110 25
57 42 97 36 2
31 25
25 25



15
1 1
2 2
3 3 
4 4
5 5
10 5
15 5
1 0
2 0
3 0
4 0
5 0
6 0
10 0
20000 20000

10 0 5 5 2
7 1
8 1




ans:
2
2

8 
3
*/

 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved