Poj 1328 radar installation greedy

Source: Internet
Author: User
Tags radar
There are many islands in the sea. Now we need to build a radar on the coast, but the coverage radius of each radar is only D. In order to make every island covered, we need the least radar. (Create a coordinate system. The coast is the X axis, and the island is given in the form of coordinates.
Solution: First, find the coverage range of each island. That is, when the island is covered, the leftmost position and rightmost position that the radar can establish. Sort the leftmost position of each island in ascending order, and then solve it with greed.
# Include <cmath> # include <algorithm> # include <cstdlib> # include <iostream> using namespace STD; struct P {double left; double right;} POS [1001]; int CMP (const void * a, const void * B) {P * C, * D; C = (p *) A, D = (p *) B; return C-> left> D-> left? 1:-1;} int main () {int N, res, I, test = 0; double D, current, X, Y; while (CIN> N> D & N & D) {res = 0; for (I = 0; I <n; I ++) {CIN> x> Y; If (Y> d) RES =-1; else {pos [I]. left = x-SQRT (D * D-y * Y); POS [I]. right = x + SQRT (D * D-y * Y);} If (RES =-1) {cout <"case" <++ test <":-1" <Endl; continue;} qsort (Pos, N, sizeof (P), CMP ); current = POS [0]. right; Res = 1; for (I = 1; I <n; I ++) {/* When the leftmost position of island I is greater than the rightmost position of the previous Island, it indicates that the previous Island cannot share radar with the island behind it. Therefore, a radar */If (Pos [I] needs to be built for the previous Island. left> current) {res ++; current = POS [I]. right;} else {/* When island I is within the coverage of the previous Island, current should be changed to the right side of island I */If (Pos [I]. right <Current) Current = POS [I]. right ;}} cout <"case" <++ test <":" <res <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.