Description

一个序列,每次可以把相邻的两个数合为一个,价值+1,求最后的最大价值.

Sol

区间DP.

\(f[i][j]\) 表示 \(i-j\) 中合成一个数字为多少,转移就是枚举断点,断点两边的价值一样,就合并.

复杂度 \(O(n^3)\)

Code

/**************************************************************
Problem: 4580
User: BeiYu
Language: C++
Result: Accepted
Time:308 ms
Memory:1544 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; const int N = 255;
const int M = 50; int n,ans;
int a[N];
int f[N][N]; inline int in(int x=0){ scanf("%d",&x);return x; }
int DFS(int l,int r){
if(l == r) return f[l][r]=a[l];
if(~f[l][r]) return f[l][r];
f[l][r]=0;
for(int i=l;i<r;i++){
int x=DFS(l,i),y=DFS(i+1,r);
if(x!=0 && y!=0 && x == y) f[l][r]=max(f[l][r],x+1);
}ans=max(ans,f[l][r]);return f[l][r];
}
int main(){
n=in();
for(int i=1;i<=n;i++) a[i]=in(),ans=max(ans,a[i]); memset(f,0xff,sizeof(f)); for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) DFS(i,j); // for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) cout<<i<<" "<<j<<" "<<DFS(i,j)<<endl; cout<<ans<<endl;
return 0;
}

  

BZOJ 4580: [Usaco2016 Open]248的更多相关文章

  1. 4580: [Usaco2016 Open]248

    Description Bessie likes downloading games to play on her cell phone, even though she does find the ...

  2. BZOJ 4576: [Usaco2016 Open]262144

    Description 一个序列,每次可以将两个相同的数合成一个数,价值+1,求最后最大价值 \(n \leqslant 262144\) Sol DP. 这道题是 BZOJ 4580: [Usaco ...

  3. bzoj4580: [Usaco2016 Open]248(区间dp)

    4580: [Usaco2016 Open]248 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 255  Solved: 204[Submit][S ...

  4. 【BZOJ 4580】【Usaco2016 Open】248

    http://www.lydsy.com/JudgeOnline/problem.php?id=4580 区间dp,f(i,j)表示区间[i,j]全部合成一个数,这个数是多少. 可以归纳证明[i,j] ...

  5. BZOJ 4742: [Usaco2016 Dec]Team Building

    4742: [Usaco2016 Dec]Team Building Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 21  Solved: 16[Su ...

  6. 【bzoj4580】[Usaco2016 Open]248 区间dp

    题目描述 Bessie likes downloading games to play on her cell phone, even though she does find the small t ...

  7. bzoj 4506: [Usaco2016 Jan]Fort Moo

    4506: [Usaco2016 Jan]Fort Moo Description Bessie is building a fort with her friend Elsie. Like any ...

  8. bzoj 4412: [Usaco2016 Feb]Circular Barn

    4412: [Usaco2016 Feb]Circular Barn Description 有一个N个点的环,相邻两个点距离是1.点顺时针标号为1..N.每一个点有ci头牛,保证∑ci=N.每头牛都 ...

  9. BZOJ4580: [Usaco2016 Open]248

    n<=248个数字,可以进行这样的操作:将相邻两个相同的数字合并成这个数字+1,求最大能合成多少. f(i,j)--区间i到j能合成的最大值,f(i,j)=max(f(i,k)+1),f(i,k ...

随机推荐

  1. /MT、/MD编译选项,以及可能引起在不同堆中申请、释放内存的问题

    一.MD(d).MT(d)编译选项的区别 1.编译选项的位置 以VS2005为例,这样子打开: 1)         打开项目的Property Pages对话框 2)         点击左侧C/C ...

  2. C#中导入Win32 API函数

    C#中导入Win32 API的方法: 1.引用命名空间 using System.Net.Security; using System.Runtime.InteropServices; 2. [Dll ...

  3. MBProgressHUD上传照片进度提示

    第一步,控制器先来个属性 @property (strong, nonatomic) MBProgressHUD *HUD; 第二步,显示与隐藏的调用方法 - (void)hudTipWillShow ...

  4. 在 docker中 运行 mono /jexus server 并部署asp.net mvc站点

    http://linuxdot.net/bbsfile-3988 1.  安装 docker:      // docker 1.7 新版 安装非常容易,理论上说,在主流的任意linux发行版上都可以 ...

  5. tmpfs——Linux的一种虚拟内存文件系统

    虚拟内核文件系统(VirtualKernel File Systems),是指那些是由内核产生但不存在于硬盘上(存在于内存中)的文件系统.例如 1.proc proc文件系统为操作系统本身和应用程序之 ...

  6. oracle union 注入工具

    '***********************************************************************************************'ora ...

  7. python快排算法

    通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列. ...

  8. C语言打乱一组数字顺序

    #include<stdio.h> #include<stdlib.h> #include<math.h> #include<time.h> int m ...

  9. 【8-21】java学习笔记03

    内部类(静态内部类&非静态内部类) 静态外部类成员方法(如main方法)不能直接访问内部类,但是可以通过外部类的方法,在其中创建内部类实例对象,间接使用: 非静态内部类可以直接访问外部类的私有 ...

  10. Java实验2-数据库编程

    目标:掌握Java数据库编程 内容: 学生选课系统包括如下数据库表 学生表:Student(Sno,Sname,Ssex,Sage,Sdept) 课程表:Course(Cno,Cname,Ccredi ...