Poj 1328 greedy, fast sort/minimum radar count

Source: Internet
Author: User
Tags radar

For each island, a radar is required in the range [a, B] to sort the left coordinates of these ranges.

The right coordinate of the first Island is right,

The initial radar count is 1. For the second island, if its right coordinate is smaller than that of the right coordinate, it indicates that range 2 contains range 1, and the radar count does not need to be increased, and set right = the right coordinate of Island 2;

If its right coordinate is greater than right, and the left coordinate of Island 2 is greater than right, the number of radars is + 1, and the right coordinate is set to Island 2.

# Include <algorithm> # include <math. h> # include <iostream> Using   Namespace STD; typedef Struct Island { Double L; Double R;} node;IntQcmp (ConstVoid* Arg1,ConstVoid* Arg2 ){Return(Node *) arg1)-> L> (node *) arg2)-> L )? 1:-1 ;}  Int CMP (node arg1, node arg2 ){ Return Arg1.l <arg2.l ;} Int Main ( Int Argc, Char * Argv []) { // Freopen ("D:/t.txt", "r", stdin );  Int Count;Int R; Int C = 0; While (CIN> count> r & (count | r )){ Bool Err = False ; Node N [1000]; C ++; For ( Int I = 0; I <count; I ++ ){ Double X, Y; CIN> x> Y; y = Y <0? -Y: Y; If (Y> r) {err = True ;} Double D = SQRT (R * r-y * Y); N [I]. L = x-D; n [I]. r = x + D ;}If (ERR) {printf (" Case % d:-1 \ n ", C ); Continue ;} Qsort (n, count,Sizeof(N [0]), qcmp );  // Sort (N, N + count, CMP );  Int Ans = 1; Double Left = N [0]. R; For ( Int J = 1; j <count; j ++ ){ If(N [J]. r <= left) {left = N [J]. R ;}Else{If(N [J]. L> left) {ans ++; left = N [J]. r ;}} } Printf ("Case % d: % d \ n ", C, 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.