poj 2431 优先队列,贪心

题意:从当前位置到目的地,给出初始油量和距离,给出一系列的加油站离终点的距离和可以加的油量,每走一个单位消耗一个单位油量,求要到达目的地最少要在几个加油站下车加油。

题解:既然是最少,那么尽可能在油消耗完的时候给加油,如果再走a米的路程中注定要加一次油,那么就选择这段路程油量最大的加油站下车

代码实现就是每经过一个加油站将这个加油站的油量存下来, 等到不得不下车加油的时候,就选择储存的这些数据中最大的那个(不一定是在油刚刚消耗完的时候,在那之前也行),并且选择完了以后将这个数据移出。直到到达目的地。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
struct Node
{
    int num;
    Node* next;
};
struct Point
{
    int dist;
    int fuel;
};
Node *root;
int n;
int L, res;
void input(Node *a)
{
    Node *beg = root;
    while(beg->next != NULL && beg->next->num >= a->num) beg = beg->next;
    a->next = beg->next;
    beg->next = a;
}
bool cmp(const Point a, const Point b)
{
    if(L - a.dist != L - b.dist) return L - a.dist < L - b.dist;
    return a.fuel < b.fuel;
}
Node *newnode(){return (Node*)malloc(sizeof(Node*));}
int main()
{
    while(scanf("%d", &n) != EOF)
    {
        root = (Node *)malloc(sizeof(Node *));
        root->num = 0xffffff;
        root->next = NULL;
        Point p[10005];
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &p[i].dist, &p[i].fuel);
        }

        scanf("%d%d", &L, &res);
        sort(p, p + n, cmp);
        Node *n0 = newnode();
        n0->num = res;
        input(n0);
        int ii = 0;
        int cango = 0;
        int ans = -1;
        while(root->next != NULL)
        {
            Node *n1 = root->next;
            root->next = n1->next;
            cango += n1->num;
            //printf("cango = %d\n", cango);
            ans++;if(cango >= L)break;

            for(; L - p[ii].dist <= cango; ii++)
            {
                Node *n0 = newnode();
                n0->num = p[ii].fuel;
                input(n0);
            }
        }
        if(cango < L) cout << "-1" << endl;
        else printf("%d\n", ans);

    }
}

可以用c++中的容器优先队列

priority_queue。
时间: 2024-08-26 09:16:20

poj 2431 优先队列,贪心的相关文章

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 贪心

题意:一辆汽车由起点开往小镇,总路程为L,路上有N个加油站,第i个加油站距离小镇a[i],最多可为提供b[i]的汽油,汽车开始时有P单位汽油,问汽车内否到达小镇,若能到达输出最小的加油次数. 思路:每经过一个加油站i,汽车就获得了一次在任何时候加油b[i]的权利,当汽车不足以到达下一站时,就加入过往的最大的b值. #include<stdio.h> #include<queue> #include<iostream> #include<algorithm>

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 1862 Stripies 贪心+优先队列

http://poj.org/problem?id=1862 题目大意: 有一种生物能两两合并,合并之前的重量分别为m1和m2,合并之后变为2*sqrt(m1*m2),现在给定n个这样的生物,求合并成一个的最小重量 思路: m1+m2 >=  2*sqrt(m1*m2) 所以每次取大的去合并,能变小. 直接优先队列就可以啦. #include<cstdio> #include<cmath> #include<queue> using namespace std;

[POJ 2431]Expedition

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

hdu 4544 湫湫系列故事——消灭兔子 优先队列+贪心

将兔子的血量从小到大排序,箭的威力也从小到大排序, 对于每只兔子将威力大于血量的箭加入队列,写个优先队列使得出来数位价钱最少.. #include<stdio.h> #include<queue> #include<algorithm> #include<iostream> #include<functional> using namespace std; const int maxn=100010; struct tt { int d; int

poj 1456 Supermarket (贪心+并查集)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int fa[10010]; struct node { int p; int d; }; struct node a[10010]; bool cmp(node a1,node a2)//利润从大到小 { return a1.p>a2.p; } int find(int x) { if(fa[x]