Companies always have a lot of equipment, furniture and other things. All of them should be tracked. To do this, there is an inventory number assigned with each item. It is much easier to create a database by using those numbers and keep the track of everything.
During an audit, you were surprised to find out that the items are not numbered sequentially, and some items even share the same inventory number! There is an urgent need to fix it. You have chosen to make the numbers of the items sequential, starting with 1. Changing a number is quite a time-consuming process, and you would like to make maximum use of the current numbering.
You have been given information on current inventory numbers for n items in the company. Renumber items so that their inventory numbers form a permutation of numbers from 1 to n by changing the number of as few items as possible. Let us remind you that a set of n numbers forms a permutation if all the numbers are in the range from 1 to n, and no two numbers are equal.
Input
The first line contains a single integer n — the number of items (1 ≤ n ≤ 105).
The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 105) — the initial inventory numbers of the items.
Output
Print n numbers — the final inventory numbers of the items in the order they occur in the input. If there are multiple possible answers, you may print any of them.
Examples
Input
3
1 3 2
Output
1 3 2
Input
4
2 2 3 3
Output
2 1 3 4
Input
1
2
Output
1
Note
In the first test the numeration is already a permutation, so there is no need to change anything.
In the second test there are two pairs of equal numbers, in each pair you need to replace one number.
In the third test you need to replace 2 by 1, as the numbering should start from one.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve a problem: Given n numbers (1 ≤ ai ≤ 1e5), we want to assign new numbers to some items (changing as few as possible) such that the final numbers form a permutation of 1..n. The output should be the final numbers in the order of input items. We can change any numbers, but we want to minimize the number of changes. We need to output any valid permutation that requires minimal changes.
Observation: The current numbers may already be in range 1..n but some may be duplicates or out of range. The goal is to end up with a permutation of 1..n. If we keep an item's number unchanged, that number must be in 1..n and must be unique among the kept ones. We want to maximize the number of items that keep their number (i.e., minimize changes). This is like: from the given list, we want to select a subset of items to keep their numbers unchanged, such that the numbers are all in [1,n] and distinct, and then assign the remaining numbers (unused numbers in 1..n) to the remaining items (changing them). The maximum size of such subset is the maximum number of items that can retain their original numbers. That is the size of a maximum matching: each item that has a value in [1,n] can potentially keep that value, but we need to ensure no two items keep the same value. So essentially, we need to choose, for each value v in [1,n], at most one item with that original value. Since we want to maximize the count of unchanged items, we can greedily assign items with unique valid numbers to keep them. But we also need to consider that some items may have numbers outside [1,n]; they must be changed because the final numbers must be in 1..n. So we can only keep items with a_i in [1,n] and that value is not already taken.
The maximum number of unchanged items is the number of distinct values among those a_i that are in [1,n], but limited by n of course. However, if there are duplicates, we can keep only one per value. So the maximum unchanged count is: For each v from 1 to n, if there is at least one item with a_i = v, we can keep exactly one such item. That gives the count of distinct values in [1,n] present. That is clearly the maximum possible, because we cannot keep two items with same v, and any item with a_i out of range cannot be kept. So the minimal number of changes = n - (number of distinct values in [1,n] present among a_i). But we also need to be careful: could we keep an item with a_i > n? No, because final numbers must be in 1..n. So they must change. Could we keep an item with a_i <=0? Not possible because a_i >=1. So only in [1,n] eligible.
Thus the problem reduces to: We need to output a permutation of 1..n. For some items, we want to preserve their original value if possible (i.e., if that value hasn't been used already and is in [1,n]). For the rest, assign the missing numbers (the numbers from 1..n that are not used by any kept item) in some order.
However, we must ensure that the number of changes is minimal. The above selection yields maximal kept items. But is there any nuance? Could keeping a certain item prevent us from keeping another item that also has the same value? Obviously duplicates: we can only keep one. But what about the order? The assignment of which specific item among those with the same value we keep does not affect the count, as long as we keep exactly one. So we can arbitrarily decide which item gets to keep that value.
Thus algorithm:
Input n, array a[1..n].
Create a boolean array used_value[1..n] initialized to false. This will mark which numbers from 1..n are already used in the final assignment (i.e., kept unchanged for some item) OR assigned to changed items later? We'll need to assign missing numbers to changed items.
Also, we need a data structure to hold available numbers (the set of numbers from 1..n that are not used as kept numbers). Actually, we need to assign each item a final number. We'll first decide for each item whether we keep its original number. We'll go through items, but we need to avoid using a number more than once.
One approach: First pass: For each item, if a_i is in [1,n] and used_value[a_i] is false, then we set final[i] = a_i, and mark used_value[a_i] = true. Else, we set final[i] = -1 (to be filled later). This greedy pass (in input order) ensures we keep a_i whenever possible; but does that always yield the maximum count? Let's think: Suppose we have duplicates. In what order we assign the kept values might affect ability to assign missing numbers? No, because the kept values are distinct and each corresponds to a value that is in 1..n; there's no conflict with missing numbers assignment. Since we mark used numbers, we can later assign the unused numbers arbitrarily to items with final = -1. This greedy pass will keep a_i for the first occurrence of each value in [1,n]; that yields exactly one kept per distinct value, which is maximum. There's no reason to skip a valid value to allow another item to keep it? Because all items with same value are identical; keeping one yields same count. So greedy is fine.
But is there any scenario where not keeping a value that appears could allow a different assignment that keeps more? Suppose we have a_i = 5, and also a_j = 5, and both are in [1,n]. Keeping one is max, cannot keep both. So fine.
What about a_i = v where v is in [1,n], but we might want to change that item and use v to assign to some other item? That would not increase kept count; it would just swap which item remains unchanged, still same number of unchanged items. So any assignment that uses each distinct value exactly once as a kept value yields the same count. The greedy approach yields a valid assignment with minimal changes.
Thus steps:
Read n, array a.
Initialize used = boolean array size n+1, all false.
Initialize final = array size n, all 0.
For i from 0 to n-1:
if 1 <= a[i] <= n and used[a[i]] == false:
final[i] = a[i];
used[a[i]] = true;
else:
final[i] = 0 (placeholder)
Now we need to assign numbers to placeholders. We need a list of numbers from 1 to n that are not used (i.e., for which used[x] == false). Let missing = list of those numbers.
We need to assign each missing number to a placeholder item. But we must be careful: for placeholder items, we can assign any missing number, except we must ensure that we don't assign a number that equals the original a_i? Actually, we are changing the number; we can assign any number, even if it equals original? But if original was not used (either out of range or duplicate and we didn't keep it), we are allowed to change it to a new number. Could we assign the same number as original? If original was duplicate, we could assign it to another duplicate but that would cause duplicate in final, which is not allowed. So we must assign distinct numbers. Also, if original was in [1,n] but we didn't keep it because used already, we could still assign that same number if it's missing? But if we didn't keep it, that number is already used (by the kept one), so it's not missing. So missing numbers are those not used; they are distinct from all kept numbers. So assigning a missing number to a changed item is fine. Also, if original a_i is out of range ( > n ), we can assign any missing number.
But is there any constraint that we cannot assign a number that equals a_i? That would mean we are not changing it, but we already decided to change it because used flag prevented? If a_i is duplicate and we didn't keep it, then used[a_i] is true, so that number is not missing. So we cannot assign it again. So that's fine. For out-of-range, a_i is > n, not in missing list, so no conflict.
Thus we can simply iterate over placeholder items and assign them numbers from the missing list sequentially.
But careful: The order of assignment may affect something? No, any assignment yields a valid permutation. So we can just assign.
Edge cases: n up to 1e5, a_i up to 1e5. used array size n+1 (since numbers 1..n). For a_i > n, we ignore.
Complexity O(n).
But is there any scenario where greedy first pass fails to produce a valid assignment because it uses some numbers that we later need for some items that must be kept? Actually, the greedy pass uses numbers for items that we keep. But what about an item with a_i in [1,n] but we choose not to keep it to free that number for another item? That would not increase kept count; it's just swapping. The greedy pass yields maximal kept items, but is it always possible to assign missing numbers to the rest? Yes, because we have exactly n - kept count missing numbers, and there are exactly that many placeholders. So assignment is possible.
But wait, could there be an issue if an item's original a_i is in [1,n] but we mark it as placeholder, and later we assign a missing number that is also equal to its original a_i? That would mean original a_i is not used (so missing) and we didn't keep it because used flag was false at the time? Actually, if a_i is in [1,n] and used[a_i] is false at the time we process that item, then we would keep it, not set it as placeholder. So we only set as placeholder if either a_i out of range OR used[a_i] is already true. In the latter case, used[a_i] is true, so that number is not missing. So we cannot assign the same number to it. In the former case, a_i > n, not missing. So the missing numbers are all distinct from any a_i of placeholder items? Not necessarily: Consider a_i = 5, n=5, and we have another item earlier that kept 5. Then used[5] becomes true. The second item with a_i=5 becomes placeholder. Missing numbers are numbers not used; 5 is used, so not missing. So we cannot assign 5 to it. That's fine. Consider a_i = 5, n=5, and there is also a_i=6 (out of range). used[5] might be false when we process the item with a_i=5, we keep it; then we process a_i=6, it's out of range -> placeholder. Missing numbers might include 6? But 6 > n, so missing numbers only up to n, so missing numbers are like 2,3,4 if not used. So we assign some number <=5 to the placeholder; but that number is not 6, so it's fine.
Thus assignment works.
But there is a nuance: The greedy pass in input order might sometimes lead to a situation where a later item has a_i = v that is currently unused, and we assign it as kept. That is fine. But could there be a case where we should not keep an item even if its a_i is available because that would cause later an item with a_i = something else to become impossible to assign missing numbers? No, because missing numbers are all distinct and we have enough; there is no constraint beyond distinctness. So it's always safe to keep a value when available.
Hence the simple algorithm works.
But wait, consider the following: n=2, a = [2,2]. Distinct values in [1,n] is {2}, so we can keep one item unchanged. Our greedy pass: first item: a1=2, used[2]=false -> keep, final[0]=2, used[2]=true. Second item: a2=2, used[2]=true -> placeholder. Missing numbers: used[1]=false, so missing=[1]. Assign to second: final[1]=1. Output: 2 1. That's valid and changes 1 item.
Another example: n=2, a=[1,1]. Keep first 1, second gets 2. OK.
What about n=2, a=[3,3]. Both out of range, so no keep. used all false initially. Greedy: first item: a1=3 (out of range) -> placeholder. second: placeholder. missing numbers: [1,2]. Assign: first gets 1, second gets 2. Output: 1 2. That changes 2 items. Could we do better? No, because both original numbers invalid, so both must change.
Now what about n=3, a=[1,1,2]. Distinct values: 1,2. Keep two items. Greedy: first: keep 1; second: a=1 but used[1] true -> placeholder; third: a=2 unused -> keep 2. missing numbers: used[1]=true, used[2]=true, used[3]=false -> missing=[3]. assign to second: 3. Output: 1,3,2. That changes 1 item. Good.
Now, consider scenario: n=3, a=[2,2,2]. Distinct values: {2}, keep one. Greedy: first keep 2, second placeholder, third placeholder. missing numbers: 1 and 3. Assign: second gets 1, third gets 3. Output: 2,1,3. Changes 2.
All fine.
But is there any scenario where we might want to not keep a value because it might be needed for an item that can't accept any other number? For example, what if an item has a_i = v and v is also needed to assign to some other item because that other item's original is something else but it's forced? There's no such constraint; all placeholder items can accept any missing number.
Thus algorithm is straightforward.
Edge Cases: The sample inputs:
3: 1 3 2 -> all in [1,3] and distinct, so greedy: first keep 1, second keep 3 (used[3] false), third keep 2 (used[2] false). All used, no missing numbers, output same. Good.
1: 2 -> out of range, placeholder. missing: [1]. assign: 1. output 1. matches.
Thus solution accepted.
Implementation details:
n up to 1e5, used array of size n+2 maybe.
For missing numbers, we can generate a list of numbers from 1..n where used[x] is false. Or we can maintain a queue/stack.
Since we need to assign to placeholders in order, we can iterate over placeholder indices and pop from missing list.
Time O(n).
Now, verify that this approach indeed minimizes number of changes: The number of kept items = number of distinct values in [1,n] present. Is it always possible to achieve that many unchanged items? Yes, by keeping exactly one item per distinct value, and changing the rest. So min changes = n - count_distinct_valid.
But is there any scenario where we could keep more than count_distinct_valid? Possibly if some numbers outside [1,n] can be kept? No, because final numbers must be in 1..n. So they must change. So no.
Thus solution is correct.
But wait: There is a subtlety: The numbers a_i can be up to 1e5, but n may be less than 1e5. So numbers > n are invalid, numbers <= n are valid only if >=1. So all good.
Thus code is straightforward.
However, consider that the input numbers can be as high as 1e5, but we only care about those <= n. For used array, we only need size n+1.
Now, double-check with random test: n=5, a=[1,2,3,4,5] -> all distinct, keep all, output same.
n=5, a=[1,1,2,2,3] -> distinct values:1,2,3 -> keep 3 items. Greedy: keep first 1, second placeholder, keep third 2, fourth placeholder, keep fifth 3. used:1,2,3 true; missing:4,5. assign to second and fourth: say second=4, fourth=5. Output:1,4,2,5,3. That's valid permutation with 2 changes.
But could we keep 4 items? Suppose we keep first 1, second 1? No duplicate. Or keep second 1 and change first to something else? That would still be 3 kept (since 1,2,3 used). So max 3.
Ok.
Now, check if any issue with order: we must output final numbers in input order. Our method does that.
Now, is there any requirement to output the numbers as a permutation of 1..n? Yes.
Thus solution done.
But let's think about potential pitfalls: The greedy assignment of kept numbers uses the first occurrence of each value. That's fine. But we also need to ensure that when we assign missing numbers to placeholders, we don't inadvertently assign a number that equals the original a_i of that placeholder if that number is missing? As argued, that can't happen because if a_i is in [1,n] and it's missing (i.e., not used), then used[a_i] would be false when we processed that item. So we would have kept it, not made it placeholder. Contradiction. So for placeholder items, either a_i out of range, or a_i in [1,n] but used[a_i] is true (so not missing). So we never assign a missing number that equals a_i of placeholder? Actually, if a_i > n, missing numbers are <=n, so cannot equal. If a_i in [1,n] and used[a_i] true, missing numbers exclude a_i. So safe.
Thus no conflict.
Now, the only thing: need to ensure we use 64-bit? No, numbers up to n, int fine.
Complexities: O(n).
Now, let's consider a more elaborate approach if there was a twist, but not needed.
Thus code will be:
code
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i=0; i<n; i++) cin >> a[i];
vector<bool> used(n+1, false); // 1-indexed
vector<int> final(n, 0); // 0 means to be assigned
for (int i=0; i<n; i++) {
if (a[i] >= 1 && a[i] <= n && !used[a[i]]) {
final[i] = a[i];
used[a[i]] = true;
}
}
// collect missing numbers
vector<int> missing;
for (int v=1; v<=n; v++) {
if (!used[v]) missing.push_back(v);
}
// assign missing to placeholder positions
int idx = 0;
for (int i=0; i<n; i++) {
if (final[i] == 0) {
final[i] = missing[idx++];
}
}
// output
for (int i=0; i<n; i++) {
cout << final[i] << " ";
}
cout << endl;
return 0;
}
But careful: The placeholder final[i] is set to 0, but 0 could also be a valid number? No, numbers are from 1 to n, so 0 is safe as sentinel.
But what about the case where a_i is 0? Input says 1 ≤ ai ≤ 10^5, so no 0.
Thus good.
But is there any scenario where there are more placeholders than missing numbers? The count of placeholders is n - kept. The count of missing numbers is n - (number of distinct values kept). But number of kept items equals number of distinct values we used. In greedy, we keep exactly one per distinct value encountered and available. However, it's possible that we kept fewer than the number of distinct values? Because we only keep when a_i is in range and used false. But suppose there are duplicates of a value v. The first occurrence sets used[v]=true, subsequent ones become placeholders. So kept count = number of distinct values that appear at least once (and are in range). That is exactly the number of distinct values in [1,n] present. But could there be a case where a value v appears but we never have the chance to keep it because we process an item with v after another item with same v? Still we keep the first. So we keep exactly one per distinct value that appears. So kept count = number of distinct values present. Thus missing count = n - distinct_count. The number of placeholders = n - kept = n - distinct_count. So they match.
Thus assignment works.
But we must ensure that missing numbers list is not empty if n == kept? Then no placeholders; assignment loop won't run.
Now, potential issue: What if there is a value v that appears but we also have a placeholder that originally has a_i = v and we assign a missing number that is also v? As argued, not possible. But let's verify with a counterexample: n=2, a=[1,1]. Process first: keep 1. used[1]=true. Second: placeholder. missing = [2]. assign 2, not 1. Good.
Another: n=2, a=[2,1]. Process first: 2 keep, used[2]=true. Second: 1 keep, used[1]=true. No placeholder. Fine.
Another: n=3, a=[2,2,2]. Process first: keep 2, used[2]=true. Second: placeholder. Third: placeholder. missing = [1,3]. assign 1 and 3.
All good.
Thus solution correct.
Now consider time and memory: O(n) each, fine.
Now, can there be any more efficient way? Not needed.
Now, finalize.
But wait, is there any chance that we could keep an item with a_i > n by reassigning some number? That would be changing, not keeping. So no.
Thus final answer.
We might need to ensure output format: spaces between numbers, trailing space allowed? In examples they have trailing space. So fine.
Now, I'll produce final solution explanation.
However, the problem statement says: "Renumber items so that their inventory numbers form a permutation of numbers from 1 to n by changing the number of as few items as possible." And "If there are multiple possible answers, you may print any of them."
So our algorithm indeed yields an answer with minimal changes.
But let's double-check the proof of optimality: The minimal number of changes is at least n - (number of distinct values in [1,n] present) because among the final permutation, the values that can remain unchanged must be a set of distinct numbers from the original set and within 1..n. So the maximum size of such set is the size of a largest set of items with distinct values in [1,n]; that is exactly the number of distinct values in [1,n] present (since we can pick at most one per value). So changes >= n - distinct_valid. Our construction achieves exactly that. So optimal.
Thus solution is correct.
Now, final answer: we'll provide the code in C++.
But before final, think about possible pitfalls with large n and reading input quickly: use scanf/printf or cin with sync. For 1e5, cin is fine.
Now, for the missing list, we could also use a queue or a pointer, but vector is fine.
Edge Cases:
n=100000, all a_i are 1e5+1? Actually, a_i max 1e5, but n could be 1e5, so a_i <=1e5. If n=1e5, a_i could be 1e5, which is within range. So no out-of-range huge numbers beyond used array? used array size n+1 = 100001, fine.
All a_i are 1 (n=100000). Then we keep first 1, and have 99999 placeholders. missing numbers: 2..100000 (99999 numbers). Assignment works.
Now, memory: used vector may be efficient; vector final size n; missing size up to n; fine.
Thus final.
But wait: the algorithm assigns missing numbers to placeholders in input order. Could it ever assign a missing number that is equal to the original a_i of the placeholder if the placeholder's a_i is out of range? No, out of range > n, missing <= n. So fine.
But what about placeholder with a_i in [1,n] and used true? That a_i is not missing, so we won't assign it. So fine.
Now, what if we had some placeholder whose original a_i is in [1,n] and used true, but the missing number assigned might be something else, okay.
Thus final solution.
I'll now produce final answer: explanation and code.
But the instruction says "final" channel. I'll output the code.
Now, I'm ChatGPT, I'll produce final answer. We need to renumber n items so that they form a permutation of numbers 1…n, changing as few items as possible.
An item can keep its current number only if that number is in [1, n] and no other item keeps the same number.
Hence the maximum number of unchanged items equals the number of distinct values among the input that lie in [1, n].
Algorithm
Read n and the array a.
Create a boolean array used[1..n], initially all false, to mark which numbers are already taken in the final permutation.
Create an array final[1..n] for the answer.
First pass over the items in input order:
If 1 ≤ a[i] ≤ n and used[a[i]] is false, then we keep the number: final[i] = a[i] and set used[a[i]] = true.
Otherwise, we mark the item for later assignment (final[i] = 0).
Collect all numbers from 1 to n that are not yet used – these are the missing numbers.
Assign each missing number to one of the items that still have final[i] == 0 (any order works).
Output the final array.
This construction uses exactly one item per distinct valid value, so it changes the minimal possible number of items.
Time complexity: O(n). Memory: O(n).
cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
vector<bool> used(n + 1, false);
vector<int> final(n, 0);
// Keep numbers when possible
for (int i = 0; i < n; ++i) {
if (a[i] >= 1 && a[i] <= n && !used[a[i]]) {
final[i] = a[i];
used[a[i]] = true;
}
}
// Gather numbers that still aren't used
vector<int> missing;
for (int v = 1; v <= n; ++v)
if (!used[v])
missing.push_back(v);
// Fill the remaining positions
int pos = 0;
for (int i = 0; i < n; ++i) {
if (final[i] == 0) {
final[i] = missing[pos++];
}
}
// Output
for (int i = 0; i < n; ++i) {
cout << final[i] << " ";
}
cout << "\n";
return 0;
}