Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.
Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.
Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.
In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal — compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.
Determine the array Ivan will obtain after performing all the changes.
The first line contains an single integer n (2 ≤ n ≤ 200 000) — the number of elements in Ivan's array.
The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n) — the description of Ivan's array.
In the first line print q — the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.
4 3 2 2 3
2 1 2 4 3
6 4 5 6 3 2 1
0 4 5 6 3 2 1
10 6 8 4 6 7 1 6 3 4 5
3 2 8 4 6 7 1 9 3 10 5
In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers — this permutation is lexicographically minimal among all suitable.
In the second example Ivan does not need to change anything because his array already is a permutation.
题目大意:
给出一个长度为N的序列,让我们将其置换为一个全排列,问我们最少需要进行多少次操作 ,同时问最少操作的情况下,最小字典序的结果。
思路:
我们O(n)枚举全排列,对于当前数i,分成三种情况去讨论:
①如果这个数只在原序列中出现过一次,那么不动。
②如果这个数在现在的序列中还具有多个,那么对应保留这个数第一个出现的位子。其余的位子预备留作之后放置其他数所用,标记这个数其他位子上的数变成INF即可。
③如果这个数在现在的序列中还没出现过,那么对应在现在的序列中找到最左端的位子,使得这个位子pos,a【pos】>当前数。
过程用线段树查询区间最大值,因为区间最大值有单调性,所以③这种情况的pos我们可以通过二分来求。
(很麻烦的写法= =)
Ac代码:
#include
#include
#include
#include
using namespace std;
vectormp[250000];
int a[250000];
int vis[250000];
int back[250000];
int last[250000];
/*******************************/
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
int tree[200050*4];
void pushup(int rt)
{
tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
int Query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return tree[rt];
}
else
{
int m=(l+r)>>1;
int ans=0;
if(L<=m)
{
ans=max(Query(L,R,lson),ans);
}
if(m>1 ;
build(lson) ;
build(rson) ;
pushup(rt) ;
}
}
void update(int p,int c,int l,int r,int rt)//p阵营c数据.
{
if(l==r)
{
tree[rt]=c;
}
else
{
int m=(l+r)>>1;
if(p<=m) update(p,c,lson);
else update(p,c,rson);
pushup(rt);
}
}
/*******************************/
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)mp[i].clear();
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
vis[a[i]]++;
mp[a[i]].push_back(i);
}
int cnt=0;
build(1,n,1);
for(int i=1;i<=n;i++)
{
if(vis[a[i]]>1)
{
update(i,a[i],1,n,1);
}
}
for(int i=1;i<=n;i++)
{
if(vis[i]==1)continue;
else if(vis[i]>0)
{
int pos=0;
for(int j=0;j=0)
{
int mid=(l+r)/2;
int temp=Query(1,mid,1,n,1);
if(temp>i)
{
pos=mid;
r=mid-1;
}
else l=mid+1;
}
vis[a[pos]]--;
if(vis[a[pos]]==1)
{
for(int j=0;j