您的位置:首页 > 其它

HDU - 1896

2017-07-25 15:38 102 查看

Stones

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

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同学扔石子的全过程。对于每个石子来说,都需要记录两个信息:1.当前位置 2.能扔的距离。因此采用一个结构体表示,同时用一个优先队列priority_queue表示Sempr同学还能遇到的石子,越先遇到的优先级越高。

我们会发现如果有n个石子,整个过程可看做n-1次 捡起石子丢出去,路过下一个。最后再做一次丢出,此时就得到了最远距离。

其中注意一下 优先队列的判定方式 不要写错即可。

源代码

#include<iostream>
#include<stdio.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

int times, num;
struct Node {
int pos;
int dis;
friend bool operator < (Node a, Node b){
if (a.pos == b.pos)
return a.dis > b.dis;
else
return a.pos > b.pos;
}

};
priority_queue<Node> q;
Node temp;

int main() {
scanf("%d", ×);
int i;
for (int ti = 0; ti < times; ti++) {
scanf("%d", &num);
for (i = 0; i < num; i++) {
scanf("%d%d", &temp.pos, &temp.dis);
q.push(temp);
}
for(i=0;i<num-1;i++){
temp = q.top();
temp.pos = temp.pos + temp.dis;
q.push(temp);
q.pop();
q.pop();
}
temp = q.top();
printf("%d\n", temp.pos + temp.dis);
q.pop();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  优先队列 HDU