跳到正文

题解

题解:P8425 [JOI Open 2022] 长颈鹿 / Giraffes

ax_by_c约 16 分钟
原文
文章信息与版本

文章信息

发布
2025年10月6日
收录
2025年12月12日
更新
2026年11月6日
篇幅
约 16 分钟
题目
P8425

版本

  1. v2当前
  2. v1

观感不是差的等价于序列中不存在离散化后为 2 1 4 3,2 4 1 3,3 1 4 2,3 4 1 2 的子序列。

发挥注意力,这个条件等价于每次删去开头或结尾的最小值或最大值能删完,以此不难设计出一个 O(n3)O(n^3) dp。

但是这样其实不好优化并且是比较无脑的,考虑判断能否固定一些位置不动。

把值画到坐标轴上形成点,那么上述过程就相当于每次删掉正方形的两条边且这两条边的交点处有一个点。

由于其它数可以任意放,所以能够固定位置不动相当于能够将这些点按照上述形态放入一系列成链嵌套的正方形中。

不难设计出这样的 dp:fi,j,0/1/2/3f_{i,j,0/1/2/3} 表示 (i,ai)(i,a_i) 在正方形的某角且共有 jj 个点时正方形的最小边长,这样是 O((n−ans)n2)O((n-ans)n^2) 的。

由于转移是二维偏序,容易做到 O((n−ans)nlog⁡n)O((n-ans)n\log n)。由于数据随机,参考 LIS 可知 n−ans=O(n)n-ans=O(\sqrt n),于是时间复杂度 O(nnlog⁡n)O(n\sqrt n\log n)。

