[CF1312E]Array Shrinking

题目

传送门

题解

由于 \(n\le 500\),所以这道题支持类似 \(\mathcal O(n^3)\) 之类的小暴力...

发现对于固定的某一段 \([l,r]\),我们可以直接处理出它们会合并成什么亚子,定义 \(f[i][j]\)\([i,j]\) 可以合并成什么数,如果不可行为 \(-1\),否则为合并的值

这里直接给出求 \(f\) 的代码,不作过多说明

inline int Merge(const int l,const int r){
    if(l==r)return a[l];
    if(a[l]!=a[l+1])return -1;
    if(l+1==r)return a[l]+1;
    rep(i,l+2,r)if(a[i-1]+1!=a[i])return -1;
    return a[r]+1;
}

inline void Solve_f(){
    rep(i,1,n)rep(j,i,n)f[i][j]=Merge(i,j);
}

然后,我们可以直接进行 \(dfs\),首先定义 \(memo[i][j]\) 表示区间 \([i,j]\) 最少会被合并成多少个点,首先我们显然知道,若 \(f[i][j]\neq -1\),那么 \(memo[i][j]=1\),对于其他情况,显然有转移

\[memo[i][j]=\min \{memo[i][k]+memo[k+1][j]|k\in[l,r-1]\} \]

但是有一种比较特殊的情况,就是 \(memo[i][k]=memo[k+1][j]=1\)\(f[i][k]=f[k+1][j]\) 时,我们可以将左右区间合并,即 \(memo[i][j]=1,f[i][j]=f[i][k]+1\),其他情况随便做就可以了...

代码

#include<cstdio>
#include<cstring>

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
typedef long long LL;
// typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef unsigned uint;
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void read(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T read(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=500;
const int INF=(1<<30)-1;

int a[MAXN+5],n;
int f[MAXN+5][MAXN+5];

inline void Init(){
    n=read(1);
    rep(i,1,n)a[i]=read(1);
}

inline int Merge(const int l,const int r){
    if(l==r)return a[l];
    if(a[l]!=a[l+1])return -1;
    if(l+1==r)return a[l]+1;
    rep(i,l+2,r)if(a[i-1]+1!=a[i])return -1;
    return a[r]+1;
}

inline void Solve_f(){
    rep(i,1,n)rep(j,i,n){
        f[i][j]=Merge(i,j);
        // printf("f[%d, %d] == %d\n",i,j,f[i][j]);
    }
}

int memo[MAXN+5][MAXN+5];
int dfs(const int l,const int r){
    // printf("Now l == %d, r == %d\n",l,r);
    if(f[l][r]!=-1)return memo[l][r]=1;
    if(memo[l][r]!=-1)return memo[l][r];
    memo[l][r]=INF;int ret1,ret2;
    rep(i,l,r-1){
        ret1=dfs(l,i),ret2=dfs(i+1,r);
        if(ret1==ret2 && ret1==1 && f[l][i]==f[i+1][r]){
            f[l][r]=f[l][i]+1,memo[l][r]=1;
        }
        else if(ret1+ret2<memo[l][r])memo[l][r]=ret1+ret2;
    }
    // printf("memo[%d, %d] == %d\n", l, r, memo[l][r]);
    return memo[l][r];
}

signed main(){
    Init();
    Solve_f();
    memset(memo,-1,sizeof memo);
    printf("%d\n",dfs(1,n));
    return 0;
}
posted @ 2020-07-28 16:15  Arextre  阅读(192)  评论(0编辑  收藏  举报