Poj 1328: radar installation

Source: Internet
Author: User
Tags cmath radar

Radar installation
Time limit:1000 ms   Memory limit:10000 K
Total submissions:50843   Accepted:11415

Description

Assume the coasting is an infinite straight line. land is in one side of coasting, sea in the other. each small island is a point locating in the sea side. and any radar installation, locating on the coasting, can only cover D distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most D.

We use Cartesian coordinate system, defining the coasting is the x-axis. the sea side is abve X-axis, and the land side below. given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. note that the position of an island is represented by its X-Y coordinates.

Figure A sample input of radar installations

Input

The input consists of several test cases. the first line of each case contains two integers n (1 <= n <= 1000) and D, where N is the number of islands in the sea and D is the distance of coverage of the radar installation. this is followed by n lines each containing two integers representing the coordinate of the position of each island. then a blank line follows to separate the cases.

The input is terminated by a line containing pair of zeros

Output

For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" Installation means no solution for that case.

Sample Input

3 21 2-3 12 11 20 20 0

Sample output

Case 1: 2Case 2: 1

Idea: This question is intended to find the minimum number of radars that can cover all islands. Each small island corresponds to a range on the X axis, and radar is placed at any point in the range, then it can cover the small island. The range calculation uses [X-SQRT (D * D-y * Y), X + SQRT (D * D-y * y)]; in this way, the problem is converted to a known number of intervals, and the minimum number of vertices is obtained so that each interval has at least one vertex. For each iteration, select the rightmost vertex for the first interval, because it can satisfy many intervals. If you do not select the rightmost vertex for the first interval (select the previous vertex ), after changing it to the rightmost vertex, the previously satisfied interval is still satisfied, so the rightmost vertex of the first interval is greedy, delete the expected range and perform the next iteration until the end of the iteration.

Take the left interval as the standard:

# Include <cstdio> # include <cstring> # include <algorithm> # include <iostream> # include <cmath> using namespace STD; const int M = 1000 + 5; Double X, y; double DIS; // the radius of the circle double temp; int ans; // used to calculate the int N; // The number of points int flag; // judge struct node {double left; // left interval double right; // right interval} island [m]; bool CMP (node A, Node B) // sort by the Left interval from small to large {return. left <B. left;} int main () {int CAS = 0; while (scanf ("% d % lf", & N, & dis) & N & dis) {flag = 0; ans = 0; CAS ++; For (INT I = 0; I <n; I ++) {scanf ("% lf ", & X, & Y); If (Y> dis | Y <0) // nonconforming vertex flag = 1; Island [I]. right = x + SQRT (Dis * Dis-y * Y); Island [I]. left = x-SQRT (Dis * Dis-y * Y);} If (flag = 1) printf ("case % d:-1 \ n", CAS ); else {sort (Island, island + N, CMP); // sort temp = island [0]. right; ans = 1; for (INT I = 1; I <n; I ++) {If (Island [I]. right <temp) // indicates that this interval is included in the previous interval. As long as the number of vertices meet the requirements between cells, the large interval must also meet the requirements of temp = island [I]. right; else if (Island [I]. left> temp) // indicates that the two intervals do not overlap and cannot be represented by a circle. Then, the result is incremented by {ans ++; temp = island [I]. right ;}} printf ("case % d: % d \ n", Cas, ANS) ;}} return 0 ;}

The first right interval prevails: (this is the right interval written by my classmates)


#include<iostream>#include<cstring>#include<cmath>#include<cstdio>#include<algorithm>using namespace std;struct node{double a,b;}s[1005],l[1005];int cmp(node x,node y){return x.b<y.b;}int main(){int n,m,k=0;while(cin>>n>>m){if(n==0&&m==0)   break;int i,j,p=0;for(i=0;i<n;i++){scanf("%lf%lf",&s[i].a,&s[i].b);if(s[i].b<0||s[i].b>m)p=1;l[i].a=s[i].a-sqrt(m*m-s[i].b*s[i].b);l[i].b=s[i].a+sqrt(m*m-s[i].b*s[i].b);}sort(l,l+n,cmp);int sum=1; double r=l[0].b;for(i=1;i<n;i++){if(l[i].a>r){r=l[i].b;sum++;}}printf("Case %d: ",++k);if(p)cout<<-1<<endl;elsecout<<sum<<endl;}return 0;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.