您的位置:首页 > 其它

poj,2431Expedition 贪心 + 优先队列

2016-05-12 10:15 267 查看
题意:从起点到终点,途中有n个加油站,每个加油站的加油的油量有限,一个卡车的油箱无限,每走一个单元要消耗一单元的油,问卡车到达终点的最少加多少次油。(注意:加油站的位置是到终点的距离)

解题思路:当油量充足的时候,一直向前走,并且将途中的加油站加的油量放入优先队列(按大到小排序),当没有油了,优先队列弹出顶端的加油,直到加的油保证可以到达下一个加油站或者终点,把终点当做一个加油站,加的油为0。

#include <iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 10005
struct u
{
int a,b;
bool operator <(const u& p)const
{
return a>p.a;
}
};
u c
;
int main()
{
int n;
while(~scanf("%d",&n))
{
int l,p;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&c[i].a,&c[i].b);
}
scanf("%d%d",&l,&p);
c[0].a=0;
c[0].b=0;
sort(c,c+n+1);
priority_queue<int>q;
int ans=0;
int i;
for(i=0;i<=n;i++)
{
if(l-c[i].a<=p)
{
q.push(c[i].b);
}
else
{
if(q.empty())
break;
p=q.top()+p;
q.pop();
ans++;
i--;
}
}
if(p>=l)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: