POJ 2431 greedy + priority queue

Source: Internet
Author: User

Test instructions: A truck away from the key L, the existing oil volume p, the truck every 1 meters of oil consumption 1, on the way there are some gas stations, ask at least at a few gas stations refueling can make the truck reach the end or reach the end. Idea: Use priority queue, will be able to go to the gas station to join the priority queue, the oil is not enough when the priority queue to join the highest value of oil, if the oil is not enough when the queue empty can not reach.
#include <cstdio>#include<queue>#include<algorithm>using namespaceStd;priority_queue<int>que;structStop {intDis,fuel;} stop[10010];BOOLCMP (Stop a,stop b) {if(A.dis==b.dis)returnA.fuel>B.fuel; returna.dis<B.dis;}intMain () {intn,l,p;  while(~SCANF ("%d",&N)) { for(intI=0; i<n;i++) {scanf ("%d%d",&stop[i].dis,&Stop[i].fuel); } scanf ("%d%d",&l,&p);  for(intI=0; i<n;i++) {Stop[i].dis=l-Stop[i].dis; } sort (Stop,stop+n,cmp); Stop[n].dis=l;stop[n].fuel=0; intCnt=0, pos=0, flag=0;  for(intI=0; i<=n;i++) {            intd=stop[i].dis-POS;  while(p<d) {if(!Que.empty ()) {P+=Que.top ();                    Que.pop (); CNT++; }Else  Break; }            if(p<d) {flag=1; Puts ("-1"); Break; } P-=D;            Que.push (Stop[i].fuel); POS=Stop[i].dis; }        if(flag==0) printf ("%d\n", CNT);  while(!que.empty ()) Que.pop (); }    return 0;}

POJ 2431 greedy + 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.