#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define sz(x) x.size
#define pb push_back
#define all(x) x.begin(),x.end()
// my vector
struct vec{
int a[20100], size;
vec() { memset(a,0,sizeof a); }
void resize(int n) { size = n; }
void assign(int n, int val) {
size = n;
for (int i = 0; i < n; i++)
a[i] = val;
}
void push_back(int x) {
a[size++] = x;
}
void clear() { size = 0; }
int* begin() { return a; }
int* end() { return a+size; }
int& operator[] (int i) { return a[i]; }
};
vec a, b, chain, u, allshifts, save, ptr, st, oldptr;
int N, M, W, Q, shifts, v, cheaton;
// adds all chain of shifts to chain
void find_chain(int v) {
while (true) {
u[v] = 1;
st.pb(v);
chain.pb(v);
save[v] = a[v];
int val = a[v];
int &to = ptr[val];
while (to < N && (u[to] || a[to] == val) && b[to] == val)
to++;
if (to == N || b[to] != val)
return;
v = to;
}
}
int main() {
// input
scanf("%d%d%d", &N, &M, &W);
a.resize(N);
ptr.resize(M+1);
v = N-1;
for (int i = 0; i < N; i++)
scanf("%d", &a[i]);
// b = sorted(a)
b = save = a;
stable_sort(all(b));
// ptr[i] points at first
// position of i in sorted array b
// O(MlgN)
for (int i = 1; i <= M; i++)
ptr[i] = lower_bound(all(b), i) - b.begin();
Q = (N+W-2) / (W-1);
// for every round out of Q
printf("%d\n", Q);
// next cycle works O(N/W) time
while (Q--) {
// if there is no need to
// do anything, just output zero
if (cheaton) {
puts("0");
continue;
}
shifts = 0;
allshifts.clear();
for (int i = 0; i <= M; i++)
oldptr[i] = ptr[i];
// until number of shifts < W
while (shifts < W) { // O(W)
chain.clear();
// find any number standing wrong
while (v >= 0 && (u[v] || a[v] == b[v]))
v--;
if (v == -1)
break;
find_chain(v);
// add chain until we fillup shifts
chain.resize(min(sz(chain), W-shifts));
for (int i = 0; i < sz(chain); i++) {
allshifts.pb(chain[i]);
allshifts.pb(chain[(i+1)%sz(chain)]);
}
shifts += sz(chain);
}
// if there are no changes,
// no need to output anything but zeroes
if (!shifts)
cheaton = 1;
// output all changes
printf("%d ", shifts);
for (int i = 0; i < sz(allshifts); i++)
printf("%d ", allshifts[i]+1);
printf("\n");
// apply all chains now
for (int i = 0; i < sz(allshifts); i += 2)
a[allshifts[i+1]] = save[allshifts[i]];
// move all ptrs as far as possible
for (int i = 1; i <= M; i++) {
ptr[i] = oldptr[i];
int &o = ptr[i];
while (o < N && b[o] == i && a[o] == i)
o++;
}
// clear used array
for (int i = 0; i < sz(st); i++)
u[st[i]] = 0;
st.clear();
}
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxjc3RyaW5nPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsgCgojZGVmaW5lIHN6KHgpIHguc2l6ZQojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIGFsbCh4KSB4LmJlZ2luKCkseC5lbmQoKQoKLy8gbXkgdmVjdG9yCnN0cnVjdCB2ZWN7CglpbnQgYVsyMDEwMF0sIHNpemU7Cgl2ZWMoKSB7IG1lbXNldChhLDAsc2l6ZW9mIGEpOyB9Cgl2b2lkIHJlc2l6ZShpbnQgbikgeyBzaXplID0gbjsgfQoJdm9pZCBhc3NpZ24oaW50IG4sIGludCB2YWwpIHsKCQlzaXplID0gbjsKCQlmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykKCQkJYVtpXSA9IHZhbDsKCX0KCXZvaWQgcHVzaF9iYWNrKGludCB4KSB7CgkJYVtzaXplKytdID0geDsKCX0KCXZvaWQgY2xlYXIoKSB7IHNpemUgPSAwOyB9CglpbnQqIGJlZ2luKCkgeyByZXR1cm4gYTsgfQoJaW50KiBlbmQoKSB7IHJldHVybiBhK3NpemU7IH0KCWludCYgb3BlcmF0b3JbXSAoaW50IGkpIHsgcmV0dXJuIGFbaV07IH0KfTsKCnZlYyBhLCBiLCBjaGFpbiwgdSwgYWxsc2hpZnRzLCBzYXZlLCBwdHIs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