Codeforces Round #336 (Div. 2)-D Zuma(区间DP)
来源:互联网 发布:安卓好用的拼长图软件 编辑:程序博客网 时间:2024/06/06 00:31
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.
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.
Print a single integer — the minimum number of seconds needed to destroy the entire line.
31 2 1
1
31 2 3
3
71 4 4 2 3 2 1
2
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
题意:给你n个数,每次可以选择一个回文子串消除,然后剩下的序列自动按顺序组成串,重复操作,问你最少删多少次可以消完。
题解:裸的区间DP。。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[505],dp[505][505];int main(void){int n,i,j,k,len;scanf("%d",&n);for(i=1;i<=n;i++)scanf("%d",&a[i]);memset(dp,127,sizeof(dp));for(i=1;i<=n;i++)dp[i][i]=1;for(len=2;len<=n;len++)for(i=1;i+len-1<=n;i++){j=i+len-1;for(k=i;k<j;k++)dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);if(a[i]==a[j]){if(i+1==j)dp[i][j]=1;dp[i][j]=min(dp[i+1][j-1],dp[i][j]); }}printf("%d\n",dp[1][n]);return 0;}
- Codeforces Round #336 (Div. 2) D. Zuma(区间dp)
- Codeforces Round #336 (Div. 2)-D Zuma(区间DP)
- Codeforces Round #336 (Div. 2)D. Zuma【区间dp】
- Codeforces Round #336 (Div. 1)B. Zuma (区间DP)
- Codeforces Round #336 (Div. 2) 608D Zuma(dfs+dp)
- Codeforces Round #336 (Div. 2) D. Zuma
- Codeforces Round #336 (Div. 2) D. Zuma
- Codeforces Round #336 (Div. 1)-B. Zuma(区间dp)
- 【Codeforces Round 336 (Div 2) D】【区间DP 讨论 好题】Zuma 区间取回文串最小操作次数使得取光全串
- codeforces #336 D. Zuma (区间dp)
- Codeforces Round #336 (Div. 2) D 区间dp
- Codeforces Round #336 (Div. 1) B. Zuma
- CodeForces 607 B. Zuma(区间DP)
- CodeForces 607B:Zuma(区间DP)
- Codeforces #337(Div.2)D. Zuma【记忆化搜索】
- CodeForces 608D Zuma(DP)
- Codeforces Round #273 (Div. 2)D dp
- Codeforces Round #358 (Div. 2) D dp
- 不要过度依赖JQuery
- 笔记:php curl请求头与服务端接收
- 【iOS_GitHub】banner(广告页/循环滚动广告图片/无限循环自动滚动卡片)
- 串口 i2c
- Netty RPC demo 试跑
- Codeforces Round #336 (Div. 2)-D Zuma(区间DP)
- Locker UVA
- Spring security+rememberme学习笔记(1)
- c++学习:析构函数
- Log4net详细说明
- C# 多个table转成一个table的一行
- 天天学Linux命令35--top命令
- 基于Android的http&https中间人攻击
- Android--开机自启动(activity或service)