HDU 1896 Stones

来源:互联网 发布:watershed算法原理 编辑:程序博客网 时间:2024/06/07 03:13

HDU 1896 Stones

(http://acm.hdu.edu.cn/showproblem.php?pid=1896)
Problem Description
Because of the wrong status of the bicycle, Sempr begin to walk east to west every morning and walk back every evening. Walking may cause a little tired, so Sempr always play some games this time.
There are many stones on the road, when he meet a stone, he will throw it ahead as far as possible if it is the odd stone he meet, or leave it where it was if it is the even stone. Now give you some informations about the stones on the road, you are to tell me the distance from the start point to the farthest stone after Sempr walk by. Please pay attention that if two or more stones stay at the same position, you will meet the larger one(the one with the smallest Di, as described in the Input) first.

Input
In the first line, there is an Integer T(1<=T<=10), which means the test cases in the input file. Then followed by T test cases.
For each test case, I will give you an Integer N(0

#include <iostream>#include <queue>using namespace std;struct node{    int p, d;    //bool operator<(const node t)const  //越近的优先级越高    //{    //    if(p!=t.p)    //        return t.p < p;    //    return t.d < d;    //}    friend bool operator < (node a, node b)//重载操作符,越近的优先级越高    {        if(a.p!=b.p)            return a.p > b.p;        return a.d > b.d;    }};int main(){    int t, n;    cin >> t;    node stone;    while(t--)    {        cin >> n;        priority_queue<node> q;        for(int i = 0; i < n; i++)        {            cin >> stone.p >> stone.d;            q.push(stone);        }        bool flag = true;        while(!q.empty())        {            stone = q.top();//stone最终会是最后的一块石头            q.pop();//先将未抛出的节点出队            if(flag)            {                flag = false;                stone.p += stone.d;                q.push(stone);//再将抛出后的节点入队并自动排序            }            else//偶数则不抛                flag = true;        }        cout << stone.p << endl;    }    return 0;}
原创粉丝点击