POJ2431优先队列

来源:互联网 发布:mac u盘 200m 编辑:程序博客网 时间:2024/06/06 09:03

Expedition
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10296 Accepted: 3002

Description

A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of fuel every unit of distance it travels. 

To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1..100 units at each stop). 

The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000). 

Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all. 

Input

* Line 1: A single integer, N 

* Lines 2..N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop. 

* Line N+2: Two space-separated integers, L and P

Output

* Line 1: A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output -1.

Sample Input

44 45 211 515 1025 10

Sample Output

2


遇到的问题和思路:

        这也是书上的例题(挑战p76),不过是前天看的,今天打算写一下,写的时候都已经忘了是优先队列了,只是单纯记得思路。然后书上对题目有所简化,输入和输出都不是书上这个样子的,所以写起来的时候遇到了点小麻烦。

        思路:因为每一个过程可能加油,也可能不加油,本来可能是动态规划的问题,但是这道题如果用优先队列的话,可以更加简便。将沿途经过的加油站的都先放到队列里面去,然后因为是优先队列,然后就从最顶端取出那个最大的油,看看能取几次就可以了。


给出AC代码:(备注:和书上不一样,我的可能稍微复杂了一点)


#include<cstdio>#include<queue>#include<cmath>#include<cstring>using namespace std;int n, l, p;int dis[10005],oil[10005];void solve(){priority_queue <int > que;que.push(0);int ans = 0;int pos = 0, i = 0;while(!que.empty()){int plat = dis[i] - pos;//等会儿要走的距离 //printf("%d\n", que.size());while(plat > p){//printf("%d\n", que.size());if (que.size() == 0){printf("%d\n",-1);return ;}p = p + que.top();//printf("%d\n", p);que.pop();ans++;}if(p >= plat){p -= plat;pos = dis[i];que.push(oil[i]);i++;}if(i > n)break;}printf("%d\n", ans);}int main(){while(scanf("%d",&n)!=EOF){memset(dis, 0, sizeof(dis));memset(oil, 0, sizeof(oil));for(int i = 0; i < n; i++){scanf("%d%d", &dis[i], &oil[i]);}scanf("%d%d", &l, &p);for(int i = 0; i < n; i++){dis[i] = l - dis[i];}//将距离小的弄到前面来for(int i = 0; i < n - 1; i++){for(int j = i + 1; j < n; j++){if(dis[i] > dis[j]){swap(dis[i], dis[j]);swap(oil[i], oil[j]);}}}dis[n] = l; oil[n] = 0;solve();}return 0;}


0 0
原创粉丝点击