POJ 2431 Expedition 贪心

题意:一辆汽车由起点开往小镇,总路程为L,路上有N个加油站,第i个加油站距离小镇a[i],最多可为提供b[i]的汽油,汽车开始时有P单位汽油,问汽车内否到达小镇,若能到达输出最小的加油次数。

思路:每经过一个加油站i,汽车就获得了一次在任何时候加油b[i]的权利,当汽车不足以到达下一站时,就加入过往的最大的b值。

#include<stdio.h>
#include<queue>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#define INF 0x3f3f3f3f
#define LL long long
#define MOD 100000007
#define MAXSIZE 20005

using namespace std;

struct node
{
    int a,b;
}p[MAXSIZE];

int cmp(struct node A,struct node B)
{
    if(A.a != B.a)
        return A.a < B.a;
    return A.b > B.b;
}

int Solve(int n,int l,int k)
{
    priority_queue<int> Q;
    int ans=0,pos=0,hav=k;
    for(int i=0;i<=n;i++)
    {
        int d = p[i].a - pos;
        while(hav - d < 0)
        {
            if(Q.empty())
            {
                return -1;
            }

            hav += Q.top();
            Q.pop();
            ans++;
        }
        hav -= d;
        pos = p[i].a;
        Q.push(p[i].b);
    }
    return ans;
}

int main()
{
    int n,l,k;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&p[i].a,&p[i].b);
        }
        scanf("%d%d",&l,&k);
        for(int i=0;i<n;i++)
            p[i].a = l - p[i].a;
        p[n].a = l;
        p[n].b = 0;
        sort(p,p+n,cmp);
        int ans = Solve(n,l,k);
        printf("%d\n",ans);
    }
    return 0;
}

时间: 2024-10-16 03:36:20

POJ 2431 Expedition 贪心的相关文章

poj 2431 Expedition 贪心+最大堆

当油量不够时从走过的油站中选最大加油量的 #include<iostream> #include<queue> #include<stdlib.h> #include<algorithm> using namespace std; #define MAX_N 10005 struct node{ int dist,fuel; }t[MAX_N]; bool cmp(const node &a,const node &b) { return a

poj 2431 Expedition 贪心+优先队列

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7707   Accepted: 2263 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

POJ 2431 Expedition 贪心 优先级队列

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30702   Accepted: 8457 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 t

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

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6890   Accepted: 2065 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

poj 2431 Expedition(优先队列)

题目链接 http://poj.org/problem?id=2431 Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9243   Accepted: 2700 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor driver

POJ 2431 Expedition (贪心 + 优先级队列)

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7696   Accepted: 2260 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

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

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11538   Accepted: 3336 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 t

[POJ 2431]Expedition

1.题目连接:http://poj.org/problem?id=2431 2.题目大意:你需要开着一辆卡车行驶L单位距离,最开始卡车有P单位汽油,卡车每开一单位距离需要消耗1单位汽油,如果在中途卡车汽油耗尽,卡车就无法继续前行,到不了终点,在途中一共有n个加油站,告诉你每个加油站距离终点的距离和每个加油站可以加的油量,假设卡车的油箱是无穷大的,问卡车至少要加多少次油才能到达终点?卡车到不了终点输出-1 3.题目思路:使用优先队列+贪心思想完成此题,队列中维护到达目前的加油站之前没有用过的加油站

POJ 2431 Expedition (STL 优先权队列)

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8053   Accepted: 2359 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