luogu P3146 [USACO16OPEN]248

基础区间的dp题

状态很容易得出:dp[i][j]表示区间i——j可以合成的最大数。

状态转移方程很显然:if (dp[i][k]==dp[k+1][j]) dp[i][j]=max(dp[i][j],dp[i][k]+1)

那么只需要先枚举结点,在以该结点为中心向两边枚举长度即可,但一次循环不能保证所有数据都正确,因为后得出的区间可能更新前得出的区间。

为了解决问题,跑多次循环

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int n,dp[250][250];
int main()
{
    cin>>n;
    int ans=0;
    for(int i=1;i<=n;i++)
    {
       cin>>dp[i][i];
       ans=max(ans,dp[i][i]);
    }
    for (int i=n-1;i>=1;i--)
        for (int j=i+1;j<=n;j++){
            for (int k=i;k<j;k++)
                if (dp[i][k]==dp[k+1][j])
                    dp[i][j]=max(dp[i][j],dp[i][k]+1);
            ans=max(ans,dp[i][j]);
        }
    cout<<ans;
    return 0;
}

原文地址:https://www.cnblogs.com/charlesss/p/10336676.html

时间: 2024-11-09 04:04:29

luogu P3146 [USACO16OPEN]248的相关文章

洛谷 P3146 [USACO16OPEN]248

P3146 [USACO16OPEN]248 题目描述 Bessie likes downloading games to play on her cell phone, even though she doesfind the small touch screen rather cumbersome to use with her large hooves. She is particularly intrigued by the current game she is playing.The

洛谷P3146 [USACO16OPEN]248

题目描述 Bessie likes downloading games to play on her cell phone, even though she doesfind the small touch screen rather cumbersome to use with her large hooves. She is particularly intrigued by the current game she is playing.The game starts with a seq

P3146 [USACO16OPEN]248

实际上1*n的地图..不就是线性的么.. 1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 using namespace std; 8 int n,m,ans; 9 int f[270000][66],a[270000]; 10 int ma

[luoguP3146] [USACO16OPEN]248(区间DP)

传送门 f[i][j]表示区间 i-j 合并的最大值 转移: 若f[i][k] && f[k+1][j] && f[i][k] == f[k+1][j] --> f[i][j] = max(f[i][k]+1,f[i][j]) 但要注意, 若f[i][k]!=f[k+1][j],那么无法进行转移 代码 #include <cstdio> #include <iostream> #define max(x, y) ((x) > (y) ?

luogu P3147 [USACO16OPEN]262144

题目描述 Bessie likes downloading games to play on her cell phone, even though she doesfind the small touch screen rather cumbersome to use with her large hooves. She is particularly intrigued by the current game she is playing.The game starts with a seq

luogu P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver解题报告

题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to temporarily close down his farm to save money in the meantime. The farm consists of NN barns connected with MM bidirectional paths between some pairs of

luogu3146 [USACO16OPEN]248

细节被坑惨 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1e6+5; 4 const int INF=1e9+7; 5 int n,ans,dp[305][305]; 6 template <class t>void red(t &x) 7 { 8 x=0; 9 int w=1; 10 char ch=getchar(); 11 while(ch<'0'||ch>'9') 1

Luogu P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

又是一个学数据结构学傻了的人 才不会承认是看到Splay,觉得可以写平衡树才进来的呢 Description: 给出一个序列,问排序后,选取两个区间,使其没有重合部分,且每个区间右端点减去左端点不大于k,求这两个区间长度之和的最大值. 前置技能:FHQ-Treap.线段树 (不会的出门百度) Solution: 看数据范围,5e4,很好,O(nlogn)完全可以水过去.那么我们可以放心的考虑FHQ了.因为要最优,所以我们要找到每一个点,在有序序列中对应的满足题目条件的区间的大小,这就可以用FHQ

USACO16OPEN 248

题目传送门 比较坑的区间\(DP\) 定义f[i][j]为将i~j这段区间全都合并为一个数的最大值 于是我很自然的就想到了这样转移: f[i][j] = max(f[i][j], max(f[i][k], f[k+1][j])); if(f[i][k] == f[k+1][j]) f[i][j] = max(f[i][j], f[i][k] + 1); 但是,因为f[i][k]和f[k+1][j]的最大值可能并不相邻,所以第一个转移是不对的,应该只有 if(f[i][k] == f[k+1][j