pb_jiang

CF1906E

Sep 12th, 2025
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. // Problem: E. Merge Not Sort
  2. // Contest: Codeforces - 2023-2024 ICPC, Asia Jakarta Regional Contest (Online Mirror, Unrated, ICPC Rules, Teams
  3. // Preferred) URL: https://codeforces.com/problemset/problem/1906/E Memory Limit: 1024 MB Time Limit: 1000 ms
  4. //
  5. // Powered by CP Editor (https://cpeditor.org)
  6.  
  7. #include <assert.h>
  8. #include <bits/stdc++.h>
  9. using namespace std;
  10. #ifndef __DEBUG__
  11. #define dbg(...) 42
  12. #endif
  13. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  14.  
  15. namespace rngs = std::ranges;
  16. using ll = long long;
  17. using a2l = array<ll, 2>;
  18. using vl = vector<ll>;
  19.  
  20. void solve()
  21. {
  22.     ll n;
  23.     cin >> n;
  24.     vl ps(2 * n);
  25.     for (auto &x : ps)
  26.         cin >> x;
  27.  
  28.     ps.push_back(2 * n + 1);
  29.     vector<a2l> opt;
  30.     for (ll i = 0, j = 0; i < 2 * n;) {
  31.         j = i;
  32.         while (j + 1 < 2 * n + 1 && ps[j] > ps[j + 1])
  33.             ++j;
  34.         // dbg(i, j, ps[j], ps[j + 1]);
  35.         opt.push_back({j - i + 1, i});
  36.         i = j + 1;
  37.     }
  38.  
  39.     dbg(opt);
  40.     vl sol;
  41.     auto dfs = [&](auto &&self, ll id, ll acc) -> bool {
  42.         if (acc == n)
  43.             return true;
  44.         if (acc > n)
  45.             return false;
  46.         if (id >= opt.size())
  47.             return false;
  48.         sol.push_back(id);
  49.         if (self(self, id + 1, acc + opt[id][0]))
  50.             return true;
  51.         sol.pop_back();
  52.         return self(self, id + 1, acc);
  53.     };
  54.     auto ans = dfs(dfs, 0, 0);
  55.     dbg(sol, ans);
  56.     if (!ans) {
  57.         cout << -1 << '\n';
  58.         return;
  59.     }
  60.  
  61.     vl v1, v2;
  62.     for (ll i = 0, j = 0; i < 2 * n; ++i) {
  63.         if (j < sol.size() && opt[sol[j]][0] + opt[sol[j]][1] <= i)
  64.             ++j;
  65.  
  66.         if (j < sol.size() && i >= opt[sol[j]][1] && i < opt[sol[j]][1] + opt[sol[j]][0])
  67.             v1.push_back(ps[i]);
  68.         else
  69.             v2.push_back(ps[i]);
  70.     }
  71.     assert(v1.size() == n);
  72.     assert(v2.size() == n);
  73.     for (auto x : v1)
  74.         cout << x << ' ';
  75.     cout << '\n';
  76.     for (auto x : v2)
  77.         cout << x << ' ';
  78.     cout << '\n';
  79. }
  80.  
  81. int main(int argc, char **argv)
  82. {
  83.     std::ios::sync_with_stdio(false);
  84.     std::cin.tie(nullptr);
  85.  
  86.     ll t = 1;
  87.     // cin >> t;
  88.     while (t--)
  89.         solve();
  90.  
  91.     return 0;
  92. };
  93.  
Advertisement
Add Comment
Please, Sign In to add comment