#P13326. [GCJ 2012 #2] Mountain View

    ID: 15191 远端评测题 4000ms 1024MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>贪心2012Special Judge构造Google Code Jam

[GCJ 2012 #2] Mountain View

题目描述

You are walking through the mountains. It turns out that in this mountain range there is a peak every kilometer, and there are no intermediate peaks. On every peak, you lie down for a rest, look forward, and perceive one of the peaks in front of you to be the highest one. The peak that looks like it's the highest might not really be the highest, for two reasons: there could be a higher peak that is obscured by another peak that's closer to you, and not as high; or you could be looking down, and a faraway peak could look higher than a nearby one.

To be precise, when we say that peak BB looks like it's the highest from peak AA we mean that BB is further down the road than AA; all peaks between AA and BB are below the line connecting the peaks AA and BB; and all the peaks that are further than BB are below or on this line.

You don't know how high each peak is, but you have a very good memory; you've been on all the peaks; and you remember which peak looks like it's the highest from each of them. You would like to invent a set of heights for the peaks that is consistent with that information. Note that you were lying down when looking, so we assume you always looked from the ground level on each peak.

In this example, the fourth peak looks like it's the highest from the first and third peaks. When you're lying on the second peak, you can't see the fourth peak; the third one obscures it, and looks like it's the highest.

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case consists of two lines. The first contains one number, NN, the number of peaks in the range. You began your trip on peak 11 and went forward to peak NN. The next line contains N1N-1 numbers xix_i. The ii-th number denotes the index of the peak that appeared to be the highest from peak ii (note that peak NN is the last peak, so there are no other peaks to see from there).

输出格式

For each test case, output one line containing "Case #nn: y1y_1 y2y_2 ... yNy_N", where nn is the case number (starting from 11) and yiy_i is the height of the ii-th peak. You can output any solution agreeing with the input data, except that all the heights you output have to be integers between 00 and 10910^9, inclusive.

If no solution is possible, output "Case #nn: Impossible" instead.

4
6
2 3 4 5 6
4
4 4 4
4
3 4 4
4
4 3 4
Case #1: 10 10 10 10 10 2
Case #2: 10 20 40 80
Case #3: Impossible
Case #4: 5 3 6 8

提示

Limits

  • 1T30.1 \leq T \leq 30.
  • i<xiN.i < x_i \leq N.

Test set 1 (13 Pts, Visible Verdict)

  • 2N10.2 \leq N \leq 10.

Test set 2 (14 Pts, Hidden Verdict)

  • 2N2000.2 \leq N \leq 2000.