您的位置:首页 > 其它

HDOJ1896Stones(队列优先级)

2016-07-26 10:14 387 查看
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

题意:在路边踢石头,如果是奇数次遇见石头,则把他踢走,偶数次不管。告诉你石头的位置还有能踢多远,问最远的石头距离出发点多远。如果同时遇见石头,先看到体积大的石头即扔的距离越近体积越大
思路:队列优先级,将扔的距离大的(即体积小的)石头还有所在距离大的放在队列后面。然后所有的数都进队列。判断奇数次遇见石头=踢走(即进队列),偶数次不管。每一次遇见石头都更新最远距离。
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
struct st{
int place;
int dis;
bool friend operator<(st x,st y)
{
if(x.place!=y.place)
return x.place>y.place;
else
return x.dis>y.dis;
}
}a;
int main()
{
priority_queue<st > que;
int t,n;
cin>>t;
while(t--)
{
int k=1;
int max=0;
cin>>n;
while(n--)
{
cin>>a.place>>a.dis;
que.push(a);
}
while(!que.empty())
{
if(k%2!=0)
{
a=que.top();
if(a.place>max)
max=a.place;
que.pop();
a.place+=a.dis;
que.push(a);
}
else
{
a=que.top();
if(a.place>max)
max=a.place;
que.pop();
}
k++;
}
cout<<max<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: