HDU 1896 Stones 优先队列

来源:互联网 发布:python简明中文教程 编辑:程序博客网 时间:2024/05/22 06:47

题目:

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;}
0 0
原创粉丝点击