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;
#define int long long
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define sz(x) ((int)(x).size())
constexpr int N = 1e6 + 5;
int fw[N];
void upd(int c,int x){
for(;c<N;c+=c&-c) fw[c]=max(fw[c],x);
}
int query(int c,int res=0LL){
for(;c;c-=c&-c) res=max(res,fw[c]);
return res;
}
void solve(){
int n;
cin >> n;
int ar[n+5];
int dp[n+5];
for(int i=1;i<=n;i++) cin >> ar[i];
vector<array<int,2>> layer[n+5];
for(int i=1;i<=n;i++){
dp[i] = query(ar[i]) + 1;
upd(ar[i],dp[i]);
layer[dp[i]].pb({ar[i],i});
}
int lis = query(n);
vector<vector<int>> ans;
vector<int> poi(lis+5,0);
int d = 1;
vector<int> cur;
cur.pb(layer[1][0][1]);
bool ok = 1;
while(!cur.empty() && ok){
if(d==lis){
ans.pb(cur);
cur.clear();
for(int i=1;i<=lis;i++){
poi[i]++;
if(poi[i]==sz(layer[i])) ok=0;
}
d=1;
cur.pb(layer[d][poi[d]][1]);
continue;
}
int ind = poi[d];
while(poi[d+1] < sz(layer[d+1]) && layer[d+1][poi[d+1]][1] < layer[d][ind][1]) poi[d+1]++;
if(poi[d+1]==sz(layer[d+1])) break;
if(ar[cur.back()]<layer[d+1][poi[d+1]][0]){
d++;
cur.pb(layer[d][poi[d]][1]);
}
else{
cur.pop_back();
poi[d]++;
if(poi[d]==sz(layer[d])) break;
d--;
if(d==0){
d=1;
cur.pb(layer[d][poi[d]][1]);
}
}
}
cout << sz(ans) << " " << lis << endl;
for(auto u : ans){
for(int x : u) cout << x << " ";
cout << endl;
}
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;//cin >> t;
while(t--) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |