poj 3431 Expedition 優先隊列
阿新 • • 發佈:2017-11-30
node 位置 pro exp 起點 -i pty const return
poj 3431 Expedition 優先隊列
題目鏈接:
http://poj.org/problem?id=2431
思路:
優先隊列。對於一段能夠達到的距離,優先選擇其中能夠加油最多的站點,這樣,行駛過這段距離之後還能走更遠的距離。
將輸入的數據進行排序處理,按照位置的先後。註意輸入的距離是與終點的,要轉化成與起點的。
代碼:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <queue>
using namespace std;
const int maxn = 10005 ;
struct node {
int a,b;
} ans[maxn];
bool cmp(const node& x, const node& y) {return x.a<y.a;}
priority_queue<int,vector<int>,less<int> > q;
int main() {
int n,l,p;
scanf("%d",&n);
for(int i=0;i<n;++i) scanf("%d %d",&ans[i].a,&ans[i].b);
scanf(" %d %d",&l,&p);
for(int i=0;i<n;++i) ans[i].a=l-ans[i].a;
ans[n].a=l;
ans[n].b=0;
n++;
sort(ans,ans+n,cmp);
int index=0,last=p,cnt=0;
for(int i=0;i<n;++i) {
int dist=ans[i].a-index;
while(dist>last) {
if(q.empty()) {
puts("-1 \n");
return 0;
}
last+=q.top();
q.pop();
cnt++;
}
last-=dist;
index=ans[i].a;
q.push(ans[i].b);
}
printf("%d\n",cnt);
return 0;
}
poj 3431 Expedition 優先隊列