Skip to main content
Back to problems
Codeforces
Hard
Arrays
Hash Maps
Sorting
Unique Values (Hard version)

Choose values for the array so that it satisfies the uniqueness rule for every prefix, or determine that it is impossible.

Acceptance 0%
Problem Statement

You are given an array with some positions fixed and some positions unspecified. Your task is to fill the unspecified positions so that the resulting array satisfies a uniqueness condition related to the values appearing in it.

In the hard version, the constraints are large enough that a direct brute-force search is not feasible. You need to reason about value frequencies and how to assign numbers while keeping the array valid.

If a valid assignment exists, output any one of them. Otherwise, report that it is impossible.

Input Format

  • The first line contains an integer tt — the number of test cases.
  • For each test case, the input describes an array with partially specified values.
  • The exact encoding of fixed and unspecified entries follows the problem statement for the platform version.

Output Format

For each test case, print one valid completed array if it exists. If no valid completion exists, print -1.

Constraints

  • The hard version uses large input sizes.
  • You should assume the solution must be near-linear or O(nlogn)O(n \log n) per test case.
  • All values should fit in standard 32-bit signed integers unless otherwise implied by the original platform statement.
Examples
Sample cases returned by the problem API.

Example 1

Input

1
5
1 0 0 2 0

Output

1 3 4 2 5

Explanation

One possible completion is shown. The completed array uses distinct values where needed and satisfies the uniqueness requirement.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.