【动态规划DP】[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 sequence of N positive integers (2≤N≤248), each in the range 1..40 . In one move, Bessie cantake two adjacent numbers with equal values and replace them a singlenumber of value one greater (e.g., she might replace two adjacent 7swith an 8). The goal is to maximize the value of the largest numberpresent in the sequence at the end of the game. Please help Bessiescore as highly as possible!
给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-40),问最大能合出多少。注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3。
分析
区间DP,我们按照惯例先定义状态
F[i][j]表示i到j的最大的答案。
那么根据题意,方程为F[i][j]=Max(F[i][j],F[i][k]+1) (F[i][k]=F[k+1][j])
AC代码
#include <bits/stdc++.h>
using namespace std;
#define ms(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const int maxn=255;
int n;
int a[maxn];
int f[maxn][maxn];
inline int read(){
int X=0,w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
int main(int argc,char* argv[]){
n=read();
for (int i=1;i<=n;i++) a[i]=read(),f[i][i]=a[i];
int ans=0;
for (int i=n;i>=1;i--) {
for (int j=i;j<=n;j++) {
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);
ans=max(ans,f[i][j]);
}
}
}
printf("%d\n",ans);
return 0;
}
【动态规划DP】[USACO16OPEN]248的更多相关文章
- 「区间DP」「洛谷PP3146 」[USACO16OPEN]248 G
[USACO16OPEN]248 G 题目: 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...
- [USACO16OPEN]248 G——区间dp
[USACO16OPEN]248 G 题目描述 Bessie likes downloading games to play on her cell phone, even though she do ...
- 洛谷P3146 [USACO16OPEN]248
P3146 [USACO16OPEN]248 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...
- 洛谷 P3146 [USACO16OPEN]248
P3146 [USACO16OPEN]248 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...
- P3146 [USACO16OPEN]248
P3146 [USACO16OPEN]248 题解 第一道自己码出的区间DP快庆祝一哈 2048 每次可以合并任意相邻的两个数字,得到的不是翻倍而是+1 dp[L][R] 区间 L~R 合并结果 然后 ...
- 动态规划dp
一.概念:动态规划dp:是一种分阶段求解决策问题的数学思想. 总结起来就一句话:大事化小,小事化了 二.例子 1.走台阶问题 F(10):10级台阶的走法数量 所以:F(10)=F(9)+F(8) F ...
- 算法-动态规划DP小记
算法-动态规划DP小记 动态规划算法是一种比较灵活的算法,针对具体的问题要具体分析,其宗旨就是要找出要解决问题的状态,然后逆向转化为求解子问题,最终回到已知的初始态,然后再顺序累计各个子问题的解从而得 ...
- 又一道区间DP的题 -- P3146 [USACO16OPEN]248
https://www.luogu.org/problemnew/show/P3146 一道区间dp的题,以区间长度为阶段; 但由于要处理相邻的问题,就变得有点麻烦; 最开始想了一个我知道有漏洞的方程 ...
- P3146 [USACO16OPEN]248 (区间DP)
题目描述 给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-40),问最大能合出多少.注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3. 这道题的思路: 状态: ...
随机推荐
- MySQL必知必会笔记-Mysql基本操作
Mysql基本操作 mysql的基本操作包括增.删.改.查,本书中前三章简单的介绍MySQL为何物,查是mysql中非常重要的功能,4-6章展示了mysql的查(查询--select)的简单实现,my ...
- Headline 项目总结中
目录 1.项目准备 1.1 rem适配 1.2 通用样式CSS 1.3删除测试代码 1.4Git托管 2.login页面 2.1 页面布局和表单校验 2.2login页的接口抽取 2.5.loadin ...
- 【NX二次开发】Block UI 选择小平面区域
属性说明 属性 类型 描述 常规 BlockID String 控件ID Enable Logical 是否可操作 Group ...
- 排查bug:竟然是同事把Redis用成这鬼样子,坑了我
首先说下问题现象:内网sandbox环境API持续1周出现应用卡死,所有api无响应现象 刚开始当测试抱怨环境响应慢的时候 ,我们重启一下应用,应用恢复正常,于是没做处理.但是后来问题出现频率越来越频 ...
- Golang学习(用代码来学习) - 第一篇
package main import ( "fmt" "time" "unsafe" ) //示例代码 var isActive bool ...
- C#异步迭代 IAsyncEnumerable 应用
最近用WPF做金税盘开发中有这样一个需求,批量开票每次开票都需要连接一次金税盘. 比如我有发票 a, b ,c ,d e 这五张发票,每次开具发票都需要调用金税盘底层,才能正常开票. 首先,尝试写第一 ...
- 计算机网络参考模型与5G协议
计算机网络参考模型与5G协议 目录 一.分层思想 1.1·2:分层思想概念 1.2.优点 二.OSI七层参考模型 三.TCP/IP协议族 3.1.TCP/IP协议族的组成 3.2.OSI模型与TCP/ ...
- 熬夜总结vue3中setUp函数的2个参数详解
1.setUp函数的第1个参数props setup(props,context){} 第一个参数props: props是一个对象,包含父组件传递给子组件的所有数据. 在子组件中使用props进行接 ...
- Java中对象初始化过程
Java为对象初始化提供了多种选项. 当new一个对象的时候,对象初始化开始: 1.首先,JVM加载类(只加载一次,所以,即使多次new对象,下面的代码也只会在第一次new的时候执行一次),此时, 静 ...
- Java核心API-日期时间
java.util.Date Date类用来表示时间点. 时间是用距离一个固定时间点的毫秒数表示的,这个时间点就是纪元. UTC时间是为表示这个纪元的科学标准时间,从1970年1月1日0时开始.另一种 ...