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