http://acm.hdu.edu.cn/showproblem.php?pid=1896
因为自行车的位置不对,塞姆普开始每天早上从东到西步行,每天晚上步行回来。散步会引起疲劳,所以这次塞姆普总是在路上玩一些游戏。
路上有很多石头,当他遇到一块石头时,如果碰到的是第奇数个的石头,他会尽可能地把它扔到前面;如果碰到的是第偶数个的石头,他会把它留在原处。现在给您一些关于路上石头的信息,您要告诉我从起点到塞姆普路过的最远石头的距离。请注意,如果两个或更多的石头停留在同一位置,您将首先遇到较大的(具有最小Di的,如输入中所述)。
Input
在第一行中,有一个整数T(1<=T<=10),它表示输入文件中的测试用例。
然后是T个测试用例。对于每个测试用例,我将在第一行中给出整数N (0<=N<=100000),这意味着路上的石头数量。然后是N行,行中有两个整数Pi(0<=Pi<=100000)和Di(0<=Di<=1000),这意味着第i个石头的位置以及塞姆普可以把它扔多远。
Output
个测试用例输出一行,如描述所述。
Sample Input
2 2 1 5 2 4 2 1 5 6 6
Sample Output
11 12
思路:优先队列,p小的在前面,p一样则d小的在前面,然后依照题意模拟就行了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
struct node
{
int p,d;
bool operator < (const node &a)const
{
if(p==a.p)
return d>a.d;//同一位置 d小的在前面
return p>a.p; //位置靠前的在前面
}
};
int n;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int i=0;
priority_queue<node> q;
int t1,t2;
for(int i=0;i<n;i++)
{
scanf("%d %d",&t1,&t2);
node a;
a.p=t1,a.d=t2;
q.push(a);
}
int pos;
while(!q.empty())
{
node a=q.top();
pos=a.p;
q.pop();
if((++i)&1)//奇数个
{
a.p+=a.d; //向前扔
q.push(a);//入队
}
}
printf("%d\n",pos);
}
return 0;
}