zoukankan      html  css  js  c++  java
  • CCF-CSP题解 201612-4 压缩编码

    (CSP)也考(DP)的嘛...想了两小时贪心的我在宿舍凌乱...

    还是智障+老花啊...这不是一道区间合并裸题嘛...石子合并啊...

    再看看这(3s)的时限,(O(n^3))都够了,何必想那么久贪心呢...

    #include <bits/stdc++.h>
    typedef long long LL;
    const int maxn = 1000;
    
    using namespace std;
    
    LL a[maxn + 10];
    
    LL dp[maxn + 10][maxn + 10];
    
    int main()
    {
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
            scanf("%lld", a + i);
    
        for (int i = 2; i <= n; i++)
            a[i] += a[i - 1];
    
        memset(dp, 0x3f, sizeof(dp));
    
        for (int i = 1; i <= n; i++)
            dp[i][i] = 0;
    
        for (int len = 2; len <= n; len++)
        {
            for (int l = 1; l + len - 1 <= n; l++)
            {
                int r = l + len - 1;
                for (int mid = l; mid <= r - 1; mid++)
                {
                    dp[l][r] = min(dp[l][r], dp[l][mid] + dp[mid + 1][r]);
                }
                dp[l][r] += a[r] - a[l - 1];
            }
        }
    
        if (n == 1)
            printf("%lld
    ", a[1]);
        else
            printf("%lld
    ", dp[1][n]);
    
        return 0;
    }
    
  • 相关阅读:
    MySQL远程登陆
    一键安装LAMP wordpress
    编译安装httpd2.4.46
    RDB转化AOF 花式删库
    3周作业
    2周作业
    1周作业
    预习第三周作业
    预习第二周作业
    预习第一周作业
  • 原文地址:https://www.cnblogs.com/acboyty/p/11387826.html
Copyright © 2011-2022 走看看