This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (ll)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
ll mod = 1e9 + 7;
const int N = 1e6 + 1;
int st[N * 4];
void upd(int id, int l, int r, int p, int v) {
st[id] = max(st[id], v);
if (l == r) {
return;
}
int m = (l + r) / 2;
if (m >= p) {
upd(id * 2, l, m, p, v);
} else {
upd(id * 2 + 1, m + 1, r, p, v);
}
}
int get(int id, int l, int r, int u, int v) {
if (l > v || r < u || u > v)
return 0;
if (l >= u && r <= v) {
return st[id];
}
int m = (l + r) / 2;
return max(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v));
}
int a[N], f[N], len = 0;
vector <int> layer[N], lst;
int dfs(int u) {
lst.pb(u);
if (f[u] == len)
return 1;
while (sz(layer[f[u] + 1]) && layer[f[u] + 1].back() < u) {
layer[f[u] + 1].pop_back();
}
while (sz(layer[f[u] + 1]) && a[layer[f[u] + 1].back()] > a[u]) {
int is = dfs(layer[f[u] + 1].back());
layer[f[u] + 1].pop_back();
if (is) {
return 1;
}
}
lst.pop_back();
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
f[i] = get(1, 1, n, 1, a[i] - 1) + 1;
upd(1, 1, n, a[i], f[i]);
len = max(len, f[i]);
}
for (int i = n; i >= 1; i--) {
layer[f[i]].pb(i);
}
vector < vector <int>> ans;
while (sz(layer[1])) {
lst.clear();
if (dfs(layer[1].back())) {
ans.pb(lst);
}
layer[1].pop_back();
}
cout << sz(ans) << " " << len << '\n';
for (auto x : ans) {
for (auto t : x) {
cout << t << ' ';
}
cout << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |