標籤:
題目連結:
http://poj.org/problem?id=2431
題意:
一條路上有n個加油站,終點離起點的距離為L,然n個加油站離終點的距離為a[i],每個加油站可以給汽車加b[i]的油,
問最少加多少次可以到達終點,不能到達終點輸出-1。
分析:
要想最少我們肯定是在馬上要沒有的時候加油,然後每次加的應該是最多的油。
因此我們走過第i個加油站的時候,把b[i]放入優先隊列裡,然後不能到達的時候
每次取出一個直到可以到達當前的位置,如果隊列為空白而且還不能動到達當前
位置則永遠不可達。
代碼如下:
#include <iostream>#include <cstdio>#include <queue>#include <algorithm>using namespace std;const int maxn = 1000010;struct stop { int a,b; bool operator <(const struct stop &tmp)const { return a<tmp.a; }} p[maxn];int n,l,pp;void solve(){ p[n].a=l,p[n].b=0; sort(p,p+n+1); int tot = pp; priority_queue<int >Q; while(!Q.empty()) Q.pop(); int ans = 0,pos=0,tag=0; for(int i=0; i<=n; i++) { int dis = p[i].a-pos; while(tot<dis) { if(Q.empty()) { printf("-1\n"); return; } tot+=Q.top(); Q.pop(); ans++; } tot-=dis; pos=p[i].a; Q.push(p[i].b); //printf("tot: %d\n",tot); } printf("%d\n",ans); return ;}int main(){ while(~scanf("%d",&n)) { int a,b; for(int i=0; i<n; i++) scanf("%d%d",&p[i].a,&p[i].b); scanf("%d%d",&l,&pp); for(int i=0; i<n; i++) p[i].a=l-p[i].a; solve(); } return 0;}/***44 45 211 515 1025 10***/
POJ2431 Expedition (優先隊列)