1312E Array Shrinking

20 篇文章 0 订阅
1 篇文章 0 订阅

1312E Array Shrinking
题意:
给你 n ( 1 ≤ n ≤ 500 ) n(1\leq n\leq 500) n(1n500) 长的数组,你可以任意次操作使得满足 a [ n ] = a [ n + 1 ] a[n]=a[n+1] a[n]=a[n+1] 的两个数被替换为 a [ n ] + 1 a[n]+1 a[n]+1 ,求数组最小长为多少。
思路:
区间 d p dp dp
观察 n n n 的范围,可知这道题可以采用 O ( n 3 ) O(n^3) O(n3) 的算法来解决。
先用区间 d p dp dp 预处理,再 d p dp dp 取最小值。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  maxn = 1e6 + 5;

ll n,a[1005],num[1005][1005],f[1005];

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		num[i][i]=a[i];//初始化num[i][i]为本身
	}
	for(int len=1;len<=n;len++){//枚举区间长度
		for(int l=1;l+len<=n;l++){//枚举l端点
			int r=l+len;//r端点
			for(int k=l;k<r;k++){
				if(num[l][k]==num[k+1][r] && num[l][k]>0)//将一个区间切为两部分 如果这两部分相等 合并区间
					num[l][r]=num[l][k]+1;
			}

		}
	}
	for(int i=1;i<=n;i++){
		f[i]=f[i-1]+1;
		for(int j=1;j<=i;j++){
			if(num[j][i]>0)
				f[i]=min(f[i],f[j-1]+1);
		}
	}
	cout<<f[n];
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值