HDU 1896 Stones 优先队列

本文介绍了一道 HDU ACM 竞赛题目(编号 1896),该题要求通过优先队列算法求解石头投掷问题。文章详细解释了题目背景及解决思路,并提供了一个 C++ 实现示例,使用了重载小于号来定义优先级。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

http://acm.hdu.edu.cn/showproblem.php?pid=1896

题意:

路上有一些石头,给定每个石头所在的位置和石头能扔到的距离。现在从位置较小的石头开始走,若是遇到的第奇数块石头,就捡起来扔到前面去,若是第偶数块,就不管,若一个位置上有一块以上的石头,则优先选择扔的较近的那个,问最后最远的石头位置

思路:

优先队列简单应用,重载小于号,位置为第一关键字,扔的距离为第二关键字

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
#include <queue>
using namespace std;

typedef long long ll;
struct node
{
    ll pos, val;
    node(ll a = 0, ll b = 0)
    {
        pos = a, val = b;
    }
    friend operator< (node a, node b)
    {
        return a.pos != b.pos ? a.pos > b.pos : a.val > b.val;
    }
};
int main()
{
    int t, n, a, b;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        priority_queue<node> que;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &a, &b);
            que.push(node(a, b));
        }
        int cnt = 0;
        ll res = 0;
        while(! que.empty())
        {
            node p = que.top(); que.pop();
            res = max(res, p.pos);
            cnt++;
            if(cnt & 1) que.push(node(p.pos+p.val, p.val));
        }
        printf("%I64d\n", res);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值