• poj1390 (区间dp)


    Description

    Some of you may have played a game called ‘Blocks’. There are n blocks in a row, each box has a color. Here is an example: Gold, Silver, Silver, Silver, Silver, Bronze, Bronze, Bronze, Gold.

    The corresponding picture will be as shown below:
    这里写图片描述

    Figure 1

    If some adjacent boxes are all of the same color, and both the box to its left(if it exists) and its right(if it exists) are of some other color, we call it a ‘box segment’. There are 4 box segments. That is: gold, silver, bronze, gold. There are 1, 4, 3, 1 box(es) in the segments respectively.


    Every time, you can click a box, then the whole segment containing that box DISAPPEARS. If that segment is composed of k boxes, you will get k*k points. for example, if you click on a silver box, the silver segment disappears, you got 4*4=16 points.


    Now let’s look at the picture below:
    这里写图片描述

    Figure 2


    The first one is OPTIMAL.


    Find the highest score you can get, given an initial state of this game.

    Input

    The first line contains the number of tests t(1<=t<=15). Each case contains two lines. The first line contains an integer n(1<=n<=200), the number of boxes. The second line contains n integers, representing the colors of each box. The integers are in the range 1~n.


    Output

    For each test case, print the case number and the highest possible score.

    Sample Input

    2 
    9
    1 2 2 2 2 3 3 3 1
    1
    1

    Sample Output

    Case 1: 29 
    Case 2: 1

    Submit

    分析:
    显然,一样颜色的一段区域要一起消,这样贡献才最大
    我们可以把一段颜色相同的区域缩成一个点
    (后来证明没必要)

    我一开始的方程是这样的:
    f[i][j] 表示消掉i~j的最大值
    如果i和j的颜色一样,那么f[i][j]=f[i+1][j-1]+(len[i]+len[j])^2
    否则f[i][j]=max{f[i][k]+f[k+1][j]}

    但是可能出现这样的状态:
    11221113331
    很显然,先消掉2和3,之后一起消掉1才最优
    但是要是按照我的转移状态,
    只会是消掉22111333之后再消掉最边上的1

    所以我们就要改变一下转移的状态:
    f[i][j][k] i~j消到只剩下k个,

    规定这k个块和i,j的颜色一样

    g[i][j] i~j全部消掉的最大值

    f[i][j][k]=max{f[i][l][k-1]+g[l+1][j-1]}
    表示把l+1~j-1段完全消除,使j与前面颜色相同的k-1个合并,

    注意枚举的l必须与i,j颜色相同

    **这里写图片描述**

    g[i][j]=max(g[i][l]+g[l+1][j],f[i][j][k]+k^2)
    可以将i~j从中间断开分别消除,也可以将i~j剩下的k个一次消除

    tip

    g[i][j]=g[i][l]+g[l+1][j]
    不牵扯到k,所以可以单独拿出来转移

    重申原则

    一定要从稳定状态转移

    我写出来之后发现跑出来的答案比标准答案大
    这时候我发现题解上有一句话
    memset(f,128,sizeof(f)); //初始值为-INF
    我一开始是把所有f都附成了0
    改过来之后就好了

    这里写代码片
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    
    using namespace std;
    
    int n;
    int a[202],tot;
    int f[202][202][202],g[202][202];
    
    int doit()
    {
        int i,j,k,l;
        memset(f,128,sizeof(f));
        memset(g,0,sizeof(g));
        for (i=1;i<=n;i++) g[i][i]=1,f[i][i][0]=0,f[i][i][1]=0;
        for (i=n-1;i>=1;i--)
            for (j=i+1;j<=n;j++)
            {
                if (a[i]==a[j])   //col must be same
                for (k=1;k<=j-i+1;k++)
                {
                    for (l=i;l<j;l++)
                        if (a[i]==a[l])  //col must be same
                            f[i][j][k]=max(f[i][j][k],f[i][l][k-1]+g[l+1][j-1]);
                    //f[i][j][k]已经固定下来了,才能进行个g[i][j]的转移 
                    g[i][j]=max(g[i][j],f[i][j][k]+k*k);
                }
                for (k=i;k<j;k++) g[i][j]=max(g[i][j],g[i][k]+g[k+1][j]);
            }
        return g[1][n];
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        int TT=0;
        while (++TT<=T)
        {
            int x;
            scanf("%d",&n);
            for (int i=1;i<=n;i++) scanf("%d",&a[i]);
            printf("Case %d: %d
    ",TT,doit());
        }
        return 0;
    }
  • 相关阅读:
    《安富莱嵌入式周报》第262期:2022.04.182022.04.24
    第4期ThreadX视频教程:单片机动态APP加载玩法,像手机电脑一样加载卸载多个应用软件(20220501)
    H7TOOL发布固件V2.16, 脱机烧录增加汇顶,普冉,ESMT(台湾晶豪) SPIFLASH等支持(20220508)
    MDK5.37发布,围绕CortexM85更新了一大堆东西,MDK AC5正式退出历史舞台(20220504)
    【BSP视频教程】STM32H7视频教程第14期:超干货,MPU和Cache实战,一张图了解所有经典配置案例,争取人人都可以玩溜(20220508)
    地表最强CortexM85发布
    【Git】安装及配置
    限制input 只能输入正整数小数点一位 两位(指令方法)
    往对象中新增一个参数(之前没有定义)
    js 删除对象里的某个属性
  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673201.html
Copyright © 2020-2023  润新知