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)