Advertisement
ivnikkk

Untitled

Nov 4th, 2022
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define debug(tl) cerr<<#tl<<' '<<tl<<'\n';
  3. #include "bits/stdc++.h"
  4. using namespace std;
  5. #define all(d) d.begin(), d.end()
  6. typedef long long ll;
  7. typedef pair<ll, ll> pll;
  8. typedef pair<int, int> pii;
  9. typedef long double ld;
  10. signed main() {
  11. #ifdef _DEBUG
  12.     freopen("input.txt", "r", stdin);
  13.     freopen("output.txt", "w", stdout);
  14. #endif
  15.     ios_base::sync_with_stdio(false);
  16.     cin.tie(nullptr);
  17.     cout.tie(nullptr);
  18.     struct event {
  19.         int x, type, ind;
  20.         event(int _x, int _type, int _ind) {
  21.             type = _type, x = _x, ind = _ind;
  22.         }
  23.         bool operator<(const event& other) {
  24.             return (x < other.x || (x == other.x && type > other.type));
  25.         }
  26.     };
  27.     int n, m;
  28.     cin >> n >> m;
  29.     vector<event> line;
  30.     for (int i = 0; i < n; i++) {
  31.         int x;
  32.         cin >> x;
  33.         line.push_back(event(x, 0, i));
  34.     }
  35.     vector<int> r(m);
  36.     for (int i = 0; i < m; i++) {
  37.         int L, R;
  38.         cin >> L >> R;
  39.         r[i] = R;
  40.         line.push_back(event(L, 1, i));
  41.         line.push_back(event(R, -1, i));
  42.     }
  43.     sort(all(line));
  44.     multiset<pii> unused;
  45.     vector<vector<int>> us(n);
  46.     for (const event& i : line) {
  47.         if (i.type == 0) {
  48.             auto it = unused.begin();
  49.             while (true) {
  50.                 if (it == unused.end() || (int)us[i.ind].size() == 3) {
  51.                     break;
  52.                 }
  53.                 us[i.ind].push_back((*it).second);
  54.                 it++;
  55.             }
  56.         }
  57.         if (i.type == 1) {
  58.             unused.insert({ r[i.ind],i.ind });
  59.         }
  60.         if (i.type == -1) {
  61.             unused.erase({ r[i.ind],i.ind });
  62.         }
  63.     }
  64.     vector<vector<bool>> dp(3, vector<bool>(n, false));
  65.     vector<vector<int>> kid(3, vector<int>(n, -1));
  66.     for (int i = 0; i < (int)us[0].size(); i++)dp[i][0] = true, kid;
  67.  
  68.     for (int i = 1; i < n; i++) {
  69.         for (int j = 0; j < (int)us[i].size(); j++) {
  70.             for (int k = 0; k < (int)us[i - 1].size(); k++) {
  71.                 dp[j][i] = (dp[j][i] || (dp[k][i - 1] && (us[i - 1][k] != us[i][j])));
  72.                 if (dp[k][i - 1] && (us[i - 1][k] != us[i][j])) {
  73.                     kid[j][i] = k;
  74.                 }
  75.             }
  76.         }
  77.     }
  78.     bool ok = false;
  79.     int f = 0;
  80.     for (int i = 0; i < (int)us.back().size(); i++) {
  81.         ok = (ok || dp[i][n - 1]);
  82.         if (dp[i][n - 1])f = i;
  83.     }
  84.     if (ok) {
  85.         cout << "Yes\n";
  86.         int i = n - 1;
  87.         vector<int> ans;
  88.         while (1) {
  89.             if (i < 0) {
  90.                 break;
  91.             }
  92.             ans.push_back(us[i][f]);
  93.             f = kid[f][i], i--;
  94.         }
  95.         reverse(all(ans));
  96.         for (int i = 0; i < n; i++) {
  97.             cout << ans[i] + 1 << ' ';
  98.         }
  99.     }
  100.     else {
  101.         cout << "No\n";
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement