Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <array>
- #include <iostream>
- #include <vector>
- #include <stack>
- #include <deque>
- using namespace std;
- #define int long long
- const long long INF = 1e18 + 7;
- const int MAXN = 2e5 + 10;
- const int N = 2e5;
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n;
- cin >> n;
- deque<int> a(n);
- vector<bool> used1(n + 1), used2(n + 2);
- stack<int> st;
- for (int i = 0; i < n; ++i) {
- cin >> a[i];
- used1[a[i]] = true;
- }
- vector<pair<int, int>> ans;
- int cur = 1;
- while (cur <= n) {
- if (used1[cur]) {
- int cnt = 0;
- while (a[0] != cur) {
- st.push(a[0]);
- used1[a[0]] = false;
- used2[a[0]] = true;
- a.pop_front();
- ++cnt;
- }
- st.push(a[0]);
- used1[a[0]] = false;
- used2[a[0]] = true;
- a.pop_front();
- ++cnt;
- ans.emplace_back(1, cnt);
- ans.emplace_back(2, 1);
- st.pop();
- ++cur;
- } else if (used2[cur]) {
- if (st.top() != cur) {
- cout << 0 << '\n';
- return 0;
- }
- ans.emplace_back(2, 1);
- used2[cur] = false;
- st.pop();
- ++cur;
- }
- }
- vector<pair<int, int>> ans1 = {ans[0]};
- for (int i = 1; i < ans.size(); ++i) {
- if (ans1.back().first == ans[i].first) {
- ++ans1.back().second;
- } else {
- ans1.emplace_back(ans[i]);
- }
- }
- for (auto& [x, y]: ans1) {
- cout << x << ' ' << y << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment