BZOJ 4580: [Usaco2016 Open]248
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的更多相关文章
- 4580: [Usaco2016 Open]248
Description Bessie likes downloading games to play on her cell phone, even though she does find the ...
- BZOJ 4576: [Usaco2016 Open]262144
Description 一个序列,每次可以将两个相同的数合成一个数,价值+1,求最后最大价值 \(n \leqslant 262144\) Sol DP. 这道题是 BZOJ 4580: [Usaco ...
- bzoj4580: [Usaco2016 Open]248(区间dp)
4580: [Usaco2016 Open]248 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 255 Solved: 204[Submit][S ...
- 【BZOJ 4580】【Usaco2016 Open】248
http://www.lydsy.com/JudgeOnline/problem.php?id=4580 区间dp,f(i,j)表示区间[i,j]全部合成一个数,这个数是多少. 可以归纳证明[i,j] ...
- BZOJ 4742: [Usaco2016 Dec]Team Building
4742: [Usaco2016 Dec]Team Building Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 21 Solved: 16[Su ...
- 【bzoj4580】[Usaco2016 Open]248 区间dp
题目描述 Bessie likes downloading games to play on her cell phone, even though she does find the small t ...
- bzoj 4506: [Usaco2016 Jan]Fort Moo
4506: [Usaco2016 Jan]Fort Moo Description Bessie is building a fort with her friend Elsie. Like any ...
- bzoj 4412: [Usaco2016 Feb]Circular Barn
4412: [Usaco2016 Feb]Circular Barn Description 有一个N个点的环,相邻两个点距离是1.点顺时针标号为1..N.每一个点有ci头牛,保证∑ci=N.每头牛都 ...
- BZOJ4580: [Usaco2016 Open]248
n<=248个数字,可以进行这样的操作:将相邻两个相同的数字合并成这个数字+1,求最大能合成多少. f(i,j)--区间i到j能合成的最大值,f(i,j)=max(f(i,k)+1),f(i,k ...
随机推荐
- haproxy安装
最近一直在整理之前用过的东西,现在到haproxy了,安装如下: tar xf haproxy-1.4.27.tar.gz cd haproxy-1.4.27 make TARGET=linux26 ...
- Unity Particle System Sorting Order
http://answers.unity3d.com/questions/577288/particle-system-rendering-behind-sprites.html
- Python基础之【第一篇】
Python简介: python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语 ...
- Selenium 代码收集
[转载]使用Selenium2测试含有iframe的Ajax网页 原文地址:http://www.cnblogs.com/hexin0614/archive/2012/03/24/2415670.h ...
- ASP.NET MVC 表单提交List到Controller
1.实体结构: 2.View代码: 3.controller代码: 参考链接:http://shiyousan.com/post/635383025861004585
- 关于MySQL的SLEEP(N)函数
都知道通过在MySQL中执行select sleep(N)可以让此语句运行N秒钟: ? 1 2 3 4 5 6 7 mysql> select sleep(1); +----------+ | ...
- addEventListener和on的区别
为什么需要addEventListener? 先来看一个片段: html代码 <div id="box">追梦子</div> 用on的代码 1 window ...
- jqueryui / accordion的用法记录
jqueryui 的 widget 中包含了基本上我们都需要的ui组件, 除了那个unslider. 参考地址是: www.jqueryui.com. 要能够看懂/并使用/ 完全掌握的话, 就要使用其 ...
- 使用jQuery的Scrollify插件实现鼠标滚轮或者手势滑动到页面下一节点部分
有时我们需要做一个单页面介绍产品特性,而单页面内容非常多且页面非常长,为了快速定位到产品特性节点,我们使用js侦听用户滚轮事件,当用户触发滚轮滑动或者使用手势触屏滑动时,即可定位到相应的节点.一款jQ ...
- rose中设置组合
1.在工具栏选择 "association" (关联).2.编辑role B 属性 open specital , role B detail ,multiplic(重数) 为1, ...