(CodeForces

来源:互联网 发布:数据分析师先学r还是p 编辑:程序博客网 时间:2024/05/22 09:43

(CodeForces - 607B)Zuma

time limit per test:2 seconds
memory limit per test:512 megabytes
input:standard input
output:standard output

Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.

In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?

Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 500) — the number of gemstones.

The second line contains n space-separated integers, the i-th of which is ci (1 ≤ ci ≤ n) — the color of the i-th gemstone in a line.

Output

Print a single integer — the minimum number of seconds needed to destroy the entire line.

Examples

input

3
1 2 1

output

1

input

3
1 2 3

output

3

input

7
1 4 4 2 3 2 1

output

2

Note

In the first sample, Genos can destroy the entire line in one second.

In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.

In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1.

题目大意:对于一个数字序列,每一次攻击可以消灭一个回文串(如果只有一个字符也算一个回文串需要攻击一次),消灭一串回文串后,两端没有消灭的数字序列会接在一起。问最少需要几次攻击可以完全消灭这个数字序列。

思路:题目中提示回文串是最外边两端数字相同,再考虑次外边两端数字相同。因此考虑区间[i,j],以i为研究对象,设f[i][j]表示将区间[i,j]数字全部消去所需要的最少攻击次数。那么最最一般的情况(就是单独一个字符成一个回文串)便是f[i][j]=f[i+1][j]+1,如果两端的数字相等(a[i]==a[j])那么f[i][j]=min(f[i][j],f[i+1][j1]),当然这里i+1可能大于j-1所以需要特判。上述那种情况也可能不是最优解,先消灭中间的回文串会更优,所以枚举k(i<k<j)如果(a[i]==a[k]),那么f[i][j]=min(f[i][j],f[i+1][k1]+f[k+1][j]),同理这里i+1可能大于k-1所以需要特判。

#include<cstdio>#include<algorithm>using namespace std;const int maxn=505;int a[maxn],f[maxn][maxn];int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=1;i<=n;i++)        {            scanf("%d",a+i);            f[i][i]=1;        }        for(int len=2;len<=n;len++)            for(int i=1;i+len-1<=n;i++)            {                int j=i+len-1;                f[i][j]=f[i+1][j]+1;                if(a[i]==a[j])                {                    if(i+1>j-1) f[i][j]=min(f[i][j],1);                    else f[i][j]=min(f[i][j],f[i+1][j-1]);                }                for(int k=i+1;k<j;k++)                    if(a[i]==a[k])                    {                        if(i+1>k-1) f[i][j]=min(f[i][j],1+f[k+1][j]);                        else f[i][j]=min(f[i][j],f[i+1][k-1]+f[k+1][j]);                    }            }        printf("%d\n",f[1][n]);    }    return 0;}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 我的世界联机平台房间进不去怎么办 验车时间过了3天怎么办 在携程订的机票和酒店不能退怎么办 千牛工作台中旺旺登录失败怎么办 掌通家园换手机号了忘了更改怎么办 qq注销了后绑定的全民k歌怎么办 小米账号绑定的手机号注销了怎么办 微信账号注销后绑定的手机号怎么办 微信账号不想用了注销不了怎么办 苹果手机掉了捡到不还怎么办 在工作中把和别人结下梁子了怎么办 在阿里巴巴上买的货物没发货怎么办 做了下颌角一边脸反复肿怎么办 玩广东11选5输50万怎么办 玩广东11选5输了十几万怎么办 新办公楼装修好就要搬进去怎么办 oppo手机上的黄页删了怎么办 小米不小心把手机联系人删了怎么办 58同城小区名输不了怎么办 e路发注册直接有积分怎么办 大众速腾的不锈钢圈被碰花了怎么办 公司卡着生育险不给怎么办 济南图书馆借书超过期限了怎么办 淘宝解绑支付宝支付密码不对怎么办 微博支付宝支付密码忘记了怎么办 图虫签约师通过了认证标识怎么办 签证做假工作证明资料被拒怎么办 在广州办个建设厅电工证怎么办 水利考的五大员证到有效期怎么办 额头注射玻尿酸吸收后不平怎么办 施工员证书挂靠拿不回来怎么办 森林公安未转政法编制的人员怎么办 北京的限行新政策外地车怎么办 报考二级建造师工作年限不够怎么办 郑州航院图书馆密码忘了怎么办 无锡妇幼预约挂号过号了怎么办 云南建筑八大员考试没通过怎么办 订车ax7一个月提不到车怎么办 提车时间到了却没车怎么办 一汽大众速腾气囊灯亮该怎么办呢? 幼儿园上课时候电脑上的课件怎么办