Acwing 322. Elimination block (interval dp)

Topic portal

Meaning of the question: give you a sequence of length n, you can choose a continuous block each time [i, j] [i, j][i,j ] is eliminated, after eliminationi − 1 i-1i1 andj + 1 j+1j+1 connection, the score of this operation is(j − i + 1) 2 (j-i+1)^2(ji+1)2. Ask how many points you can get.

Idea: It is easy to think of f [i] [j] f[i][j]f [ i ] [ j ] represents the interval[i, j] [i,j][i,j ] is the maximum score, but if thej + 1 j+1j+If 1 is the same as the jth, we can’t just eliminate thejjj without eliminating thej + 1 j+1j+1 piece. So we need to add one more dimension to represent the state, that is,f [i] [j] [k] f[i][j][k]f [ i ] [ j ] [ k ] means to eliminate[i, j] [i,j][i,j ] square and also eliminatejjAfter j (not includingjjj k k k andjjThe maximum value when j blocks are the same block.

Then there are two types of state transitions:

  1. From jjj forward to find the last one andjjj squares with different squares, record their position asppp,则有 f [ i ] [ j ] [ k ] = m a x ( f [ i ] [ j ] [ k ] , f [ i ] [ p − 1 ] [ 0 ] + ( j + k − p + 1 ) 2 ) f[i][j][k]=max(f[i][j][k],f[i][p-1][0]+(j+k-p+1)^2) f[i][j][k]=max(f[i][j][k],f[i][p1][0]+(j+kp+1)2)
  2. After finding the above p, we start from iii look backwards, find every q, there isa [q]! = a [q + 1] a[q]!=a[q+1]a[q]!=a[q+1]&& a [ q ] = = a [ j ] a[q]==a[j] a[q]==a [ j ] , and satisfyq + 1 <= p − 1 q+1<=p-1q+1<=p1,维护状态 f [ i ] [ j ] [ k ] = m a x ( f [ i ] [ j ] [ k ] , f [ i ] [ q ] [ j + k − p + 1 ] + f [ q + 1 ] [ p − 1 ] [ 0 ] ) f[i][j][k]=max(f[i][j][k],f[i][q][j+k-p+1]+f[q+1][p-1][0]) f[i][j][k]=max(f[i][j][k],f[i][q][j+kp+1]+f[q+1][p1][0])

Pay attention to the condition of recursive exit, and use recursive implementation.

Code:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
//#define int long long
#define pii pair<int,int>
#define ull unsigned long long
#define pdd pair<double,double>
#define lowbit(x) x&-x
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read()
{
    
    
    int x=0,f=1;
    char ch=gc();
    while(ch<'0'||ch>'9')
    {
    
    
        if(ch=='-')
            f=-1;
        ch=gc();
    }
    while(ch>='0'&&ch<='9')
    {
    
    
        x=x*10+ch-'0';
        ch=gc();
    }
    return x*f;
}
using namespace std;
const int N=2e3+55;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;
const double PI=acos(-1);

int a[N],f[222][222][222],cas=0;

int dfs(int x,int y,int z)
{
    
    
    if(f[x][y][z])
        return f[x][y][z];
    if(x==y)
        return f[x][y][z]=(1+z)*(1+z);
    if(x>y)
        return 0;
    int p=y;
    while(p>=x&&a[p]==a[y])
        p--;
    p++;
    f[x][y][z]=max(f[x][y][z],dfs(x,p-1,0)+(y+z-p+1)*(y+z-p+1));
    int q=x;
    while(q+1<=p-1)
    {
    
    
        if(a[q]==a[y]&&a[q+1]!=a[y])
            f[x][y][z]=max(f[x][y][z],dfs(x,q,z+y-p+1)+dfs(q+1,p-1,0));
        q++;
    }
    return f[x][y][z];
}
void solve()
{
    
    
    int n;
    cin>>n;
    memset(f,0,sizeof f);
    for(int i=1; i<=n; i++)
        cin>>a[i];
    cout<<"Case "<<++cas<<": "<<dfs(1,n,0)<<endl;
}
signed main()
{
    
    
    int t;
    cin>>t;
    while(t--)
        solve();
    return 0;
}

Guess you like

Origin blog.csdn.net/Joker_He/article/details/109732439