you are given a permutation that contains the sum of every two numbers in the lexicographically minimum permutation |
Farmer John is lining up his N cows (2≤N≤103), numbered 1…N, for a photoshoot. FJ initially planned for the i-th cow from the left to be the cow numbered ai, and wrote down the permutation a1,a2,…,aN on a sheet of paper. Unfortunately, that paper was recently stolen by Farmer Nhoj! Luckily, it might still possible for FJ to recover the permutation that he originally wrote down. Before the sheet was stolen, Bessie recorded the sequence b1,b2,…,bN−1 that satisfies bi=ai+ai+1 for each 1≤i<N. Based on Bessie's information, help FJ restore the "lexicographically minimum" permutation a that could have produced b. A permutation x is lexicographically smaller than a permutation y if for some j, xi=yi for all i<j and xj<yj (in other words, the two permutations are identical up to a certain point, at which x is smaller than y). It is guaranteed that at least one such a exists. SCORING: Test cases 2-4 satisfy N≤8. Test cases 5-10 satisfy no additional constraints. INPUT FORMAT (file photo.in): The first line of input contains a single integer N. The second line contains N−1 space-separated integers b1,b2,…,bN−1. OUTPUT FORMAT (file photo.out): A single line with N space-separated integers a1,a2,…,aN. SAMPLE INPUT: 5 4 6 7 6 SAMPLE OUTPUT: 3 1 5 2 4 a produces b because 3+1=4, 1+5=6, 5+2=7, and 2+4=6. |
Suppose the goal sequence is: 8 4 5 2 7 6 1 3 Then the given sums would be: 12 9 7 9 13 7 4 Trying to work it out by going through the breakdowns of the first sum in order: 12: (1 11) 9: can't work (11 >= 9) 12: (2 10) 9: can't work (11 >= 9) 12: (3 9) 9: can't work (11 >= 9) 12: (4 8) 9: (8 1) 7: (1 6) 9: (6 3) 13: (3 10) 7: can't work (10 >= 7) 12: (5 7) 9: (7 2) 7: (2 5) can't work (5 is dup) 12: (7 5) 9: (5 4) 7: (4 3) 9: (3 6) 13: (6 7) can't work (7 is dup) 12: (8 4) 9: (4 5) 7: (5 2) 9: (2 7) 13: (7 6) 7: (6 1) 4: (1 3) And we get: 8 4 5 2 7 6 1 3 |
|
|
I have a solution, but I won't post it unless you ask me to. |
|
|
100 11 84 77 76 93 74 99 57 30 105 149 127 147 179 126 86 101 124 139 89 113 147 99 88 125 104 43 37 50 117 189 157 98 88 153 146 143 174 124 111 100 120 158 84 89 175 158 149 106 110 115 53 54 74 127 93 32 71 146 96 25 44 43 68 100 82 41 13 71 110 46 78 161 119 44 63 101 90 77 123 143 141 128 89 56 73 126 151 176 162 79 13 32 63 130 191 169 139 108 3 8 76 1 75 18 56 43 14 16 89 60 67 80 99 27 59 42 82 57 32 81 66 33 55 70 34 9 28 22 95 94 63 35 53 100 46 97 77 47 64 36 84 74 10 79 96 62 87 19 91 24 29 25 49 78 15 17 54 92 4 21 23 20 48 52 30 11 2 69 41 5 73 88 31 13 50 51 39 38 85 58 83 45 44 12 61 65 86 90 72 7 6 26 37 93 98 71 68 40 |
|
|
|
|
|
|
// Write out if solved; cry if not if ( !ok ) { cout << "Something's wrong!"; return 1; } |
if ( i % 2 == 0)
even = !even;
toggles.
|
|
3 8 76 1 75 18 56 43 14 16 89 60 67 80 99 27 59 42 82 57 32 81 66 33 55 70 34 9 28 22 95 94 63 35 53 100 46 97 77 47 64 36 84 74 10 79 96 62 87 19 91 24 29 25 49 78 15 17 54 92 4 21 23 20 48 52 30 11 2 69 41 5 73 88 31 13 50 51 39 38 85 58 83 45 44 12 61 65 86 90 72 7 6 26 37 93 98 71 68 40 |
|
|
for(size_t j = i + 1; j < output_size + 1; j++)