Codeforces Round #336 (Div. 2) D. Zuma(区间dp)
来源:互联网 发布:2017年淘宝好做吗 编辑:程序博客网 时间:2024/06/05 15:13
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的序列,每次可以删除一个回文子串,删除之后两边合并起来,问最少几次可以将序列删完,令f[l][r]表示删完区间[l,r]子串的最小次数,注意s[l] == s[r]时,可以直接由f[l+1][r-1]转移来。其他情况可以从f[l][i]+f[i+1][r]转移过来。
#include <bits/stdc++.h>typedef long long ll;using namespace std;const int maxn = 500 + 5;int f[maxn][maxn],s[maxn];int solve(int l,int r){ if(l==r) return 1; if(l > r) return 0; int & res = f[l][r]; if(~res) return res; res = maxn; for(int i = l; i < r; ++i) { res = min(res,solve(l,i)+solve(i+1,r)); } if(s[l] == s[r]) res = min(res,max(1,solve(l+1,r-1))); return res;}int main(){ int n; while(scanf("%d",&n)==1) { memset(f,-1,sizeof f); for(int i = 1; i <= n; ++i) scanf("%d",s+i); printf("%d\n",solve(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
- Android mtk6572 Camera 录制更大视频文件的修改
- 都玩啥(DouWanSha)-开源项目实践-启动篇
- 后台播放音频,不同Activity,统一控制,已封装,轻松集成
- Git 分支的理解
- (二)在Android硬件抽象层增加接口模块访问硬件驱动程序
- Codeforces Round #336 (Div. 2) D. Zuma(区间dp)
- 第11章 掌握GCD(唐巧《IOS开发进阶》)
- 进制控制原语
- pg中replace和translate的用法(数据少的中文排序)
- linux常用命令—— 网络通信(十)
- 关于ListView中adapter调用notifyDataSetChanged失效的原因总结
- WINDOWS API——OPENCLIPBOARD——剪切板
- java.lang.IncompatibleClassChangeError
- 把$date = '08/26/2003' 转换成2003/08/26