Array Shrinking
Array Shrinking
You are given an array a1,a2,…,ana1,a2,…,ana1,a2,…,an. You can perform the following operation any number of times:
- Choose a pair of two neighboring equal elements ai=ai+1ai=ai+1ai=ai+1 (if there is at least one such pair).
- Replace them by one element with value ai+1ai+1ai+1.
After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array a you can get?
Input
The first line contains the single integer n(1≤n≤500)n (1≤n≤500)n(1≤n≤500) — the initial length of the array aaa.
The second line contains n integers a1,a2,…,an(1≤ai≤1000)a1,a2,…,an (1≤ai≤1000)a1,a2,…,an(1≤ai≤1000)— the initial array aaa.
Output
Print the only integer — the minimum possible length you can get after performing the operation described above any number of times.
Examples
input
5
4 3 2 2 3
output
2
input
7
3 3 4 4 4 3 3
output
2
input
3
1 3 5
output
3
input
1
1000
output
1
Note
In the first test, this is one of the optimal sequences of operations: 43223→4333→443→53.4 3 2 2 3 → 4 3 3 3 → 4 4 3 → 5 3.43223→4333→443→53.
In the second test, this is one of the optimal sequences of operations: 3344433→444433→44444→5444→554→64.3 3 4 4 4 3 3 → 4 4 4 4 3 3 → 4 4 4 4 4 → 5 4 4 4 → 5 5 4 → 6 4.3344433→444433→44444→5444→554→64.
In the third and fourth tests, you can’t perform the operation at all.
思路
题意:给一个数列,其中相邻且相等的数字a可合并为一个a+1的数字,求可以合并得到最小的数列长度。
分析:首先看到数组长度为500范围,丛贪心角度来看要考虑先后顺序,所以我们可以考虑dp(玄学dp,多注意范围),dp一个区间的长度和大小,通过枚举来求解~具体如下:
- 令dp[i][j]dp[i][j]dp[i][j]为区间[i,j][i,j][i,j]的长度,s[i][j]s[i][j]s[i][j]为区间[i,j][i,j][i,j]的数字大小
- 对于两个区间[a,b][a,b][a,b]和[b,c][b,c][b,c],如果dp[a][b]=dp[b][c]=1dp[a][b]=dp[b][c]=1dp[a][b]=dp[b][c]=1且s[a][b]=s[b][c]s[a][b]=s[b][c]s[a][b]=s[b][c],说明可以合并
- 按照思路枚举区间长度,区间起点和区间终点即可~
代码如下:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int mx=510;
int dp[mx][mx];//区间i到j的长度
int s[mx][mx];//区间i到j的数字大小
int n;int main()
{scanf("%d",&n);for(int i=1;i<=n;i++){for(int j=i;j<=n;j++)dp[i][j]=j-i+1;}memset(s,0,sizeof(s));for(int i=1;i<=n;i++)scanf("%d",&s[i][i]);for(int l=1;l<=n;l++)//枚举长度 {for(int i=1;i+l<=n;i++)//枚举起点{int j=i+l;//后面区间的终点for(int k=i;k<j;k++)//枚举前面区间的终点(后面区间的终点){dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);//及时更新长度if(dp[i][k]==1&&dp[k+1][j]==1&&s[i][k]==s[k+1][j]){dp[i][j]=1;s[i][j]=s[i][k]+1;}}}}printf("%d\n",dp[1][n]);return 0;
}
Array Shrinking相关推荐
- CodeForces - 1312E Array Shrinking(区间dp)(通俗易懂)
CodeForces - 1312E Array Shrinking(区间dp) 题目链接: 没做出来,看了一下别人的题解,才A掉.但网上没发现一篇讲得比较易懂的题解,所以就准备写一篇再加上我自己的理 ...
- CodeForces - 1312E Array Shrinking(区间dp)
题目链接:点击查看 题目大意:给出 n 个数,现在可执行的操作是: 找到相邻且数值相等的两个数,即 abs( i - j ) == 1 && a[ i ] == a[ j ] 使得两个 ...
- codeforces E. Array Shrinking
题目 题意: 给你一组序列,序列中相邻的两个一样的值可以合并成一个值+1的数,问最后剩下的最少的元素有多少个. 思路: 本来我的思路是用栈或者队列的方式去合并,然后输出元素的个数,但是发现假如1 1 ...
- codeforces 1312E. Array Shrinking
https://codeforces.com/problemset/problem/1312/E 傻逼DP又写了快40分钟,迟早药丸 这题之前总是按传统的区间dp想维护i,j的最多合并次数,怎么维护左 ...
- [CF1312E]Array Shrinking
题目 传送门 to luogu 题意概要 一维线段上的 2048 \tt 2048 2048 游戏:每次两个相邻 x x x 可以合并成 x + 1 x+1 x+1 .求最少能剩下几个数字. 数据范围 ...
- 老男孩上海校区Python面试题
python面试题 第一章:python基础 数据类型: 1 字典: 1.1 现有字典 dict={'a':24,'g':52,'i':12,'k':33}请按字典中的 value 值进行排序? 1. ...
- leetcode 421. Maximum XOR of Two Numbers in an Array
Given a non-empty array of numbers, a0, a1, a2, - , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- php recordarray,Array 数组 - [ php中文手册 ] - 在线原生手册 - php中文网
用户评论: [#1] florenxe [2015-10-07 18:53:45] //a nice little way to print leap years using array for ($ ...
- NumPy — 创建全零、全1、空、arange 数组,array 对象类型,astype 转换数据类型,数组和标量以及数组之间的运算,NumPy 数组共享内存
NumPy 简介 一个用 python 实现的科学计算包.包括: 1.一个强大的 N 维数组对象 Array : 2.比较成熟的(广播)函数库: 3.用于整合 C/C++ 和 Fortran 代码的工 ...
- array.array python yhzf
关于array: Python 本身没有数组这个说法, 有的就是list和tuple, list就具有其他语言中的数组特性. 至于list和tuple的区别,在于list可以在运行时修改内容和大小,t ...
最新文章
- windows下mysql开启慢查询
- java中jtextfield_java中的JTextField
- 网络推广外包中目标用户习惯对企业网站设计与开发有重要影响
- memcached全面剖析--3.memcached的删除机制和发展方向
- visio图中包含公式,插入word后公式模糊终极解决办法
- python正则表达式去除逗号_[宜配屋]听图阁 - python正则表达式去掉数字中的逗号(python正则匹配逗号)...
- DB pivot unpivot
- Android软键盘问题
- Deadline调度器之(二):细节和使用方法
- 深入解读Linux进程调度系列(5)——调度的入口
- 记录 UiPath 学习中遇到的一些问题以及解决办法
- 写作技巧~100段作文排比句(81-100段),考试一定用得上,赶紧收藏!
- 前端实现 导出图片,导出PDF(截图原理)
- 皮肤测试小软件有哪些,【皮肤测试小程序】皮肤测试小程序有什么功能呢?
- 微信扫描打开app下载提示用户打开浏览器进行下载
- html图片撑开盒子,css背景图撑开盒子高度
- git commit后回退方法
- 微信公众号接入H5支付
- node.js事件驱动的非阻塞 I/O模型理解
- dnf mysql_CentOS7使用dnf安装mysql