Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <cmath>
- #include <cassert>
- #include <algorithm>
- #include <string>
- #include <vector>
- #include <deque>
- #include <queue>
- #include <map>
- #include <set>
- using namespace std;
- #define eprintf(...) fprintf(stderr, __VA_ARGS__)
- #define pb push_back
- #define mp make_pair
- #define sz(x) ((int)(x).size())
- typedef long long ll;
- typedef vector<ll> vll;
- typedef vector<int> vi;
- typedef vector<vi> vvi;
- typedef vector<bool> vb;
- typedef vector<vb> vvb;
- typedef pair<int, int> pii;
- const int MAXN = 1e5 + 1e3;
- const int MAXM = 1e6 + 1e3;
- int n, d, m;
- int firs[MAXN];
- int nes[MAXM];
- int ids[MAXN];
- int nes2[MAXM];
- bool check(int maxw, bool write = false) {
- if (write) {
- memset(firs, -1, sizeof firs);
- }
- int ptr = 0, csz = 0;
- for (int cst = 0; cst < n; cst++)
- for (int i = ids[cst]; i >= 0; i = nes2[i]) {
- while (ptr < cst) { ptr++; csz = 0; }
- if (csz >= maxw) { ptr++; csz = 0; }
- if (ptr >= n || ptr > cst + d) return false;
- csz++;
- if (write) {
- nes[i] = firs[ptr];
- firs[ptr] = i;
- }
- }
- return true;
- }
- int main() {
- #ifdef DEBUG
- freopen("std.in", "r", stdin);
- freopen("std.out", "w", stdout);
- #endif
- while (scanf("%d%d%d", &n, &d, &m) >= 1) {
- memset(ids, -1, sizeof ids);
- for (int i = 0; i < m; i++) {
- int x;
- scanf("%d", &x), x--;
- nes2[i] = ids[x];
- ids[x] = i;
- }
- int L = 0, R = m;
- assert(check(R));
- while (L + 1 < R) {
- int M = (L + R) / 2;
- if (check(M)) R = M;
- else L = M;
- }
- printf("%d\n", R);
- check(R, true);
- for (int i = 0; i < n; i++) {
- for (int i2 = firs[i]; i2 >= 0; i2 = nes[i2])
- printf("%d ", i2 + 1);
- printf("0\n");
- }
- #ifndef DEBUG
- break;
- #endif
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment