poj 2431 Expedition 贪心

简单的说说思路,如果一开始能够去到目的地那么当然不需要加油,否则肯定选择能够够着的油量最大的加油站加油,,不断重复这个贪心的策略即可。

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=1e4+9;
int dist,p,n;
struct S
{
    int d,f;
    bool operator <(const S & xx) const
    {
        return d<xx.d;
    }
}stop[maxn];

struct cmp
{
    bool operator ()(const S &a,const S &b) const
    {
        return a.f<b.f;
    }
};

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        scanf("%d %d",&stop[i].d,&stop[i].f);
        scanf("%d %d",&dist,&p);
        for(int i=1;i<=n;i++)
        stop[i].d=dist-stop[i].d;
        sort(stop+1,stop+1+n);

        int ans=0,t=1;
        bool flag=true;
        priority_queue <S,vector<S>,cmp> q;
        while(p<dist)
        {
            while(t<=n&&stop[t].d<=p) q.push(stop[t++]);
            if(q.empty())
            {
                flag=false;
                break;
            }
            ans++;
            p+=q.top().f;
            q.pop();
        }
        if(flag) cout<<ans<<endl;
        else cout<<-1<<endl;
    }
    return 0;
}


 

 

转载于:https://www.cnblogs.com/james1207/p/3343466.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值