Loading

HDU 1896 Stones(优先队列)

Stones

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 5240    Accepted Submission(s): 3387

 

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<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
 

题目大意

Sempr上学的路上有一堆石子,每个石子都有一个确定的位置和可以被扔出的距离,Sempr遇到第奇数个石子的时候会把它扔出去,第偶数个石子则不扔,如果在同一个位置有多个石子,扔出距离小的为先遇到的,问被扔的最远的石子在哪。

分析

这个题用优先队列很好解决 维护一个Sempr要遇到石子的优先队列,距离近的先遇到,距离相同的被扔出距离小的先遇到,第奇数个遇到的,将石子的位置加上扔出的距离,然后入队,第偶数个遇到的就直接出队了。

代码实现

#include<bits/stdc++.h>

using namespace std;

typedef struct node 
{
    int add;
    int far;
    friend bool operator < (const node &a,const node b)
    {
        return a.add>b.add||(a.add==b.add&&a.far>b.far);
     } 
}stone;

stone s;

priority_queue<stone>q;

int main()
{
    int t,n,i,x,y;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(i=1;i<=n;i++)
        {
            cin>>x>>y;
            s.add=x;
            s.far=y;
            q.push(s);    
        }
        int k=1,anss=0;
        while(!q.empty())
        {
            if(k&1==1)
            {
                s=q.top();
                s.add+=s.far;
                anss=max(s.add,anss);
                q.pop();
                q.push(s);
            }
            else
            {
                s=q.top();
                anss=max(s.add,anss);
                q.pop();
            }
            k++;
        }
        cout<<anss<<endl;
    }
}

 

posted @ 2019-07-26 23:18  WinterFa1L  阅读(175)  评论(0编辑  收藏  举报