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>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
ll readint(){
ll x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,dp[1000005],prv[1000005];
pii a[1000005];
namespace seg{
int lim;
pii st[4000005];
void init(){
for(lim=1;lim<=n+1;lim*=2);
}
void modify(int p,pii v){
for(st[p+=lim]=v;p>1;p>>=1) st[p>>1]=max(st[p],st[p^1]);
}
pii query(int l,int r){
pii ret=mp(-1e9,-1);
for(l+=lim,r+=lim+1;l<r;l>>=1,r>>=1){
if(l&1) ret=max(ret,st[l++]);
if(r&1) ret=max(ret,st[--r]);
}
return ret;
}
};
int solve(int x){
for(int i=0;i<=n;i++){
dp[i]=(int)-1e9;
prv[i]=0;
}
dp[0]=0;
for(int i=0;i<=n;i++) seg::modify(i,mp(dp[i],i));
for(int i=1;i<=n;i++){
if(a[i].fi>i) continue;
pii v=seg::query(max(0,i-x),i-a[i].fi);
dp[i]=v.fi+1;
prv[i]=v.se;
seg::modify(i,mp(dp[i],i));
}
return dp[n];
}
int main(){
n=readint();
for(int i=1;i<=n;i++) a[i].fi=readint(),a[i].se=i;
seg::init();
sort(a+1,a+n+1);
int mx=solve(n);
int l=a[n].fi,r=n,f=n;
while(l<=r){
int mid=(l+r)/2;
if(solve(mid)==mx) f=mid,r=mid-1;
else l=mid+1;
}
solve(f);
printf("%d\n",mx);
int p=n;
while(p>0){
printf("%d ",p-prv[p]);
for(int i=prv[p]+1;i<=p;i++) printf("%d ",a[i].se);
printf("\n");
p=prv[p];
}
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |