P3146 [USACO16OPEN]248 G

简介: P3146 [USACO16OPEN]248 G

文章目录

  • P3146 [USACO16OPEN]248 G
  • AC代码


P3146 [USACO16OPEN]248 G

本题链接:P3146 [USACO16OPEN]248 G

本博客给出本题截图

5.png

AC代码

代码解释:

这里的状态转移方程定义有一些特别,我们定义f[i][j]表示将[i, j]范围内完全合并后的分数最大值

所以我们带入到区间dp后的思路为,只有当我们的f[i][k] == f[k + 1][j]的时候,才可以进行状态转移,我们用res表示我们的最终答案,为什么最终答案不是f[1][n]?答:由于我们定义f[i][j]为完全合并,但是显然从[1, n]不是全部都能合并的,如果不能完全合并,那么显然会有f[1][n] == 0,故f[1, n]不是最终的解,而是需要不断的去更新res


代码:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 250;
int f[N][N];
int n, res;
int s[N];
int main()
{
    cin >>n;
    for (int i = 1; i <= n; i ++ )
    {
        cin >> s[i];
        f[i][i] = s[i];
    }
    for (int len = 2; len <= n; len ++ )
        for (int i = 1; i + len - 1 <= n; i ++ )
        {
            int j = i + len - 1;
            for (int k = i; k < j; k ++ )
            {
                if (f[i][k] == f[k + 1][j]) 
                {
                    f[i][j] = max(f[i][j], f[i][k] + 1);
                    res = max(res, f[i][j]);
                }
            }
        }
    cout << res << endl;
    return 0;
}




目录
相关文章
|
9月前
USACO1.3 修理牛棚
USACO1.3 修理牛棚
|
算法
[USACO 2007 Jan S]Protecting the Flowers
[USACO 2007 Jan S]Protecting the Flowers
|
索引
[usaco]3.1.5 stamps
<p>Stamps</p> <p>Given a set of N stamp values (e.g., {1 cent, 3 cents}) and an upper limit K to the number of stamps that can fit on an envelope, calculate the largest unbroken list of postages
1708 0
|
人工智能
USACO/friday
Friday the Thirteenth 黑色星期五 描述 13号又是一个星期五。13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数。给出N年的 一个周期,要求计算1900年1月1日至1900+N-1年12月31日中十三号落在周一到周日的次数,N为正整数且不大于400.
882 0
[usaco] 4.1.4 PROB Cryptcowgraphy
<p>Cryptcowgraphy<br> Brian Dean <br> The cows of Farmer Brown and Farmer John are planning a coordinated escape from their respective farms and have devised a method of encryption to protect th
1106 0
[usaco] Number Triangles
<center><h1>Number Triangles</h1></center> <p>Consider the number triangle shown below. Write a program that calculates the highest sum of numbers that can be passed on a route that starts at the to
1671 0
uva 11078 Open Credit System
点击打开链接uva11078 水题 对于一个确定的j来说,要求num[i]是最大的,所以我们枚举j然后维护最大的num[i],然后同时求最大的ans即可 #include #include #include #include usin...
628 0
|
人工智能 算法
|
人工智能
uva11078Open Credit System
题意:给定n个整数,求Ai-Aj的最大值(i
582 0
|
算法 流计算
CTU Open Contest 2019 部分题解
CTU Open Contest 2019 部分题解
88 0

热门文章

最新文章