poj 2431 poj 3253 优先队列的运用

poj 2431:

题意:

一条路起点为0, 终点为l。

卡车初始时在0点,并且有p升油,假设油箱无限大。

给n个加油站,每个加油站距离终点 l 距离为 x[i],可以加的油量为fuel[i]。

问最少加几次油可以到达终点,若不能到达,输出-1。


解析:

《挑战程序设计竞赛》:

“在卡车开往终点的途中,只有在加油站才可以加油。但是,如果认为“在到达加油站i时,就获得了一次可以在之后任何位置加一次fuel[i]升油的机会”。

在解决问题上也可以这样做,之后需要加油时,认为在之前经过该加油站加的油就行了。

那么,因为希望到达终点时的加油次数尽量少,所以当燃料为0了之后再进行加油看上去是一个不错的方法。在燃料为0时,应该使用哪个加油站来加油呢?

显然,应该选择加油量fuel[i]最大的加油站。这里就用到了优先队列。”


所以,按题中,装换排序,用优先队列就行了。


代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 10000 + 10;
const double eps = 1e-9;

int n, l, p;

struct Node
{
    int x, fuel;
    bool operator<(Node a)const
    {
        return fuel < a.fuel;
    }
} node[maxn];

bool cmp(Node a, Node b)
{
    return a.x < b.x;
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    while (~scanf("%d", &n))
    {
        for (int i = 0; i < n; i++)
        {
            scanf("%d%d", &node[i].x, &node[i].fuel);
        }
        scanf("%d%d", &l, &p);
        for (int i = 0; i < n; i++)
            node[i].x = l - node[i].x;
        sort(node, node + n, cmp);
        node[n].x = l;
        node[n++].fuel = 0;
//        for (int i = 0; i < n; i++)
//        {
//            cout << node[i].x << " " << node[i].fuel << endl;
//        }
        priority_queue<Node> q;
//        q.push(node[0]);
//        q.push(node[1]);
//        q.push(node[2]);
//        q.push(node[3]);
//        cout <<endl;
//        while (!q.empty())
//        {
//            cout << q.top().fuel << endl;
//            q.pop();
//        }
        bool flag = true;
        int pos = 0, ans = 0;
        for (int i = 0; i < n; i++)
        {
            int d = node[i].x - pos;
            while (p < d)
            {
                if (q.empty())
                {
                    flag = false;
                    break;
                }
                p += q.top().fuel;
                q.pop();
                ans++;
            }
            if (!flag)
                break;
            pos = node[i].x;
            p -= d;
            q.push(node[i]);
        }
        if (!flag)
            ans = -1;
        printf("%d\n", ans);
    }
    return 0;
}


poj 3253:

贪心+优先队列。

greater的运用。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 20000 + 10;
const double eps = 1e-9;

int n;
int l[maxn];

void solve()
{
    LL ans = 0;
    priority_queue<int, vector<int>, greater<int> > q;
    for (int i = 0; i < n; i++)
        q.push(l[i]);
//    cout << n <<endl;
//    cout << q.size()    << endl;
    while (q.size() > 1)
    {
        int l1, l2;
        l1 = q.top();
        q.pop();
        l2 = q.top();
        q.pop();
//        cout << l1 << l2 << endl;
        ans += (LL)l1 + (LL)l2;
        q.push(l1 + l2);
    }
    printf("%lld\n", ans);
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    while (~scanf("%d", &n))
    {
        for (int i = 0; i < n; i++)
            scanf("%d", &l[i]);
        solve();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值