DMOPC '18 Contest 1 P4 - Sorting

View as PDF

Submit solution


Points: 12 (partial)
Time limit: 1.0s
Java 2.5s
Python 2.5s
Memory limit: 256M

Author:
Problem types

You have a sorted list A of numbers in increasing order from 1 to N. This list has M elements. Some numbers may appear multiple times, so you have recorded the number of occurrences of each number in this list, f1,f2,,fN. However, you messed up when assigning the indices, so fi is actually the number of occurrences of Pi, for some unknown permutation P of 1,2,,N. You recall that AK=X for some K and X. Find a permutation P that satisfies this. If no such permutation P exists, output -1.

You will be rewarded even if you can only determine the existence of such a P. Outputting any permutation of 1,2,,N if there exists a permutation and -1 otherwise for every test case of a subtask will earn you half of the subtask's points, assuming you do not already receive the full points (if one of the permutations outputted is wrong, but there does exist a permutation).

Constraints

1XN
1KM21011
1fi1000000
f1+f2++fN=M

Subtask 1 [10%]

1N8

Subtask 2 [20%]

1N20

Subtask 3 [20%]

1N400

Subtask 4 [10%]

1N2000

Subtask 5 [40%]

1N200000

Input Specification

The first line will contain four space-separated integers N, M, K, and X.
The next line will contain N space-separated integers f1,f2,,fN.

Output Specification

If there exists a permutation, output N space-separated integers P1,P2,,PN. There may be many valid permutations. Any one of them will be accepted.
If there does not exist a permutation, output -1.

Sample Input 1

Copy
3 10 5 1
4 5 1

Sample Output 1

Copy
2 1 3

Explanation for Sample 1

There are six possible lists A:

Copy
1 1 1 1 2 2 2 2 2 3
1 1 1 1 2 3 3 3 3 3
1 1 1 1 1 2 2 2 2 3
1 1 1 1 1 2 3 3 3 3
1 2 2 2 2 3 3 3 3 3
1 2 2 2 2 2 3 3 3 3

The third list satisfies AK=X. The corresponding P which gives this list is 2,1,3.

Sample Input 2

Copy
6 9 4 4
2 2 2 1 1 1

Sample Output 2

Copy
4 5 6 1 2 3

Sample Input 3

Copy
6 9 3 4
2 2 2 1 1 1

Sample Output 3

Copy
-1

Comments

There are no comments at the moment.