题目正文
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.
翻译
由于自行车的错误状态,Sempr开始每天早上从东向西走,每天晚上再往回走。走路可能会让人有点累,所以Sempr这次总是玩一些游戏。
路上有很多石头,当他遇到一块石头时,如果他遇到的是奇数的石头,他会把它扔到尽可能远的地方,如果是偶数的石头,他会把它留在原来的地方。现在给你一些关于路上石头的信息,你要告诉我从起点到Sempr经过后最远石头的距离。请注意,如果两个或两个以上的石头停留在同一位置,您将首先遇到较大的石头(输入中描述的Di最小的石头)。
输入
在第一行中,有一个整数T(1<=T<=10),表示输入文件中的测试用例。然后是T检验案例。
对于每个测试用例,我将在第一行给您一个整数N(0<N<=100000),这意味着道路上的石头数量。接着是N行,行中有两个整数Pi(0<=Pi<=100000)和Di(0<=Di<=1000),这意味着第i块石头的位置和Sempr可以扔多远。
输出
只需为一个测试用例输出一行,如描述中所述。
题意
遇到第偶数次不扔石头,当遇到奇数石头,往前扔,一直到前面没有石头为止,最后求最远一块石头到起点的距离

代码
代码:
//优先队列1.先定义一个结构体,结构体中有位置,距离
//2.运用友元函数friend,运算符重载friend bool operator < (node a,node b) {
//3.当两块石头的位置相同时,选择距离远的石头,用if语句,否则返回位置远的石头
//4.在主函数中先定义一个测试用例的变量,并输入
//5.whlie循环中,定义一个优先队列,定义一个石头的数量,并输入,定义一个遇见石头的次数
//定义一个节点
//6.循环定义两个变量,并输入,赋值给结构体的位置和距离,并把该节点进队列
//7.循环遍历队列是否为空,若不为空,取队列顶部元素,并出队列
//8.判断遇见石头的次数是奇数还是偶数,若为奇数,位置加上距离并进队列
//9.遇见石头的次数加加
//10.输出最后的位置结果
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
struct node
{
int pos;
int dis;
friend bool operator<(node a,node b)
{
if(a.pos==b.pos)return a.dis>b.dis;
return a.pos>b.pos;
}
};
int main()
{
int T;
cin>>T;
while(T--)
{
priority_queue<node> p;
int n;
cin>>n;
int num=1;//遇见石块的次数
node temp;
for(int i=1;i<=n;i++)
{
int a,b;
cin>>a>>b;
temp.pos=a;
temp.dis=b;
p.push(temp);
}
while(!p.empty())
{
temp=p.top();
p.pop();
if(num%2)
{
temp.pos+=temp.dis;
p.push(temp);
}
num++;
}
cout<<temp.pos<<endl;
}
}
总结
主要采用优先队列的方法