POJ 1328 Radar installation-placing radar (greedy, interval, two-dimensional turn one-dimensional)

Source: Internet
Author: User
Tags printf radar

http://poj.org/problem?id=1328

The question test instructions is that there are more than N islands on the sea, and a radar on the coastline (x-axis) can cover the island with its distance not exceeding D, and the minimum number of radars to cover all the islands.

.

.

.

.

.

.

.

.

.

The greedy idea is to convert a point to an interval on the x-axis coordinates (that is, a set of all possible locations that can guarantee coverage of the island), and then row in the order of points, or at the left end of the row. Then the leftmost side of the right traversal, if the left end of the next interval on the right side of the previous radar, obviously need to put a new radar; If you want to leave, then you need to judge the right end, if the right end is also on the last radar left, then this radar obviously can not cover the current small island, The radar position needs to be adjusted to the right end of the current interval, so that both the previous and the present can be covered, and if the right end is at the right end of the previous radar, no adjustment is required and no new radars need to be placed.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <
Algorithm> using namespace std;
struct Node {double Zuo, you;} fei[2100], t;
int CMP (Node A, Node B) {return A.zuo < B.zuo;} int x[2100],y[2100];
    int main () {int I, j, N, D, NUM, flag, s=0;
    Double Z, p;
        while (scanf ("%d%d", &n,&d)!=eof) {if (n==0&&d==0) break;
        s++;
        flag=0;
            for (i=0; i<n; i++) {scanf ("%d%d", &x[i],&y[i]);
        if (y[i]>d) flag=1;
        } if (flag) printf ("Case%d: -1\n", s);
                else {for (i=0; i<n; i++) {z=sqrt (d*d*1.0-y[i]*y[i]*1.0);
                fei[i].zuo= (double) x[i]-z;
            fei[i].you= (double) x[i]+z;
            } sort (fei,fei+n,cmp);
            num=0;
            p=-100000000;
      for (i=0; i<n; i++)      {if (fei[i].zuo>p) {num++;
                P=fei[i].you;
                } else if (fei[i].you<p) {p=fei[i].you;
        }} printf ("Case%d:%d\n", s,num);
}} 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.