AGC001F

link

令$a_i$为$p$中$i$的出现位置$j$,也就是$a_{p_j}=j$,那么原操作就可以转化为若$|a_i-a_{i+1}|\ge K$可以交换$a_i,a_{i+1}$。

原问题($p$字典序最小)就可以转化为$a$中1位置尽量小,同时2位置尽量小$\dots$

实际上这就相当于$a$翻转后字典序最大。

如何理解呢?大概的感性理解下:

令$a_n$尽量大,那么$<a_n$的数出现位置都会往前。

或者说要使得一个序列字典序尽量大,首先最小的必须尽量靠后,然后是第二小的….

由于$|a_i-a_{i+1}|\ge K$才可以交换,那么若$|a_i-a_j|\lt K$,它们相对顺序就不会变了 。

于是可以连边然后拓扑排序。

但是$O(n^2)$的边数有点大。

事实上,$i$只需要向最大满足$j\lt i,0\lt a_j-a_i\lt k$的$j$和最大满足$j\lt i,0\lt a_i-a_j\lt k$

的$j$连边就好了。

其它满足$j\lt i,|a_j-a_j|\lt k$的$j$都会被这两个$j$中至少一个直接或间接的连边,那么$i$也间接向$j$连了边。

时间复杂度$O(n\log n)$。

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Author: CNYALI_LK
LANG: C++
PROG: f.cpp
Mail: cnyalilk@vip.qq.com
*/
#include<bits/stdc++.h>
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const signed inf=0x3f3f3f3f;
const double eps=1e-8;
const double pi=acos(-1.0);
template<class T>int chkmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>int chkmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T>T mmin(T a,T b){return a<b?a:b;}
template<class T>T mmax(T a,T b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
#define min mmin
#define max mmax
#define abs aabs
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
// getchar
#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
// print the remaining part
inline void flush () {
fwrite (obuf, 1, oS - obuf, stdout);
oS = obuf;
}
// putchar
inline void putc (char x) {
*oS ++ = x;
if (oS == oT) flush ();
}
// input a signed integer
inline void read (signed &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
}

inline void read (long long &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
}
inline void read (char &x) {
x=gc();
}
inline void read(char *x){
while((*x=gc())=='\n' || *x==' '||*x=='\r');
while(!(*x=='\n'||*x==' '||*x=='\r'))*(++x)=gc();
}
template<typename A,typename ...B>
inline void read(A &x,B &...y){
read(x);read(y...);
}
// print a signed integer
inline void write (signed x) {
if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10;
while (qr) putc (qu[qr --]);
}

inline void write (long long x) {
if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10;
while (qr) putc (qu[qr --]);
}
inline void write (char x) {
putc(x);
}
inline void write(const char *x){
while(*x){putc(*x);++x;}
}
inline void write(char *x){
while(*x){putc(*x);++x;}
}
template<typename A,typename ...B>
inline void write(A x,B ...y){
write(x);write(y...);
}
//no need to call flush at the end manually!
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
int a[500005],p[500005];
struct smt{
int ls,rs,mx;
smt *l,*r;
smt(int la,int ra){
ls=la;rs=ra;
mx=0;
if(la==ra){
l=r=0;
}
else{
int mid=(ls+rs)>>1;
l=new smt(ls,mid);
r=new smt(mid+1,rs);
}
}
int query(int la,int ra){
if(la<=ls && rs<=ra)return mx;
int mx=0;
if(la<=l->rs)chkmax(mx,l->query(la,ra));
if(r->ls<=ra)chkmax(mx,r->query(la,ra));
return mx;
}
void upd(int x,int y){
mx=y;
if(ls==rs)return;
if(x<=l->rs)l->upd(x,y);
else r->upd(x,y);
}
};
smt *rt;
int ind[500005],c[500005];
vector<int> to[500005];
priority_queue<pii> pq;
int main(){
#ifdef cnyali_lk
freopen("f.in","r",stdin);
freopen("f.out","w",stdout);
#endif
int n,k,x;
read(n,k);
for(int i=1;i<=n;++i){
read(a[i]);
p[a[i]]=i;
}
rt=new smt(1,n);
for(int i=1;i<=n;++i){
to[i].push_back(x=rt->query(p[i]-k+1,p[i]));++ind[x];
to[i].push_back(x=rt->query(p[i],p[i]+k-1));++ind[x];
rt->upd(p[i],i);
}
for(int i=1;i<=n;++i)if(!ind[i])pq.push(make_pair(p[i],i));
int t=n+1;
while(!pq.empty()){
c[--t]=pq.top().x;
int x=pq.top().y;
pq.pop();
for(auto i:to[x]){
if(!--ind[i]){
pq.push(make_pair(p[i],i));
}
}
}
for(int i=1;i<=n;++i)a[i]=i;
sort(a+1,a+n+1,cmp_a<c>);
for(int i=1;i<=n;++i)printf("%d\n",a[i]);
return 0;
}

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×