• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

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;
}


 

 

posted @ 2013-09-27 19:49  Class Xman  阅读(160)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3