【-30】agc 001F - Wide Swap[拓扑排序]

Problem Statement


You are given a permutation $P_1…P$$N$ of the set {$1, 2, …, N.$}

You can apply the following operation to this permutation, any number of times (possibly zero):

  • Choose two indices $i$,$j$($1 \le i \le j \le N$), such that$K$$\le$ $j−i $ and |$P$$i$−$P$$j$|=1. Then, swap the values of $P$$i$ and $P$$j$.

Among all permutations that can be obtained by applying this operation to the given permutation, find the lexicographically smallest one.

Constraints


  • $2 \le N \le 500,000$
  • $1\le K \le N−1 $
  • $P$ is a permutation of the set {$1$,$ 2$, …, $N$}.

Input


The input is given from Standard Input in the following format:

1
2
N K
P1 P2 … PN

Output


Print the lexicographically smallest permutation that can be obtained.

Sample Input 1

1
2
4 2
4 2 3 1

Sample Output 1

1
2
3
4
2
1
4
3

One possible way to obtain the lexicographically smallest permutation is shown below:

  • 4231
  • 4132
  • 3142
  • 2143

    Sample Input 2

    1
    2
    5 1
    5 4 3 2 1

Sample Output 2

1
2
3
4
5
1
2
3
4
5

Sample Input 3

1
2
8 3
4 5 7 8 3 1 2 6

Sample Output 3

1
2
3
4
5
6
7
8
1
2
6
7
5
3
4
8

题解


读入的 $P_i$ 表示第 $i$个位置上的数为 $P_i$

然后设 $Q_i$表示$i$这个数在排列中的位置为$Q_i$

要使交换后 $P$ 数组字典序最小,就是使交换后的 $Q$ 数组字典序最小.(因为使尽量小的数在尽量靠前的地方,就可以使整个排列尽量小)

求出最小字典序后,在$i$前面的数只有比$i$ 小和比 $i$大 $|i-j|>=K$

所以对于$|i-j|<K$的情况,$P_i$和$P_j$的相对位置不会变,所以连边跑拓扑排序,求最小的拓扑序

每个点只需要连向左右最小的满足条件的数即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

#include<bits/stdc++.h>
using namespace std;
int read()
{
char s;
int k=0,base=1;
while((s=getchar())!='-'&&s!=EOF&&!(s>='0'&&s<='9'));
if(s==EOF)exit(0);
if(s=='-')base=-1,s=getchar();
while(s>='0'&&s<='9')
{
k=k*10+(s-'0');
s=getchar();
}
return k*base;
}
void write(int x)
{
if(x<0)
{
putchar('-');
write(-x);
}
else
{
if(x/10)write(x/10);
putchar(x%10+'0');
}
}
const int maxn=500010;
const int maxm=1010010;
int n,k,T;
int rd[maxn];
int p[maxn],q[maxn];
int ans[maxn];
int id,to[maxm<<1],ne[maxm<<1],po[maxn];
inline void add(int x,int y)
{
id++;
to[id]=y;ne[id]=po[x];
po[x]=id;
rd[y]++;
}
set<int> s;
set<int>::iterator it;
priority_queue<int,vector<int>,greater<int> > Q;
int main()
{
freopen("permutation.in","r",stdin);
freopen("permutation.out","w",stdout);
n=read();k=read();
for (int i=1;i<=n;i++) q[p[i]=read()]=i;
for (int i=1;i<=n;i++)//前面的
{
if (i>k) s.erase(p[i-k]);
it=s.insert(p[i]).first;//|i-j|<k,Pi<Pj,i和j连边
if (++it!=s.end()) add(i,q[*it]);
}
for (int i=n;i>=1;i--)//后面的
{
if (i+k<=n) s.erase(p[i+k]);
it=s.insert(p[i]).first;
if (++it!=s.end()) add(i,q[*it]);
}
for (int i=1;i<=n;i++)//入度为0的点入队
{
if (rd[i]==0) Q.push(i);
}
for (int i=1;i<=n;i++)
{
T=Q.top();Q.pop();
ans[T]=i;
for (int j=po[T];j;j=ne[j])
{
rd[to[j]]--;
if (rd[to[j]]==0) Q.push(to[j]);
}
}
for (int i=1;i<=n;i++)
{
printf("%d\n",ans[i]);
}
return 0;
}