poj 2431 Expedition(优先队列+贪心)

来源:互联网 发布:artrage mac 中文 编辑:程序博客网 时间:2024/05/01 13:58

原题地址:http://poj.org/problem?id=2431
题意:给定n个加油站,每个加油站有2个信息,一个是距离终点站的长度,一个是可加的油量,现在给出路程总长度和和初始油量,让你判断是否能将车开到终点站,若能输出最少加油次数,若不能,输出-1
Expedition
Time Limit: 1000MS

Memory Limit: 65536K
Total Submissions: 19168

Accepted: 5523
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
    4
    4 4
    5 2
    11 5
    15 10
    25 10
    Sample Output
    2
    Hint
    INPUT DETAILS:

The truck is 25 units away from the town; the truck has 10 units of fuel. Along the road, there are 4 fuel stops at distances 4, 5, 11, and 15 from the town (so these are initially at distances 21, 20, 14, and 10 from the truck). These fuel stops can supply up to 4, 2, 5, and 10 units of fuel, respectively.

OUTPUT DETAILS:

Drive 10 units, stop to acquire 10 more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then drive to the town.
分析:1.首先数据是两个有关系的,所以考虑用结构体来存储
2.然后是为了解题方便,考虑将最后的终点站也考虑成加油站,不过是可加油的数量为0的加油站
3.现在考虑如何加油是最优的,显然我们每经过一个加油站就去考虑是否要去加这个加油站的油是不合理的,因为这样子太烦琐了,所以可以考虑换个角度。我们可以这么思考,用优先队列来存储数据,每当经过一个加油站,就可以将这个加油站存入优先队列,一旦发现油不够了,这时就可以使用任何一个之前经过的加油站的油,那么最优的方案显然是将油量最多的加油站的油加到车子中。
4.退出的条件是什么?很容易想到,当你需要油的时候却发现优先队列已经空了,这很明显是不满足题意的,(因为终点站是最后一个加油站,只有当你开到了最后的加油站,才满足题意)

下面是ac代码

#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>#include <math.h>#include <ctype.h>#include <map>#include <set>#include <vector>#include <queue>#define inf 0x3f3f3f3f#define eps 1e-8typedef long long ll;using namespace std;struct node{    int x;    int y;    bool operator <(const node a) const    {        return y<a.y;    }} q[10005];bool cmp(node a,node b){    return a.x<b.x;}int main(){    int n,distance,sum;    priority_queue<node>que;    while(~scanf("%d",&n))    {        int ans=0,flag=1;//flag用于标记是否满足题意        for(int i=0; i<n; i++)        {            scanf("%d%d",&q[i].x,&q[i].y);        }        scanf("%d%d",&distance,&sum);        for(int i=0; i<n; i++)        {            q[i].x=distance-q[i].x;//因为题目数据是离终点站的距离,所以为了方便计算,将其转化为离起点站的距离        }        q[n].x=distance;        q[n++].y=0;        sort(q,q+n,cmp);        for(int i=0; i<n; i++)        {            if(q[i].x<=sum)            {                que.push(q[i]);            }            while(q[i].x>sum)            {                if(que.empty())                {                    flag=0;                    break;                }                sum+=que.top().y;                que.pop();                ans++;                i--;//如果没有i--,就会导致没有判断q[i]的入列情况,会wa            }        }        if(flag==0) printf("-1\n");        else printf("%d\n",ans);    }    return 0;}
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 法硕在职双证 在职硕士双证 双证在职研究生报考条件 在职法硕双证 双证在职研究生有哪些 在职双证 在职博士双证 一月双证在职研究生 双证在职研究生算第一学历吗 双证是什么意思 在职mba双证 在职研究生双证报考 在职研究生 双证 成人研究生报考条件 在职研究生考试报名 在职考研条件 中国在职研究生网 mpa在职研究生 怎么报考在职研究生 在职研究生统考 2017在职研究生招生简章 在职研究生学历 北大在职研究生招生网 在职研究生报名时间 在职研究生报考时间 中国在职研究生教育信息网 在职研究生哪个专业好考 浙大在职研究生招生网 怎么考在职研究生 四川大学在职研究生 在职研 北京在职研究生 在职研究生考试内容 武汉在职研究生报考条件 专业在职研究生 在职硕士报名时间 2017年在职研究生招生简章 在职研究生费用 在读研究生 中央财经大学在职研究生 在职研究生工程硕士