QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#671703 | #5035. foo~ | EXODUS | 0 | 0ms | 0kb | C++17 | 4.5kb | 2024-10-24 14:07:42 | 2024-10-24 14:07:44 |
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i(l);i<=(r);++i)
#define per(i,r,l) for(int i(r);i>=(l);--i)
#define eb emplace_back
#define Debug(...) fprintf(stderr,__VA_ARGS__)
#define Exit(p) fprintf(stderr,"[exit]: at breakpoint %d\n",p),exit(0);
#define File(filename) freopen(filename ".in","r",stdin),freopen(filename ".out","w",stdout)
template<typename T>void read(T &x){x=0;T flg=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')flg=-1;ch=getchar();}while(isdigit(ch))x=(x<<3)+(x<<1)+(ch^48),ch=getchar();x*=flg;}
template<typename T,typename... Args>void read(T &x,Args &...args){read(x),read(args...);}
template<typename T>void cmax(T& x,T y){x=max(x,y);}
template<typename T,typename... Args>void cmax(T& x,T y,Args ...args){cmax(x,y);cmax(x,args...);}
template<typename T>void cmin(T& x,T y){x=min(x,y);}
template<typename T,typename... Args>void cmin(T& x,T y,Args ...args){cmin(x,y);cmin(x,args...);}
template<typename T,typename U>ostream &operator<<(ostream &os,const pair<T,U>&x){return os<<"("<<x.first<<","<<x.second<< ")"; };
template<typename T>ostream &operator<<(ostream &os,const vector<T> &as){const int sz=as.size();os<<"[";for(int i=0;i<sz;++i){if(i>=256){os<<",...";break;}if(i>0){os<<",";}os<<as[i];}return os<<"]";}
bool membg=0;
constexpr int N=6e5+7,inf=0x3f3f3f3f;
// struct Sgt{
// #define ls(k) ((k)<<1)
// #define rs(k) ((k)<<1|1)
// int t[N<<2],tag[N<<2];
// void apply(int k,int v){t[k]+=v,tag[k]+=v;}
// void pull(int k){t[k]=max(t[ls(k)],t[rs(k)]);}
// void push(int k){if(!tag[k])return;for(auto v:{ls(k),rs(k)})apply(v,tag[k]);tag[k]=0;}
// void build(int k,int l,int r,int *F){
// tag[k]=0;
// if(l==r)return t[k]=F[l-1],void();
// int mid=(l+r)>>1;
// build(ls(k),l,mid,F);
// build(rs(k),mid+1,r,F);
// pull(k);
// }
// void modify(int k,int l,int r,int ql,int qr,int v){
// if(ql<=l&&r<=qr)return apply(k,v),void();
// int mid=(l+r)>>1;push(k);
// if(ql<=mid)modify(ls(k),l,mid,ql,qr,v);
// if(mid<qr)modify(rs(k),mid+1,r,ql,qr,v);
// pull(k);
// }
// int query(int k,int l,int r,int ql,int qr){
// if(ql<=l&&r<=qr)return t[k];
// int mid=(l+r)>>1,res=0;push(k);
// if(ql<=mid)cmax(res,query(k<<1,l,mid,ql,qr));
// if(mid<qr)cmax(res,query(k<<1|1,mid+1,r,ql,qr));
// return res;
// }
// #undef ls
// #undef rs
// }sgt[2];
struct Ds{
int bin[N],val[N],tot,tag,sum,pre[N],nxt[N];
void init(int n){
for(int i=1;i<=n+1;i++)bin[i]=i,val[i]=0,pre[i]=i-1,nxt[i]=i+1;
tot=tag=sum=0;
}
int find(int x){return (x==bin[x]?x:bin[x]=find(bin[x]));}
void erase(int x){bin[x]=find(x+1);pre[nxt[x]]=pre[x];nxt[pre[x]]=nxt[x];}
void modify(int x){
int p=find(x);
if(p==find(1)){tot++;return;}
val[p]--;sum--;
if(val[p]==0)val[p]=val[pre[p]],erase(pre[p]);
}
void push(int p,int v){
v+=tag;int now=p-1;
while(now&&tot-sum<=v)sum-=val[now],erase(now),now=pre[now];
if(!now)tot=v,sum=0;
else val[p]=tot-sum-v,sum+=tot-sum-v;
}
}dsu[2];
int n,k,a[N],ans,f[37][N];
bool memed=0;
void calc(){
// for(int i=1;i<=n;i++)
// printf("%d ",a[i]);
// printf("\n");
for(int i=0;i<=k;i++)
for(int j=1;j<=n;j++)
f[i][j]=-inf;
f[0][0]=0;
for(int i=1;i<=k;i++){
// sgt[0].build(1,1,n,f[i-1]);
// sgt[1].build(1,1,n,f[i-1]);
static int stk[N],top;
stk[0]=top=0;
dsu[0].init(n),dsu[1].init(n);
for(int j=1;j<=n;j++){
dsu[0].push(j,f[i-1][j-1]);dsu[1].push(j,f[i-1][j-1]);
while(top&&a[stk[top]]<a[j])dsu[1].tag++,dsu[1].modify(stk[top]+1),top--;
dsu[1].modify(1);dsu[0].modify(stk[top]+1);stk[++top]=j;
f[i][j]=max(dsu[0].tot-dsu[0].tag,dsu[1].tot-dsu[1].tag);
// while(top&&a[stk[top]]<a[j])
// sgt[1].modify(1,1,n,1,stk[top],-1),--top;
// stk[++top]=j;
// sgt[1].modify(1,1,n,1,j,1);
// sgt[0].modify(1,1,n,stk[top-1]+1,stk[top],1);
// f[i][j]=max(sgt[0].query(1,1,n,1,j),sgt[1].query(1,1,n,1,j));
}
}
for(int i=1;i<=n;i++)
cmax(ans,f[k][i]);
}
void solve(){
read(n,k);
for(int i=1;i<=n;i++)
read(a[i]);
int piv=0;
for(int i=1;i<=n;i++){
if(a[i]==n){
piv=i;
break;
}
}
ans=-inf;
rotate(a+1,a+piv,a+n+1);
calc();
rotate(a+1,a+2,a+n+1);
reverse(a+1,a+n+1);
calc();
printf("%d\n",ans);
return;
}
int main(){
File("alice");
Debug("%.3lfMB\n",fabs(&memed-&membg)/1024.0/1024.0);
int timbg=clock();
int T=1;
while(T--)solve();
int timed=clock();
Debug("%.3lfs\n",1.0*(timed-timbg)/CLOCKS_PER_SEC);
fflush(stdout);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Dangerous Syscalls
Test #1:
score: 0
Dangerous Syscalls
input:
23 6 16 20 22 4 21 10 3 7 5 8 15 12 9 1 6 17 23 13 11 19 18 14 2
output:
result:
Subtask #2:
score: 0
Dangerous Syscalls
Test #21:
score: 0
Dangerous Syscalls
input:
93943 1 87243 48984 50611 19218 77699 25025 85769 28141 13380 34875 42459 66419 53472 4367 48292 16894 92171 87263 42527 67085 30687 29235 27515 81053 31421 34864 83591 70491 75344 7026 50250 63402 26773 5330 36308 76399 80032 15501 16205 71750 73964 67876 68901 70548 2043 79979 89479 19784 38838 44...
output:
result:
Subtask #3:
score: 0
Dangerous Syscalls
Test #36:
score: 0
Dangerous Syscalls
input:
1992 25 144 612 1315 1966 1779 1773 1529 625 36 1849 1783 1441 1388 1558 1258 724 137 397 542 353 1162 1213 406 792 1317 882 994 298 1864 1969 103 449 508 1501 89 1721 195 778 657 222 1152 1780 613 743 1206 694 829 142 69 1973 1465 1343 655 1540 155 146 350 491 759 1695 1082 1357 1329 1745 232 1850 ...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #2:
0%