Poj 1328 greedy radar installation

Source: Internet
Author: User
Tags radar
// Inspection site: greedy Algorithm There are N closed intervals on the digital axis [ai; bi]. Take as few points as possible, so that each interval has at least one point (the points contained in different ranges can be the same) // submission: Wrong answer three times, cause: define TMP as int // harvest: The key is how to sort by the right endpoint from small to large. If the right endpoint is equal, sort by the Left endpoint from large to small. this question can only be sorted by right endpoint. // AC code # include <stdio. h> # include <stdlib. h> # include <algorithm> # include <math. h> using namespace STD; int N, D; struct line {double left, right ;}; line ln [1005]; int CMP (line A, line B) {if (. right <B. right) return 1; // else if (. right = B. right) {// return. left> B. left; //} return 0;} int main () {INT cases = 0; while (scanf ("% d", & N, & D )) {If (n = 0 & D = 0) break; cases ++; int I, X, Y; bool flag = false; for (I = 0; I <n; I ++) {scanf ("% d" , & X, & Y); If (Y> d) Flag = true; Ln [I]. left = x-SQRT (D * D-y * y) * 1.0); Ln [I]. right = x + SQRT (D * D-y * y) * 1.0);} If (! Flag) {sort (LN, LN + N, CMP); int res = 1; // blind. Here it is written as int double TMP = ln [0]. right; for (I = 1; I <n; I ++) {If (Ln [I]. left <= TMP & ln [I]. right> = TMP) continue; else {TMP = ln [I]. right; Res ++ ;}} printf ("case % d: % d \ n", cases, Res);} else {printf ("case % d: -1 \ n ", cases) ;}} 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.