2013-05-29 15:34:19Morris

[UVA] 12150 - Pole Position

In car races, there is always a high pole next to the finish line of the track. Before the race starts, the pole is used to display the starting grid. The number of the first car in the grid is displayed at the top of the pole, the number of the car in second place is shown below that, and so on.

During the race, the pole is used to display the current position of each car: the car that is winning the race has its number displayed at the top of the pole, followed by the car that is in second place, and so on.

Besides showing the current position of a car, the pole is also used to display the number of positions the cars have won or lost, relative to the starting grid. This is done by showing, side by side to the car number, an integer number. A positive value v beside a car number in the pole means that car has won v positions relative to the starting grid. A negative value v means that car has lost v positions relative to the starting grid. A zero beside a car number in the pole means the car has neither won nor lost any positions relative to the starting grid (the car is in the same position it started).

We are in the middle of the Swedish Grand Prix, the last race of the World Championship. The race director, Dr. Shoo Makra, is getting worried: there have been some complaints that the software that controls the pole position system is defective, showing information that does not reflect the true race order.

Dr. Shoo Makra devised a way to check whether the pole system is working properly. Given the information currently displayed in the pole, he wants to reconstruct the starting grid of the race. If it is possible to reconstruct a valid starting grid, he plans to check it against the real starting grid. On the other hand, if it is not possible to reconstruct a valid starting grid, the pole system is indeed defective.

Can you help Dr. Shoo Makra?

Input

The input contains several test cases. The first line of a test case contains one integer N indicating the number of cars in the race (2 ≤ N ≤ 103). Each of the next N lines contains two integers C and P, separated by one space, representing respectively a car number (1 ≤ C ≤ 104) and the number of positions that car has won or lost relative to the starting grid ( -106P ≤ 106), according to the pole system. All cars in a race have different numbers.

The end of input is indicated by a line containing only one zero.

Output

For each test case in the input, your program must print a single line, containing the reconstructed starting grid, with car numbers separated by single spaces. If it is not possible to reconstruct a valid starting grid, the line must contain only the value -1.

Sample input
4
1 0
3 1
2 -1
4 0
4
22 1
9 1
13 0
21 -2
3
19 1
9 -345
17 0
7
2 2
8 0
5 -2
7 1
1 1
9 1
3 -3
0
Output for the sample input
1 2 3 4
-1
-1
5 8 2 3 7 1 9

ACM ICPC :: South American Regional 2008


我想罵這一題,只因為我看不太懂題目。什麼是 the number of position win or lost?
簡單的說,給一個 list,左邊是玩家 ID,右邊是對照原本的排名,如果往下掉就是個負數,
如果往上升就是個正數。詢問原本的排名是否存在?

如果存在的話,把原本排名列出來。

#include <stdio.h>

int main() {
    int n;
    while(scanf("%d", &n) == 1 && n) {
        int rank[10005] = {}, err = 0;
        int x, v, i;
        for(i = 0; i < n; i++) {
            scanf("%d %d", &x, &v);
            if(i+v < 0)     err = 1;
            if(i+v >= n)    err = 1;
            if(!err && rank[i+v])   err = 1;
            if(err) continue;
            rank[i+v] = x;
        }
        if(err) {
            puts("-1");
            continue;
        }
        printf("%d", rank[0]);
        for(i = 1; i < n; i++)
            printf(" %d", rank[i]);
        puts("");
    }
    return 0;
}