【动态规划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. 这道题的思路: 状态: ...
随机推荐
- fiddler选项卡-Filters(过滤)
Filter filter的意思是过滤,在fiddler中,它可以用来过滤请求,使得session列表能够更加精准的展现抓到的数据流,而不是杂乱的一堆. 1.filter的界面 2.界面详解 1.Us ...
- redis淘汰+过期双向保证高可用 | redis 为什么那么快?
前言 redis和数据相比除了他们的结构型颠覆以外!还有他们存储位置也是不相同.传统数据库将数据存储在硬盘上每次数据操作都需要IO而Redis是将数据存储在内存上的.这里稍微解释下IO是啥意思.IO就 ...
- Windows下安装kubectl及Node和Pod操作常用命令
kubernetes通过kube-apiserver作为整个集群管理的入口.Apiserver是整个集群的主管理节点,用户通过Apiserver配置和组织集群,同时集群中各个节点同etcd存储的交互也 ...
- React 开发环境准备
1. 使用reactjs,一般有以下两种方式: (1)通过script标签引入reactjs.这种方式不推荐使用,如果我们的项目比较大,就需要对项目进行拆分,于是页面就需要通过script标签引入很多 ...
- 『心善渊』Selenium3.0基础 — 14、Selenium对单选和多选按钮的操作
目录 1.页面中的单选按钮和多选按钮 2.判断按钮是否选中is_selected() 3.单选按钮的操作 4.多选按钮的操作 5.选择部分多选按钮的操作 1.页面中的单选按钮和多选按钮 页面中的单选按 ...
- unity的安装,配置,及问题
下载unity 在官网下载unity unity有三个版本,个人版免费,pro和专业版收费. 个人版 在导出exe文件时不能去掉水印片头.其他版本可以. 地址[https://store.unity. ...
- 乘风破浪,.Net Core遇见MAUI(.NET Multi-platform App UI),进击现代化跨设备应用框架
什么是MAUI https://github.com/dotnet/maui .NET Multi-platform App UI (MAUI) 的前身是Xamarin.Forms(适用于Androi ...
- To_Heart—题解——AT2165
这是一篇解题报告 首先,看到标签,考虑二分答案. 我们二分答案(即塔顶的值),把大于或等于这个值的变为1,否则变为0. 很容易发现,如果塔顶的答案是1,那么就说明值可以更大,否则相反. 复制一波样例 ...
- js 正则表达式 验证数字或字母
let reg= /^(^[0-9]*$)|(^[A-Za-z]+$)/ /*reg= /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]$/*/ if(!reg.test( ...
- 企业如何通过CRM系统使销售周期缩短
企业为什么要缩短销售周期?因为这意味着可以节约更多开支,从而达到企业利润最大化.但是有不少企业尤其是B2B行业,销售周期都在三个月以上.通过调查发现,很多企业在客户信息和销售管道上缺乏管理和策略.Zo ...