优先队列的应用

POJ2431

原题链接   点击打开链接

这道题的解题思路:
我们每经过一个加油站,就可以把这所加油站的油先保存起来,等我们用的时候再把油加进去,因为是求最少加多少次油,因此就要每次都加最多的油,因此就要用到优先队列来保存数据。

直接上代码

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
    int l;
    int p;
    friend bool operator < (node x,node y)
    {
        return x.p<y.p;
    }
}stop[10005];
bool cmp(node x,node y)
{
    if(x.l<y.l)
        return true;
    return false;
}
int main()
{
    priority_queue<node> Q;
    int L,P,n;
    cin>>n;
    n=n+1;
    for(int i=1;i<n;i++)
        cin>>stop[i].l>>stop[i].p;
        stop[n].l=0;
        stop[n].p=0;
        cin>>L>>P;
    for(int i=1;i<=n;i++)
        stop[i].l=L-stop[i].l;
    sort(stop+1,stop+1+n,cmp);
        int ans=0;
        int pos=0;
        for(int i=1;i<=n;i++)
        {
            P=P-(stop[i].l-pos);
            if(P<0)
            {
             while(P<0)
             {
                 if(Q.empty())
                 {
                     cout<<"-1"<<endl;
                     return 0;
                 }
            ans ++;
            node temp=Q.top();
            P += temp.p;
            Q.pop();
             }
            }
            Q.push(stop[i]);
            pos=stop[i].l;
        }
      cout<<ans<<endl;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值