POJ3069 (greedy + clever with priority queue)

Source: Internet
Author: User

Topic Portal:http://poj.org/problem?id=3069

The main idea : there are n points in a straight line. The distance from point I is XI. Select several plus tags from these points. Requirements: For each point, there must be a marked point (including itself) within the range of R. At least how many points must be marked to satisfy the requirement.

The title means to find the least number of Mark points, very good understanding, greedy, because recently is looking at priority queue, suddenly feel this problem can also use priority queue to do, practice a pitch, also past;

Words do not say more than the code:

CODE:

1#include <bits/stdc++.h>2 3 using namespacestd;4 5 intMain ()6 {7     intr, N, num, NUM0;8priority_queue<int, vector<int, greater<int> > Q;//priority queue, but explained more.9      while(SCANF ("%d%d", &r, &n) && (r! =-1|| n! =-1))Ten     { One          while(!q.empty ()) A Q.pop (); -          for(inti =0; I < n; i++) -         { thescanf"%d", &num); - Q.push (num); -         } -         intAns =0;//the answer to the final output +          while(Q.size () >0) -         { +             intMID = Q.top ();//the mid at this point represents the leftmost point . A              while(Q.size () >0)//This cycle is about finding the point that you want to mark. at             { -                 if(Mid + R >=q.top ()) -                 { -NUM0 =q.top (); - Q.pop (); -                 } in                 Else -                 { toMID = NUM0;//at this point, mid is the spot to mark. +                      Break; -                 } the             } *  $              while(Q.size () >0)//This loop is about finding the right border.Panax Notoginseng             { -                 if(Mid + R >=q.top ()) the Q.pop (); +                 Else A                   Break; the             } +ans++; -         } $printf"%d\n", ans); $     } -     return 0; -}

Although using the priority queue, but not too much technical components, personal feel more comfortable ...

POJ3069 (greedy + clever with priority queue)

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.