bzoj1747[Usaco2005 open]Expedition 探险*

bzoj1747[Usaco2005 open]Expedition 探险

题意:

n个加油站,每个坐标为x,可加油量为ai。一辆车初始油量为l,从起点到终点,每走一个单位就掉一个单位的油,问最少加油次数。n≤10000,坐标≤1000000。

题解:

先假设永远不加油,之后每走一段路之前如果油不够,就从之前经过的加油站中找一个可加油量最大的站加油并去掉这个加油站,答案+1,这一过程用优先队列维护。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 #define inc(i,j,k) for(int i=j;i<=k;i++)
 6 #define maxn 10010
 7 using namespace std;
 8 
 9 inline int read(){
10     char ch=getchar(); int f=1,x=0;
11     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
12     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
13     return f*x;
14 }
15 int n,l,p,ans,now; struct nd{int p,v;}nds[maxn]; bool cmp(nd a,nd b){return a.p<b.p;}
16 priority_queue<int>q;
17 int main(){
18     n=read(); inc(i,1,n)nds[i].p=read(),nds[i].v=read(); l=read(); p=read();
19     inc(i,1,n)nds[i].p=l-nds[i].p; nds[++n]=(nd){0,p}; nds[++n]=(nd){l,0}; sort(nds+1,nds+n+1,cmp);
20     inc(i,1,n-1){
21         q.push(nds[i].v); while(!q.empty()&&nds[i+1].p-nds[i].p>now)now+=q.top(),q.pop(),ans++;
22         if(q.empty()&&nds[i+1].p-nds[i].p>now){printf("-1"); goto end;} now-=(nds[i+1].p-nds[i].p);
23     }
24     printf("%d",ans-1);
25     end: return 0;
26 }

20161025

原文地址:https://www.cnblogs.com/YuanZiming/p/6013276.html