rembocoder

Untitled

Apr 18th, 2023
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define int int64_t
  6.  
  7. const int inf = 2e18;
  8. const int mod = 1e9 + 7;
  9.  
  10. bool relax(int& a, int b) {
  11.     if (a < b) {
  12.         a = b;
  13.         return true;
  14.     }
  15.     return false;
  16. }
  17.  
  18. int32_t main() {
  19.     //freopen("show.in", "r", stdin);
  20.     //freopen("show.out", "w", stdout);
  21.     ios_base::sync_with_stdio(0);
  22.     cin.tie(0); cout.tie(0);
  23.     int n;
  24.     cin >> n;
  25.     vector<pair<pair<int, int>, int>> segs(n); // r, l, ind
  26.     for (int i = 0; i < n; i++) {
  27.         int a, b;
  28.         cin >> a >> b;
  29.         b += a;
  30.         segs[i] = {{b, a}, i};
  31.     }
  32.     segs.push_back({{-1, -1}, -1});
  33.     n++;
  34.     sort(segs.begin(), segs.end());
  35.     vector<vector<vector<int>>> dp(n, vector<vector<int>>(n, vector<int>(2, -2e18)));
  36.     vector<vector<vector<pair<int, int>>>> p(n, vector<vector<pair<int, int>>>(n, vector<pair<int, int>>(2, {-1, -1})));
  37.     dp[0][0][0] = 0;
  38.     for (int i = 0; i + 1 < n; i++) {
  39.         for (int n0 = 0; n0 <= i; n0++) {
  40.             for (int t = 0; t < 2; t++) {
  41.                 int cnt = 0;
  42.                 for (int j = i + 1; j < n; j++) {
  43.                     cnt += segs[j].first.second >= segs[i].first.first;
  44.                     if (relax(dp[j][n0 + (t ? cnt : 0)][1 - t], dp[i][n0][t] + (t ? 0 : cnt))) {
  45.                         p[j][n0 + (t ? cnt : 0)][1 - t] = {i, n0};
  46.                     }
  47.                 }
  48.             }
  49.         }
  50.     }
  51.     int ans_val = -2e18;
  52.     int ans_n0 = -1;
  53.     int ans_t = -1;
  54.     for (int n0 = 0; n0 < n; n0++) {
  55.         for (int t = 0; t < 2; t++) {
  56.             if (relax(ans_val, min(dp[n - 1][n0][t], n0))) {
  57.                 ans_n0 = n0;
  58.                 ans_t = t;
  59.             }
  60.         }
  61.     }
  62.     int ans_i = n - 1;
  63.     vector<int> ans(n - 1);
  64.     while (ans_i > 0) {
  65.         int p_i = p[ans_i][ans_n0][ans_t].first;
  66.         int p_n0 = p[ans_i][ans_n0][ans_t].second;
  67.         for (int j = p_i + 1; j <= ans_i; j++) {
  68.             if (segs[j].first.second >= segs[p_i].first.first) {
  69.                 ans[segs[j].second] = ans_t + 1;
  70.             }
  71.         }
  72.         ans_i = p_i;
  73.         ans_n0 = p_n0;
  74.         ans_t = 1 - ans_t;
  75.     }
  76.     for (int x: ans) {
  77.         cout << x << ' ';
  78.     }
  79.     cout << '\n';
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment