hdoj 1896 Stones 【优先队列】

本文提供了一道经典的算法题目——HDU 1896 Stones的解题思路及AC代码实现。该题需要处理一系列石头及其抛掷规则,最终求出最远石头的位置。通过使用优先队列进行高效处理。

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

题目链接:hdoj 1896 Stones

题意:给你 n 个石头以及每个石头可以扔出去的距离,碰到第i个石头,处理有—— i 为奇数,扔出去;反之忽略。问最后一个石头距离起点的距离,相同位置的石头优先可扔距离小的。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e5+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void getmax(int &a, int b) {a = max(a, b); }
void getmin(int &a, int b) {a = min(a, b); }
void add(LL &x, LL y) { x += y; x %= MOD; }
struct cmp {
    bool operator()(pii a, pii b) {
        return a.fi != b.fi ? a.fi > b.fi : a.se > b.se;
    }
};
priority_queue<pii, vector<pii>, cmp > Q;
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int t; cin >> t;
    while(t--)
    {
        int n; cin >> n;
        for(int i = 0; i < n; i++) {
            int u, v;
            cin >> u >> v;
            Q.push(pii(u, v));
        }
        int id = 0; int ans = 0;
        while(!Q.empty())
        {
            pii u = Q.top(); Q.pop();
            ++id;
            ans = max(ans, u.fi);
            if(id & 1) {
                Q.push(pii(u.fi+u.se, u.se));
            }
        }
        cout << ans << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值