poj2431

本文地址:http://www.javashuo.com/article/p-bnthqsfi-gv.htmlhtml

題目名稱:Expeditionide

連接:http://poj.org/problem?id=2431spa

題意:有一輛卡車須要行駛 L 單位的距離,剛開始有 P 單位的油,每行駛 1 單位須要消耗 1 單位的油。路上有 N 個加油站,距離終點距離 Ai,能提供最多 Bi 單位的油。輸出最少加油次數,若沒法到達,輸出 -1
code

思路:卡車在中途須要加油時,咱們能夠認爲他能夠以前通過沒油加過油的加油站加,並且貪心的想,咱們要加那個油量 Bi 最多的。
htm

代碼以下:blog

 

 1 #include<cstdio>
 2 #include<queue>
 3 #include<algorithm>
 4 using namespace std;  5 typedef long long LL;  6 struct Fuel {  7     int a,b;  8 } fuel[10005];  9 bool cmp(Fuel a1,Fuel a2) { 10     return a1.a > a2.a; 11 } 12 int main() { 13     int n; 14     scanf("%d", &n); 15     for(int i = 1; i <= n; ++i) { 16         scanf("%d%d", &fuel[i].a, &fuel[i].b); 17  } 18     sort(fuel + 1, fuel + n + 1, cmp); //將數據從起點到終點排序
19     int L, P; 20     scanf("%d%d", &L, &P); 21     int tt = P; 22     n++; 23     fuel[0].a = L; 24     fuel[n].a = fuel[n].b = 0; 25     int sum = 0; 26     priority_queue<int> que; 27     for(int i = 1; i <= n; ++i) { 28         int d = fuel[i - 1].a - fuel[i].a;//printf("%d %d\n",fuel[i].a,fuel[i].b); 29       // 30         while(tt - d < 0) { 31             if(que.empty()) { 32                 puts("-1"); 33                 return 0; 34  } 35             tt += que.top(); 36  que.pop(); 37             sum++; 38  } 39         tt -= d; 40         if(i != n){ 41  que.push(fuel[i].b); 42  } 43  } 44     printf("%d\n", sum); 45     return 0; 46 }
View Code
相關文章
相關標籤/搜索