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
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.
2 2 1 5 2 4 2 1 5 6 6
就是扔石头的问题,因为涉及到优先级,所以用到一个operator函数
题目大意就是:一个人扔石头,碰到奇数个石头就扔,仍di远,偶数不处理,如果两个石头在同一个位置,仍小的。
#include<stdio.h>
#include<queue>
using namespace std;
struct stone
{
int pi,di;
}t;
bool operator<(stone a,stone b)
{
if(a.pi==b.pi)
return a.di>b.di;
return a.pi>b.pi;
}
int main()
{
int n,i;
scanf("%d",&n);
priority_queue<stone>q;
while(n--)
{
int m;
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%d%d",&t.pi,&t.di);
q.push(t);
}
int sum=1;
while(!q.empty())
{
t=q.top();
q.pop();
if(sum%2)
{
t.pi=t.pi+t.di;
q.push(t);
}
sum++;
}
printf("%d\n",t.pi);
}
return 0;
}