POJ 1328 Radar Installation

Source: Internet
Author: User
Tags printf radar
POJ 1328 Radar installation (greedy)
Time limit: 1000MS Memory Limit: 10000K
Total Submissions: 101806 Accepted: 22654

Description

Assume the coasting is a infinite straight line. Side of coasting, sea in the other. Each of small island was a point locating in the sea side. and any radar installation, locating on the coasting, can only cover D-distance, so a island in the sea can is 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 above x-axis, and the land side below. Given the position of 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 a 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 all case contains-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 was followed by n lines each containing and 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 all test case output one line consisting of the test case number followed by the minimal number of radar installation S needed. "-1" installation means no solution for this case.

Sample Input

3 2
1 2
-3 1 2
1

1 2
0 2

0 0

Sample Output

Case 1:2 Case
2:1
Test Instructions

Take the x axis as the demarcation, the y>0 part is the sea, the y<0 part is the land, gives some island coordinates (in the sea), gives the radar to reach the range, the radar can only be on the land, asks the least how many radar can cover so the island. Thinking of solving problems

First we can judge, because the Circle center is on the x-axis, if a bit of ordinate is greater than the radius d, then directly output "1". Look at this problem, obviously there is no direct solution to the method, we need to convert a bit. We can find each point on the x-axis can be circled area, and then can be directly sorted, sorting is also fastidious, according to the right end of the ascending order. Similar to the activity scheduling problem, each maintenance of a right boundary, each interval is equivalent to a full circle of the range, so you can directly greedy. Code

#include <stdio.h> #include <iostream> #include <algorithm> #include <math.h> #include <
Stdlib.h> using namespace std;

const int MAXN = 1005;
struct Node {double x, y;} ARR[MAXN];

int n,d;
    BOOL CMP (node A,node b) {if (A.Y!=B.Y) return a.y<b.y;
Return a.x>b.x;
    } void Solve (node A,int i) {double dis=sqrt (D*D-A.Y*A.Y);
    Arr[i].x=a.x-dis;
Arr[i].y=a.x+dis;
    } int main () {//Freopen ("In.txt", "R", stdin);
    int k=1;
        while (cin>>n>>d) {if (n==0&&d==0) break;
        int flag=0;
        printf ("Case%d:", k++);
            for (int i=0; i<n; i++) {scanf ("%lf%lf", &arr[i].x,&arr[i].y);
        if (arr[i].y>d) flag=1;
            } if (flag) {puts ("-1");
        Continue
        } for (int i=0; i<n; i++) Solve (arr[i],i);
        Sort (arr,arr+n,cmp);
        Double tmp=-0x3f3f3f3f;
        int ans=0; for (int i=0; i<n;
                i++) {if (tmp<arr[i].x) {tmp=arr[i].y;
            ans++;
    }} printf ("%d\n", ans);
} 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.