Salvens

D

Jul 27th, 2023
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <array>
  2. #include <iostream>
  3. #include <vector>
  4. #include <stack>
  5. #include <deque>
  6.  
  7. using namespace std;
  8.  
  9. #define int long long
  10.  
  11. const long long INF = 1e18 + 7;
  12. const int MAXN = 2e5 + 10;
  13. const int N = 2e5;
  14.  
  15. signed main() {
  16.     ios_base::sync_with_stdio(false);
  17.     cin.tie(nullptr);
  18.     cout.tie(nullptr);
  19.     int n;
  20.     cin >> n;
  21.     deque<int> a(n);
  22.     vector<bool> used1(n + 1), used2(n + 2);
  23.     stack<int> st;
  24.     for (int i = 0; i < n; ++i) {
  25.         cin >> a[i];
  26.         used1[a[i]] = true;
  27.     }
  28.     vector<pair<int, int>> ans;
  29.     int cur = 1;
  30.     while (cur <= n) {
  31.         if (used1[cur]) {
  32.             int cnt = 0;
  33.             while (a[0] != cur) {
  34.                 st.push(a[0]);
  35.                 used1[a[0]] = false;
  36.                 used2[a[0]] = true;
  37.                 a.pop_front();
  38.                 ++cnt;
  39.             }
  40.             st.push(a[0]);
  41.             used1[a[0]] = false;
  42.             used2[a[0]] = true;
  43.             a.pop_front();
  44.             ++cnt;
  45.             ans.emplace_back(1, cnt);
  46.             ans.emplace_back(2, 1);
  47.             st.pop();
  48.             ++cur;
  49.         } else if (used2[cur]) {
  50.             if (st.top() != cur) {
  51.                 cout << 0 << '\n';
  52.                 return 0;
  53.             }
  54.             ans.emplace_back(2, 1);
  55.             used2[cur] = false;
  56.             st.pop();
  57.             ++cur;
  58.         }
  59.     }
  60.     vector<pair<int, int>> ans1 = {ans[0]};
  61.     for (int i = 1; i < ans.size(); ++i) {
  62.         if (ans1.back().first == ans[i].first) {
  63.             ++ans1.back().second;
  64.         } else {
  65.             ans1.emplace_back(ans[i]);
  66.         }
  67.     }
  68.     for (auto& [x, y]: ans1) {
  69.         cout << x << ' ' << y << '\n';
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment