QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#472587 | #7932. AND-OR closure | ucup-team052# | RE | 0ms | 0kb | C++23 | 2.2kb | 2024-07-11 17:22:29 | 2024-07-11 17:22:29 |
answer
#include<bits/stdc++.h>
using namespace std;
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define mod 998244353
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read()
{
char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();}
int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();}
if(nega==-1) return -ans;
return ans;
}
void print(vector<int> x){for(int i=0;i<(int)x.size();i++) printf("%d%c",x[i]," \n"[i==(int)x.size()-1]);}
#define N 500005
int a[N],n,ans[N];
int solve(int l,int r,int ad)
{
// printf("%d %d\n",l,r);
if(r>n) exit(1);
if(l>r) return 0;
if(l==r)
{
ans[l]=ad+1;
return ans[l];
}
int mid=-1;
for(int i=l+1;i<=r;i++) if(a[i]>a[l])
{
mid=i;
break;
}
// printf("%d %d %d\n",l,r,mid);
if(mid==-1)
{
ans[l]=ad+(r-l+1);
solve(l+1,r,ad);
return ans[l];
}
else
{
int premxcnt=a[l]-(mid-l);
int ansmx=0;
int w=mid-l;
// ans[l]=ans[mid]=ad+w;
for(int i=l+1;i<=mid-1;i++) a[i]-=premxcnt+2;
ansmx=max(ansmx,solve(l+1,mid-1,ad));
ad+=w;
a[mid]-=w;
int cur=mid;
// solving [cur + 1, r]
vector<int> tmppos; tmppos.push_back(cur); tmppos.push_back(l);
for(int i=0;i<premxcnt;)
{
int nw=cur+(a[cur]-premxcnt+i);
// printf("%d %d - %d\n",l,r,nw);
if(a[nw]>nw-cur+premxcnt-i)
{ // h[nw]=h[cur]
for(int i=cur+1;i<=nw-1;i++) a[i]-=premxcnt-i;
ansmx=max(ansmx,solve(cur+1,nw-1,ad));
tmppos.push_back(nw);
a[nw]-=(nw-cur-1);
cur=nw;
}
else
{ // h[nw+1] is premx
for(int i=cur+1;i<=nw;i++) a[i]-=premxcnt-i;
ansmx=max(ansmx,solve(cur+1,nw,ad));
ansmx++;
for(int i:tmppos) ans[i]=ansmx;
tmppos.clear();
tmppos.push_back(nw+1);
ad=ansmx;
a[nw+1]-=(nw+1-l);
cur=nw+1;
i++;
}
}
for(int i=cur+1;i<=r;i++) a[i]--;
ansmx=max(ansmx,solve(cur+1,r,ad));
assert(tmppos.size()==1);
ansmx++;
ans[tmppos[0]]=ansmx;
return ansmx;
}
}
signed main()
{
n=read();
for(int i=1;i<=n;i++) a[i]=read();
solve(1,n,0);
for(int i=1;i<=n;i++) printf("%d%c",ans[i]," \n"[i==n]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
4 0 1 3 5