Skip to content

Instantly share code, notes, and snippets.

@manthanabc
Created November 8, 2025 10:33
Show Gist options
  • Select an option

  • Save manthanabc/53f8b366cfc4cd7faaf7b0f75554f2c5 to your computer and use it in GitHub Desktop.

Select an option

Save manthanabc/53f8b366cfc4cd7faaf7b0f75554f2c5 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
void solve() {
ll n;
if (!(cin >> n)) return;
vector<ll> a(n);
for (ll i = 0; i < n; i++) cin >> a[i];
// k is half the current array (n is even)
ll k = n / 2;
// Quick 1-op check on the WHOLE array as one subsequence of length n
// Conditions with k=n/2:
// 1) max(first k) < min(last k) OR
// 2) min(first k) > max(last k)
ll mxL = a[0], mnR = a[k];
for (ll i = 1; i < k; i++) mxL = max(mxL, a[i]);
for (ll i = k + 1; i < n; i++) mnR = min(mnR, a[i]);
bool ok1 = (mxL < mnR);
ll mnL = a[0], mxR = a[k];
for (ll i = 1; i < k; i++) mnL = min(mnL, a[i]);
for (ll i = k + 1; i < n; i++) mxR = max(mxR, a[i]);
bool ok2 = (mnL > mxR);
if (ok1 || ok2) {
// 1 operation: whole array is deletable
cout << 1 << endl;
cout << n << endl;
for (ll i = 0; i < n; i++) cout << a[i] << (i + 1 == n ? '\n' : ' ');
return;
}
// 2 operations guaranteed for a permutation of 1..n
// Let L = {1..k} and R = {k+1..n}
// First subsequence: pick L from left and R from right (preserving order)
vector<ll> leftIdx, rightIdx;
ll i = 0, j = n - 1;
while (i < j) {
while (i < j && !(1 <= a[i] && a[i] <= k)) i++;
while (i < j && !(k + 1 <= a[j] && a[j] <= n)) j--;
if (i < j) {
leftIdx.push_back(i);
rightIdx.push_back(j);
i++; j--;
}
}
// Build the first subsequence values in array order
vector<ll> firstSub;
for (ll p : leftIdx) firstSub.push_back(a[p]);
reverse(rightIdx.begin(), rightIdx.end());
for (ll p : rightIdx) firstSub.push_back(a[p]);
// Collect remaining (second subsequence) values
vector<char> used(n, 0);
for (ll p : leftIdx) used[p] = 1;
for (ll p : rightIdx) used[p] = 1;
vector<ll> secondSub;
for (ll t = 0; t < n; t++) if (!used[t]) secondSub.push_back(a[t]);
// Output
cout << 2 << endl;
cout << (ll)firstSub.size() << endl;
for (ll t = 0; t < (ll)firstSub.size(); t++)
cout << firstSub[t] << (t + 1 == (ll)firstSub.size() ? '\n' : ' ');
cout << (ll)secondSub.size() << endl;
for (ll t = 0; t < (ll)secondSub.size(); t++)
cout << secondSub[t] << (t + 1 == (ll)secondSub.size() ? '\n' : ' ');
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment