poj 2431 Expedition
2013-11-22 21:18
232 查看
Expedition
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
Sample Output
这道题可以看成 每经过一个油井 就相当于获得一次加油的机会,我们可以把获得机会压入队列中,每次没油的时候,从优先级队列中pop出一个,就可以了。。
记得距离dis给的是距离城市的距离 不是起点的距离
还有需要sort一下 我刚开始以为sort好了。。。最后敲了sort才对。。。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4600 | Accepted: 1465 |
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
4 4 4 5 2 11 5 15 10 25 10
Sample Output
2
这道题可以看成 每经过一个油井 就相当于获得一次加油的机会,我们可以把获得机会压入队列中,每次没油的时候,从优先级队列中pop出一个,就可以了。。
记得距离dis给的是距离城市的距离 不是起点的距离
还有需要sort一下 我刚开始以为sort好了。。。最后敲了sort才对。。。
#include<iostream> #include<queue> typedef long long LL; #define MAX 10050 using namespace std; int N,l,mycount; LL p; int dis[MAX],oil[MAX]; void swap(int &a,int &b) { int t; t=a;a=b;b=t; } int main() { int i,j,k,cl;//,flag; priority_queue<int> q; while(cin>>N) { mycount=0; for(i=0;i<N;i++) cin>>dis[i]>>oil[i]; cin>>l>>p; for(i=0;i<N;i++) dis[i]=l-dis[i]; cl=0;j=N-1;//flag=false; for(i=0;i<N-1;i++) { k=i; for(j=i+1;j<N;j++) { if(dis[j]<dis[k]) k=j; } if(k!=i) { swap(dis[k],dis[i]); swap(oil[k],oil[i]);} } //cl=p;p=0;j=N-1; cl=0;j=0; while(p>0 && cl<l){ cl+=p; if(cl>=l) break; p=0; //for(;j>=0;j--){ for(;j<N;j++){ if(cl>=dis[j]) { q.push(oil[j]); } else break; } if(!q.empty()) { p+=q.top(); q.pop(); mycount++; }else break; } if(cl>=l) cout<<mycount<<endl; else cout<<-1<<endl; } return 0; }
相关文章推荐
- poj 2431 Expedition 贪心 + 优先队列
- POJ 2431 - Expedition(贪心)
- poj 2431 Expedition 贪心
- POJ 2431 Expedition
- POJ 2431 Expedition
- POJ 2431 Expedition(优先队列、贪心)
- POJ 2431 Expedition
- Poj—2431 Expedition 优先队列使用经典例题
- POJ 2431 Expedition
- POJ 2431 Expedition (贪心 + 优先队列)
- poj 2431 Expedition
- POJ 2431 Expedition
- poj 2431 Expedition
- POJ 2431 Expedition
- POJ 2431:Expedition
- poj 2431 Expedition 贪心
- POJ_2431_Expedition
- POJ 2431 Expedition 贪心
- POJ 2431 Expedition
- POJ 2431 Expedition (优先队列