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
| void work() { read(n, m, k); int em = n; opt.clear(); while (!add.empty()) add.pop(); for (int i = 1; i <= k; ++ i) id[i] = 0, ins[i] = dn[i] = false; for (int i = 1; i <= n; ++ i) a[i].clear(); for (int i = 1; i < n; ++ i) add.push(i), add.push(i); for (int i = 1; i <= m; ++ i) read(c[i]); auto insert = [&](int x) { if (ins[x]) { if (dn[x]) { opt.push_back({1, em}); opt.push_back({2, id[x], em}); a[id[x]].pop_front(); ins[x] = dn[x] = false; if (a[id[x]].size()) dn[a[id[x]].front()] = true; } else opt.push_back({1, id[x]}), a[id[x]].pop_back(), ins[x] = dn[x] = false; add.push(id[x]), id[x] = 0; return; } if (add.size()) { int t = add.front(); add.pop(); dn[x] = !a[id[x] = t].size(), ins[x] = true, a[t].push_back(x); opt.push_back({1, t}); return; } }; for (int i = 1, x; i <= m; ++ i) {
if (ins[x = c[i]] || add.size()) { insert(x); continue; } int j = i + 1, flag = 0, t, uc; while (j <= m && !dn[c[j]] && c[j] != x) ++ j; if (c[j] == x) { opt.push_back({1, em}); for (int k = i + 1; k < j; ++ k) insert(c[k]); opt.push_back({1, em}); i = j; continue; } t = id[c[j]], uc = a[t].back(); for (int k = i; k < j; ++ k) flag ^= (c[k] == uc); if (flag) { opt.push_back({1, em}); int ls = -1; for (int k = i; k < j; ++ k) if (c[k] == uc) ls = k; for (int k = i + 1; k < j; ++ k) if (c[k] == uc) { if (k == ls) opt.push_back({1, t}); else opt.push_back({1, em}); } else insert(c[k]); opt.push_back({1, t}); ins[x] = dn[x] = true, id[x] = em; ins[c[j]] = ins[uc] = dn[c[j]] = dn[uc] = false, id[c[j]] = id[uc] = 0; add.push(em), a[t].clear(), a[em].push_back(x); em = t; i = j; } else { opt.push_back({1, t}); for (int k = i + 1; k < j; ++ k) if (c[k] == uc) opt.push_back({1, em}); else insert(c[k]); opt.push_back({1, em}), opt.push_back({2, t, em}); dn[x] = false, ins[x] = true, dn[uc] = true, id[uc] = id[x] = t; a[t].pop_front(), a[t].push_back(x); ins[c[j]] = false, id[c[j]] = 0; i = j; } } write(opt.size(), '\n'); for (auto p : opt) if (p.op == 1) write('1', ' ', p.u, '\n'); else write('2', ' ', p.u, ' ', p.v, '\n'); }
|