点击查看参考代码
C++
#include<bits/stdc++.h>
#define rep(i,l,r) for(int i=(l),qwp=(r);i<=qwp;i++)
#define per(i,r,l) for(int i=(r),qwp=(l);i>=qwp;i--)
#define repll(i,l,r) for(ll i=(l),qwp=(r);i<=qwp;i++)
#define perll(i,r,l) for(ll i=(r),qwp=(l);i>=qwp;i--)
#define pb push_back
#define ins insert
#define clr clear
#define ers(S,X) (S).erase((S).find((X)))
#define uset unordered_set
#define umap unordered_map
#define mset multiset
#define pct __builtin_popcountll
using namespace std;
namespace ax_by_c{
typedef unsigned long long ull;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
constexpr int intinf=1e9;
constexpr int N=8005;
int n,a[N],f[N][4],g[N][4],hsh[N*5],hc;
struct BIT{
    int lb(int x){return x&(-x);}
    int tr[N];
    void clr(){rep(i,1,hc)tr[i]=intinf;}
    void upd(int i,int x){for(;i<=hc;i+=lb(i))tr[i]=min(tr[i],x);}
    int Q(int i){int r=intinf;for(;i;i-=lb(i))r=min(r,tr[i]);return r;}
}tr;
struct node{int xl,xr,yl,yr;};
vector<node>trs;
void add(int i,int j){
    if(j==0)trs.pb({i-f[i][j]+1,i,a[i]-f[i][j]+1,a[i]});
    if(j==1)trs.pb({i,i+f[i][j]-1,a[i]-f[i][j]+1,a[i]});
    if(j==2)trs.pb({i-f[i][j]+1,i,a[i],a[i]+f[i][j]-1});
    if(j==3)trs.pb({i,i+f[i][j]-1,a[i],a[i]+f[i][j]-1});
}
void chk(int i,int j){
    if(j==0)if(i-g[i][j]+1<1||a[i]-g[i][j]+1<1)g[i][j]=intinf;
    if(j==1)if(n<i+g[i][j]-1||a[i]-g[i][j]+1<1)g[i][j]=intinf;
    if(j==2)if(i-g[i][j]+1<1||n<a[i]+g[i][j]-1)g[i][j]=intinf;
    if(j==3)if(n<i+g[i][j]-1||n<a[i]+g[i][j]-1)g[i][j]=intinf;
}
struct NODE{int x,y,z;};
vector<NODE>A,B;
bool cmp(NODE p,NODE q){return p.x<q.x;}
void slv(int _csid,int _csi){
    scanf("%d",&n);
    rep(i,1,n)scanf("%d",&a[i]);
    rep(i,1,n)rep(j,0,3)f[i][j]=1;
    rep(_,2,n+1){
        trs.clr();rep(i,1,n)rep(j,0,3)if(f[i][j]!=intinf)add(i,j);
        rep(i,1,n)rep(j,0,3)g[i][j]=intinf;
        {
            hc=0;for(auto t:trs)hsh[++hc]=t.xl-t.yl;rep(i,1,n)hsh[++hc]=-a[i]+i-1;
            sort(hsh+1,hsh+1+hc),hc=unique(hsh+1,hsh+1+hc)-hsh-1;
            A.clr();for(auto t:trs)A.pb({t.yr+1,(int)(lower_bound(hsh+1,hsh+1+hc,t.xl-t.yl)-hsh),-t.xl+1});
            B.clr();rep(i,1,n)B.pb({a[i],(int)(lower_bound(hsh+1,hsh+1+hc,-a[i]+i-1)-hsh),i});
            sort(A.begin(),A.end(),cmp),sort(B.begin(),B.end(),cmp);
            tr.clr();int j=0;
            for(auto p:B){
                while(j<(int)A.size()&&A[j].x<=p.x)tr.upd(A[j].y,A[j].z),j++;
                g[p.z][0]=min(g[p.z][0],p.z+tr.Q(p.y));
            }
        }
        {
            hc=0;for(auto t:trs)hsh[++hc]=t.yl-t.xl;rep(i,1,n)hsh[++hc]=a[i]-i;
            sort(hsh+1,hsh+1+hc),hc=unique(hsh+1,hsh+1+hc)-hsh-1;
            A.clr();for(auto t:trs)A.pb({t.xr+1,(int)(lower_bound(hsh+1,hsh+1+hc,t.yl-t.xl)-hsh),-t.yl+1});
            B.clr();rep(i,1,n)B.pb({i,(int)(lower_bound(hsh+1,hsh+1+hc,a[i]-i)-hsh),i});
            sort(A.begin(),A.end(),cmp),sort(B.begin(),B.end(),cmp);
            tr.clr();int j=0;
            for(auto p:B){
                while(j<(int)A.size()&&A[j].x<=p.x)tr.upd(A[j].y,A[j].z),j++;
                g[p.z][0]=min(g[p.z][0],a[p.z]+tr.Q(p.y));
            }
        }
        {
            hc=0;for(auto t:trs)hsh[++hc]=-t.xr-t.yl;rep(i,1,n)hsh[++hc]=-a[i]-i-1;
            sort(hsh+1,hsh+1+hc),hc=unique(hsh+1,hsh+1+hc)-hsh-1;
            A.clr();for(auto t:trs)A.pb({t.yr+1,(int)(lower_bound(hsh+1,hsh+1+hc,-t.xr-t.yl)-hsh),t.xr+1});
            B.clr();rep(i,1,n)B.pb({a[i],(int)(lower_bound(hsh+1,hsh+1+hc,-a[i]-i-1)-hsh),i});
            sort(A.begin(),A.end(),cmp),sort(B.begin(),B.end(),cmp);
            tr.clr();int j=0;
            for(auto p:B){
                while(j<(int)A.size()&&A[j].x<=p.x)tr.upd(A[j].y,A[j].z),j++;
                g[p.z][1]=min(g[p.z][1],-p.z+tr.Q(p.y));
            }
        }
        {
            hc=0;for(auto t:trs)hsh[++hc]=t.xr+t.yl;rep(i,1,n)hsh[++hc]=a[i]+i;
            sort(hsh+1,hsh+1+hc),hc=unique(hsh+1,hsh+1+hc)-hsh-1;
            A.clr();for(auto t:trs)A.pb({-t.xl,(int)(lower_bound(hsh+1,hsh+1+hc,t.xr+t.yl)-hsh),-t.yl+1});
            B.clr();rep(i,1,n)B.pb({-i-1,(int)(lower_bound(hsh+1,hsh+1+hc,a[i]+i)-hsh),i});
            sort(A.begin(),A.end(),cmp),sort(B.begin(),B.end(),cmp);
            tr.clr();int j=0;
            for(auto p:B){
                while(j<(int)A.size()&&A[j].x<=p.x)tr.upd(A[j].y,A[j].z),j++;
                g[p.z][1]=min(g[p.z][1],a[p.z]+tr.Q(p.y));
            }
        }
        {
            hc=0;for(auto t:trs)hsh[++hc]=t.yr+t.xl+1;rep(i,1,n)hsh[++hc]=i+a[i];
            sort(hsh+1,hsh+1+hc),hc=unique(hsh+1,hsh+1+hc)-hsh-1;
            A.clr();for(auto t:trs)A.pb({-t.yl,(int)(lower_bound(hsh+1,hsh+1+hc,t.yr+t.xl+1)-hsh),-t.xl+1});
            B.clr();rep(i,1,n)B.pb({-a[i]-1,(int)(lower_bound(hsh+1,hsh+1+hc,i+a[i])-hsh),i});
            sort(A.begin(),A.end(),cmp),sort(B.begin(),B.end(),cmp);
            tr.clr();int j=0;
            for(auto p:B){
                while(j<(int)A.size()&&A[j].x<=p.x)tr.upd(A[j].y,A[j].z),j++;
                g[p.z][2]=min(g[p.z][2],p.z+tr.Q(p.y));
            }
        }
        {
            hc=0;for(auto t:trs)hsh[++hc]=-t.yr-t.xl;rep(i,1,n)hsh[++hc]=-i-a[i];
            sort(hsh+1,hsh+1+hc),hc=unique(hsh+1,hsh+1+hc)-hsh-1;
            A.clr();for(auto t:trs)A.pb({t.xr+1,(int)(lower_bound(hsh+1,hsh+1+hc,-t.yr-t.xl)-hsh),t.yr+1});
            B.clr();rep(i,1,n)B.pb({i,(int)(lower_bound(hsh+1,hsh+1+hc,-i-a[i])-hsh),i});
            sort(A.begin(),A.end(),cmp),sort(B.begin(),B.end(),cmp);
            tr.clr();int j=0;
            for(auto p:B){
                while(j<(int)A.size()&&A[j].x<=p.x)tr.upd(A[j].y,A[j].z),j++;
                g[p.z][2]=min(g[p.z][2],-a[p.z]+tr.Q(p.y));
            }
        }
        {
            hc=0;for(auto t:trs)hsh[++hc]=t.yr-t.xr+1;rep(i,1,n)hsh[++hc]=a[i]-i;
            sort(hsh+1,hsh+1+hc),hc=unique(hsh+1,hsh+1+hc)-hsh-1;
            A.clr();for(auto t:trs)A.pb({-t.yl,(int)(lower_bound(hsh+1,hsh+1+hc,t.yr-t.xr+1)-hsh),t.xr+1});
            B.clr();rep(i,1,n)B.pb({-a[i]-1,(int)(lower_bound(hsh+1,hsh+1+hc,a[i]-i)-hsh),i});
            sort(A.begin(),A.end(),cmp),sort(B.begin(),B.end(),cmp);
            tr.clr();int j=0;
            for(auto p:B){
                while(j<(int)A.size()&&A[j].x<=p.x)tr.upd(A[j].y,A[j].z),j++;
                g[p.z][3]=min(g[p.z][3],-p.z+tr.Q(p.y));
            }
        }
        {
            hc=0;for(auto t:trs)hsh[++hc]=-t.yr+t.xr;rep(i,1,n)hsh[++hc]=-a[i]+i;
            sort(hsh+1,hsh+1+hc),hc=unique(hsh+1,hsh+1+hc)-hsh-1;
            A.clr();for(auto t:trs)A.pb({-t.xl,(int)(lower_bound(hsh+1,hsh+1+hc,-t.yr+t.xr)-hsh),t.yr+1});
            B.clr();rep(i,1,n)B.pb({-i-1,(int)(lower_bound(hsh+1,hsh+1+hc,-a[i]+i)-hsh),i});
            sort(A.begin(),A.end(),cmp),sort(B.begin(),B.end(),cmp);
            tr.clr();int j=0;
            for(auto p:B){
                while(j<(int)A.size()&&A[j].x<=p.x)tr.upd(A[j].y,A[j].z),j++;
                g[p.z][3]=min(g[p.z][3],-a[p.z]+tr.Q(p.y));
            }
        }
        bool fd=0;rep(i,1,n)rep(j,0,3)chk(i,j),fd|=(f[i][j]=g[i][j])!=intinf;
        if(!fd)return (void)(printf("%d\n",n-_+1));
    }
}
void main(){
	// ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int T=1,csid=-1;
	// scanf("%d",&csid);
	// scanf("%d",&T);
	// scanf("%d",&csid);
	rep(i,1,T)slv(csid,i);
}
}
int main(){string __name="";if(__name!="")freopen((__name+".in").c_str(),"r",stdin),freopen((__name+".out").c_str(),"w",stdout);ax_by_c::main();return 0;}
/*
g++ -std=c++14 -O2 -Wall -Wextra "-Wl,--stack=200000000" A.cpp -o A.exe
A.exe
*/
原文