pb_jiang

CF ROUND1026 C

May 25th, 2025
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.57 KB | None | 0 0
  1. // Problem: C. Racing
  2. // Contest: Codeforces - Codeforces Round 1026 (Div. 2)
  3. // URL: https://codeforces.com/contest/2110/problem/C
  4. // Memory Limit: 256 MB
  5. // Time Limit: 2000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8.  
  9. #include <assert.h>
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. #ifndef __DEBUG__
  13. #define dbg(...) 42
  14. #endif
  15. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  16.  
  17. namespace rngs = std::ranges;
  18. using ll = long long;
  19. using a2l = array<ll, 2>;
  20. using pll = pair<ll, ll>;
  21. using vl = vector<ll>;
  22.  
  23. void solve_bili()
  24. {
  25.     ll n;
  26.     cin >> n;
  27.     vl ds(n);
  28.     for (auto &x : ds)
  29.         cin >> x;
  30.     vector<a2l> rs(n);
  31.     for (auto &x : rs)
  32.         cin >> x[0] >> x[1];
  33.  
  34.     vector<a2l> opt(n);
  35.     for (ll i = 0, l = 0, r = 0; i < n; ++i) {
  36.         if (ds[i] != -1)
  37.             l += ds[i], r += ds[i];
  38.         else
  39.             r += 1;
  40.         l = max(l, rs[i][0]), r = min(r, rs[i][1]);
  41.         if (l > r) {
  42.             cout << -1 << '\n';
  43.             return;
  44.         }
  45.         opt[i] = {l, r};
  46.     }
  47.  
  48.     ll val = opt.back()[0];
  49.     for (ll i = n - 1; i >= 0; --i) {
  50.         if (ds[i] != -1)
  51.             val -= ds[i];
  52.         else {
  53.             if (val >= (i > 0 ? opt[i - 1][0] : 0) && val <= (i > 0 ? opt[i - 1][1] : 0))
  54.                 ds[i] = 0;
  55.             else
  56.                 ds[i] = 1;
  57.             val -= ds[i];
  58.         }
  59.     }
  60.     for (auto x : ds)
  61.         cout << x << ' ';
  62.     cout << '\n';
  63. }
  64.  
  65. void solve_ac()
  66. {
  67.     ll n;
  68.     cin >> n;
  69.     vl ds(n);
  70.     for (auto &x : ds)
  71.         cin >> x;
  72.     vector<a2l> rs(n);
  73.     for (auto &x : rs)
  74.         cin >> x[0] >> x[1];
  75.  
  76.     vl lmax(n + 1), rmin(n + 1, LLONG_MAX / 2);
  77.     for (ll i = n; i > 0; --i) {
  78.         lmax[i - 1] = max(lmax[i], rs[i - 1][0]);
  79.         rmin[i - 1] = min(rmin[i], rs[i - 1][1]);
  80.     }
  81.  
  82.     bool valid = true;
  83.     for (ll i = 0, h = 0; valid && i < n; ++i) {
  84.         if (ds[i] != -1)
  85.             h += ds[i];
  86.         else {
  87.             ds[i] = 1;
  88.             h += 1;
  89.             if (rmin[i] == h)
  90.                 ds[i] = 0, h -= 1;
  91.         }
  92.  
  93.         if (!(rs[i][0] <= h && h <= rs[i][1]))
  94.             valid = false;
  95.     }
  96.     if (!valid) {
  97.         cout << -1 << '\n';
  98.         return;
  99.     }
  100.     for (auto x : ds)
  101.         cout << x << ' ';
  102.     cout << '\n';
  103. }
  104.  
  105. void solve_edtorial()
  106. {
  107.     ll n;
  108.     cin >> n;
  109.     vl ds(n);
  110.     for (auto &x : ds)
  111.         cin >> x;
  112.     vector<a2l> rs(n);
  113.     for (auto &x : rs)
  114.         cin >> x[0] >> x[1];
  115.  
  116.     vl pos;
  117.     for (ll i = 0, h = 0; i < n; ++i) {
  118.         if (ds[i] == -1) {
  119.             pos.push_back(i);
  120.         } else {
  121.             h += ds[i];
  122.         }
  123.  
  124.         if (h < rs[i][0]) {
  125.             ll d = rs[i][0] - h;
  126.             if (pos.size() < d) {
  127.                 cout << -1 << '\n';
  128.                 return;
  129.             }
  130.             while (d--) {
  131.                 ds[pos.back()] = 1;
  132.                 h += 1;
  133.                 pos.pop_back();
  134.             }
  135.         }
  136.         if (h + pos.size() > rs[i][1]) {
  137.             ll d = h + pos.size() - rs[i][1];
  138.             if (d > pos.size()) {
  139.                 cout << -1 << '\n';
  140.                 return;
  141.             }
  142.             while (d--) {
  143.                 ds[pos.back()] = 0;
  144.                 pos.pop_back();
  145.             }
  146.         }
  147.     }
  148.     for (auto x : ds)
  149.         cout << max(x, 0ll) << ' ';
  150.     cout << '\n';
  151. }
  152.  
  153. void solve2()
  154. {
  155.     ll n;
  156.     cin >> n;
  157.     vl ds(n);
  158.     for (auto &x : ds)
  159.         cin >> x;
  160.  
  161.     vector<a2l> rs;
  162.     for (auto &x : rs)
  163.         cin >> x[0] >> x[1];
  164.     vl ans(n);
  165.  
  166.     ll hl = rs.back()[0], hh = rs.back()[1], delta = 0;
  167.     for (ll i = n - 2; i >= 0; --i) {
  168.         auto p = rs[i + 1], c = rs[i];
  169.         if (ds[i] == 1) {
  170.             delta += 1;
  171.             hl -= 1;
  172.             hh -= 1;
  173.         } else {
  174.         }
  175.         if (hl > c[1] || hh < c[0]) {
  176.             cout << -1 << '\n';
  177.             return;
  178.         }
  179.     }
  180.  
  181.     for (auto x : ans)
  182.         cout << x << ' ';
  183.     cout << '\n';
  184. }
  185.  
  186. void solve1()
  187. {
  188.     ll n;
  189.     cin >> n;
  190.     vl ds(n);
  191.     for (auto &x : ds)
  192.         cin >> x;
  193.  
  194.     vl ni;
  195.     for (ll i = 0; i < n; ++i)
  196.         if (ds[i] == -1)
  197.             ni.push_back(i);
  198.     reverse(ni.begin(), ni.end());
  199.  
  200.     vector<a2l> rs;
  201.     for (auto &x : rs)
  202.         cin >> x[0] >> x[1];
  203.     vl ans(n);
  204.     for (ll i = 0, hl = 0, hh = 0; i < n; ++i) {
  205.         if (ds[i] != -1) {
  206.             ans[i] = ds[i];
  207.             hl += ds[i];
  208.             hh += ds[i];
  209.         } else {
  210.             ll delta = rs[i][0] - hl;
  211.             if (delta > 0) {
  212.                 auto it = lower_bound(ni.begin(), ni.end(), i, std::greater<ll>());
  213.                 if (ni.end() - it >= delta) {
  214.                     for (ll j = 0; j < delta; ++j) {
  215.                         ans[ni.back()] = 1;
  216.                         ni.pop_back();
  217.                     }
  218.                     hh += delta;
  219.                     hl += delta;
  220.                 } else {
  221.                     cout << -1 << '\n';
  222.                     return;
  223.                 }
  224.             }
  225.             if (hh < rs[i][0] || hl > rs[i][1]) {
  226.                 cout << -1 << '\n';
  227.                 return;
  228.             }
  229.         }
  230.     }
  231.     while (!ni.empty()) {
  232.         ans[ni.back()] = 0;
  233.         ni.pop_back();
  234.     }
  235.     for (auto x : ans)
  236.         cout << x << ' ';
  237.     cout << '\n';
  238. }
  239.  
  240. int main(int argc, char **argv)
  241. {
  242.     std::ios::sync_with_stdio(false);
  243.     std::cin.tie(nullptr);
  244.  
  245.     ll t;
  246.     cin >> t;
  247.     while (t--)
  248.         solve_bili();
  249.  
  250.     return 0;
  251. };
  252.  
Advertisement
Add Comment
Please, Sign In to add comment