POJ2431 Expedition (優先隊列)

來源:互聯網
上載者:User

標籤:

題目連結:

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 (優先隊列)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.