hdu1896 Stones(优先队列水)

本文探讨了如何使用队列高效处理大量数据,通过实例展示了奇偶数筛选、位置累加的方法,并成功解决了一个复杂问题。重点介绍了数据结构队列的应用场景与优化策略。

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

ahhh刚开始不知道怎么输入,是一个一个输跟着处理,还是全输好存起,可是一看1000000的数据量就不知道怎么搞了。。。看了别人的没想到队列居然能忍受这么大的数据,于是就开始敲,按着自己的思路,奇数的入队列,偶数的出队列,位置累加,终于1A了(好久没有一次A掉了)大笑

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;

const int N = 5000;
const int INF = 1000000;

struct node
{
    int st, dis;
    friend bool operator < (const node &a, const node &b)
    {
        if(a.st != b.st) return a.st > b.st;
        else return a.dis > b.dis;
    }
};

int main()
{
  //  freopen("in.txt", "r", stdin);
    int T, n, num;
    scanf("%d", &T);
    while(T --)
    {
        priority_queue <node> q;
        scanf("%d", &n);
        while(n --)
        {
            node tmp;
            scanf("%d%d", &tmp.st, &tmp.dis);
            q.push(tmp);
        }
        num = 1;
        while(q.size() > 1)
        {
            if(num == 1)
            {
                node tmp1 = q.top();
                q.pop();
                tmp1.st += tmp1.dis;
                q.push(tmp1);
                num = 0;
            }
            else if(num == 0)
            {
                q.pop();
                num = 1;
            }
        }
        node tmp2 = q.top();
        printf("%d\n", tmp2.st + tmp2.dis);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值