HDU - Stones(优先队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1896
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)

Problem 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

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

Problem solving report:

Description: 从起始位置出发,第奇数个遇到的石头扔,第偶数个遇到的石头不扔。现在知道有n个石头,且知道每个石头距离起始位置的距离,和能把此石头扔的距离。求扔完石头后,距起始位置最远的石头的距离。
Problem solving: 先将所有的石头信息存入队列。与石头的距离越短,优先级越高。与多个石头距离相等时,则将石头扔出的距离越短优先级越高;依次模拟扔石头的过程,被扔出去的石头,重新进入队列时,它距起始位置的距离要再加上被扔出的距离。

Accepted Code:

#include 
using namespace std;
struct edge {
    int ids, dis;
    bool operator < (const edge & s) const {
        if (s.ids != ids)
            return s.ids < ids;
        return s.dis < dis;
    }
}p;
int main() {
    int t, n;
    scanf("%d", &t);
    while (t--) {
        priority_queue  Q;
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            scanf("%d%d", &p.ids, &p.dis);
            Q.push(p);
        }
        int k = 1;
        while (!(Q.size() <= 1 && !(k & 1))) {
            p = Q.top();
            Q.pop();
            if (k & 1) {
                p.ids += p.dis;
                Q.push(p);
            }
            k++;
        }
        printf("%d\n", Q.top().ids);
    }
    return 0;
}

你可能感兴趣的:(#,数据结构,ACM题解)