Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- #include <set>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <cmath>
- #include <cstdio>
- #include <iomanip>
- #include <fstream>
- #include <cassert>
- #include <cstring>
- #include <unordered_set>
- #include <unordered_map>
- #include <numeric>
- #include <ctime>
- #include <bitset>
- #include <complex>
- #include <random>
- using namespace std;
- bool check(int n, int k, vector<int> in, vector<int> pans) {
- assert((int)in.size() == n);
- assert((int)pans.size() == k);
- assert(is_sorted(pans.begin(), pans.end()));
- auto relax = [&](int &mn, int &wr, int i, int j) {
- if (j < 0 || j >= k) {
- return;
- }
- if (mn > abs(i - pans[j])) {
- mn = abs(i - pans[j]);
- wr = j;
- }
- };
- int pnt = -1;
- for (int i = 0; i < n; i++) {
- if (in[i] != -1) {
- while (pnt + 1 < k && pans[pnt + 1] < i) {
- pnt++;
- }
- int wr = -1;
- int mn = n + 1;
- relax(mn, wr, i, pnt);
- relax(mn, wr, i, pnt + 1);
- if (in[i] != wr) {
- return false;
- }
- }
- }
- return true;
- }
- struct St {
- vector<int> a;
- vector<pair<int, int>> wr;
- int get_a(int x) {
- return a[x];
- }
- pair<int, int> get_wr(int x) {
- return wr[x];
- }
- void set(int l, int r, pair<int, int> x) {
- for (int i = l; i <= r; i++) {
- a[i] = 1;
- wr[i] = x;
- }
- }
- void set_a(int i) {
- a[i] = 1;
- }
- St(int n) {
- a.resize(n, 0);
- wr.resize(n, {-1, -1});
- }
- St(){}
- };
- vector<int> solve(int n, int k, vector<int> in) {
- const int K = 20;
- vector<int> l(n);
- vector<int> r(n);
- l[0] = in[0];
- for (int i = 1; i < n; i++) {
- if (in[i] != -1) {
- l[i] = in[i];
- } else {
- l[i] = l[i - 1];
- }
- }
- r[n - 1] = in[n - 1];
- for (int i = n - 2; i >= 0; i--) {
- if (in[i] != -1) {
- r[i] = in[i];
- } else {
- r[i] = r[i + 1];
- }
- }
- auto find = [&](int i, int wr) {
- if (wr == 0) {
- return l[i];
- } else {
- return r[i];
- }
- };
- vector<int> minp(k, -1);
- vector<int> maxp(k, -1);
- for (int i = 0; i < n; i++) {
- if (in[i] != -1) {
- if (minp[in[i]] == -1) {
- minp[in[i]] = i;
- }
- maxp[in[i]] = i;
- }
- }
- auto check = [&](int i, int j, int k, int f, int s, int &ok) {
- if (k < i || k > j) {
- return;
- }
- if (abs(i - k) <= abs(j - k) && in[k] != f) {
- ok = 0;
- }
- if (abs(i - k) > abs(j - k) && in[k] != s) {
- ok = 0;
- }
- };
- auto can_relax = [&](int i, int j, int iw, int jw) {
- int f = find(i, iw);
- int s = find(j, jw);
- if (f == -1 || s == -1 || f + 1 != s) {
- return 0;
- }
- int ok = 1;
- check(i, j, maxp[f], f, s, ok);
- check(i, j, minp[s], f, s, ok);
- return ok;
- };
- vector<St> dp(2, St(n));
- {
- int fs = -1;
- int ss = -1;
- for (int i = 0; i < n; i++) {
- if (in[i] == 0 && fs == -1) {
- fs = i;
- }
- if (in[i] == 1 && ss == -1) {
- ss = i;
- }
- }
- for (int i = 0; i <= fs; i++) {
- dp[1].set_a(i);
- }
- for (int i = fs; i < ss; i++) {
- dp[0].set_a(i);
- }
- }
- for (int i = 0; i < n; i++) {
- for (int f = 0; f < 2; f++) {
- if (!dp[f].get_a(i)) {
- continue;
- }
- for (int s = 0; s < 2; s++) {
- if (find(i, f) + 1 == k) {
- continue;
- }
- int omn = i + 1;
- if (i <= maxp[find(i, f)]) {
- int delta = maxp[find(i, f)] - i;
- omn = max(omn, i + 2 * delta);
- }
- if (!s) {
- omn = max(omn, minp[find(i, f) + 1]);
- }
- if (omn >= n || !can_relax(i, omn, f, s)) {
- continue;
- }
- int omx = omn;
- for (int j = K; j >= 0; j--) {
- if ((omx + (1 << j)) < n && can_relax(i, omx + (1 << j), f, s)) {
- omx += (1 << j);
- }
- }
- dp[s].set(omn, omx, {i, f});
- }
- }
- }
- vector<int> ans;
- int toi = -1;
- int toj = -1;
- for (int i = n - 1; i >= 0; i--) {
- for (int j = 0; j < 2; j++) {
- if (dp[j].get_a(i) && find(i, j) == k - 1) {
- toi = i;
- toj = j;
- }
- }
- }
- assert(toi != -1);
- while (toi != -1) {
- ans.push_back(toi);
- tie(toi, toj) = dp[toj].get_wr(toi);
- }
- reverse(ans.begin(), ans.end());
- return ans;
- }
- void read() {
- int n, k;
- cin >> n >> k;
- vector<int> a(n);
- for (auto &t : a) {
- cin >> t;
- t--;
- }
- auto rs = solve(n, k, a);
- cout << "res:" << endl;
- for (auto t : rs) {
- cout << t + 1 << ' ';
- }
- cout << endl;
- if (check(n, k, a, rs)) {
- cout << "OK\n";
- } else {
- cout << "WA\n";
- }
- }
- void gen(int n, int k) {
- set<int> sel;
- while ((int)sel.size() < k) {
- sel.insert(rand() % n);
- }
- vector<int> ps;
- for (auto t : sel) {
- ps.push_back(t);
- }
- vector<int> out(n, -1);
- vector<int> cnt(k);
- for (int i = 0; i < n; i++) {
- int mn = n + 1;
- int wr = -1;
- for (int j = 0; j < k; j++) {
- if (mn > abs(i - ps[j])) {
- mn = abs(i - ps[j]);
- wr = j;
- }
- }
- out[i] = wr;
- cnt[wr]++;
- }
- int sk = n / 2;
- while (sk--) {
- int i = rand() % n;
- if (out[i] != -1 && cnt[out[i]] > 1) {
- cnt[out[i]]--;
- out[i] = -1;
- }
- }
- {
- cout << "Case:" << endl;
- cout << n << ' ' << k << endl;
- for (auto t : out) {
- cout << t + 1 << ' ';
- }
- cout << endl;
- auto rs = solve(n, k, out);
- if (check(n, k, out, rs)) {
- cout << "OK\n";
- } else {
- cout << "WA\n";
- exit(0);
- }
- cout << endl;
- }
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- // read();
- int n, k;
- cin >> n >> k;
- while (true) {
- gen(n, k);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement