您的位置:首页 > 其它

hdu 1896(2013.9.15周赛D题)优先队列

2013-09-17 23:35 323 查看

Stones

Time Limit: 3000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID:1896

64-bit integer IO format: %I64d Java class name: Main

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<N<=100,000) in the first line, which means the number of stones on the road. Then followed by N lines and there are two integers Pi(0<=Pi<=100,000) and Di(0<=Di<=1,000) in the line, which means the position
of the i-th stone and how far Sempr can throw it.

Output

Just output one line for one test case, as described in the Description.

Sample Input

2
2
1 5
2 4
2
1 5
6 6


Sample Output

11
12


题意:某人走在路上会遇见一些石头,如果遇见第奇数个石子,则将它扔到前面,如果是偶数,则什么也不做,如果某一个位置上有多个石头,则先遇见扔的比较近的那个,现在给出一些石头的初始位置和能够扔的距离,问到最后最远处的石头离初始位置多远!

思路:搞定了简单的队列,这优先队列也就简单多了,不过用到了重载,好点吃力的。刚开始会一点,所以看了学长们教的课件又百度好多资料才搞明白优先队列。不过还好,终于明白了。重载那个是看别人程序才会的,不全是自己写的,因为刚刚会这优先队列,所以还不会写那重载。而且重载我看了好久,研究了好久才明白怎么回事,唉……原来和平常的比较是相反的,让我理解了好久!!详见程序……

#include<iostream>
#include<queue>
using namespace std;
struct abc
{
    int a,b;
    friend bool operator<(abc c,abc d)   //不能少了friend
    {
        if(c.a==d.a) return c.b>d.b;       //从队头到队尾是按从小到大排序的,和平常的结构体有别。因为加入队列的值是从队尾加入,从队头删除的。
        return c.a>d.a;
    }
}s;
int main()
{
    int max,i,j,n,t;
    priority_queue<abc>q;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(i=1;i<=n;i++)
        {
            cin>>s.a>>s.b;
            q.push(s);
        }
        j=max=0;
        while(!q.empty())
        {
            s=q.top();
            q.pop();
            j++;
            if(j%2)
            {
                if(s.a+s.b>max) max=s.a+s.b;
                s.a+=s.b;   //扔出去后又得重新入列,所以距离相加
                q.push(s);
            }
        }
        cout<<max<<endl;
    }
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: