Codeforces Round #336 (Div. 1)-B. Zuma(区间dp)
来源:互联网 发布:oracledriver源码 编辑:程序博客网 时间:2024/06/06 07: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.
用dp[l][r]表示[l, r]这个区间内最少需要几步destroy all the gemstones, d[l, r] = min(dp[l, j] + dp[j+1, r]) l <= j < r, 而且dp[l, r] = min(dp[l, r], dp[l+1, r-1])
#include <bits/stdc++.h>#define INF 1e9#define maxn 505using namespace std;typedef long long ll;int dp[maxn][maxn], num[maxn];int main(){//freopen("in.txt", "r", stdin);int n;scanf("%d", &n);for(int i = 0; i < n; i++) scanf("%d", num+i);for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) dp[i][j] = INF;for(int i = 0; i < n; i++){dp[i][i] = 1;} for(int i = 1; i < n; i++) for(int j = 0; j + i < n; j++){ int l = j, r = i + j; for(int h = l; h < r; h++){ dp[l][r] = min(dp[l][r], dp[l][h] + dp[h+1][r]); } if(num[l] == num[r]){ if(i == 1) dp[l][r] = 1; else dp[l][r] = min(dp[l][r], dp[l+1][r-1]); } } printf("%d\n", dp[0][n-1]); return 0;}
- Codeforces Round #336 (Div. 1)-B. Zuma(区间dp)
- Codeforces Round #336 (Div. 1)B. Zuma (区间DP)
- Codeforces Round #336 (Div. 1) B. Zuma
- 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) 607B Zuma 回文dp小总结
- codeforces 607B #336B Zuma [区间DP]【动态规划】
- Codeforces 607B - Zuma (区间DP)
- Codeforces 607B Zuma 【区间dp】
- codeforces 607B Zuma 区间dp
- CodeForces 607 B. Zuma(区间DP)
- codeforces 607 B Zuma(区间dp)
- Codeforces 607B Zuma (区间dp)
- [Codeforces 607B Zuma]区间DP
- [区间DP] Zuma CodeForces 607B
- CodeForces 607B:Zuma(区间DP)
- Codeforces Round #336 (Div. 2) 608D Zuma(dfs+dp)
- 蜂鸣器实验
- hibernate关系映射之单向多对一和双向一对多
- (已解决)Error:(31, 29) 错误: 找不到符号 符号: 方法 setLatestEventInfo(MainActivity,String,String,<空值>) 位置: 类型为No
- jquery取值与赋值
- OneNote同时设置中英文字体显示(雅黑+Consolas)
- Codeforces Round #336 (Div. 1)-B. Zuma(区间dp)
- JSONObject JSONArray使用备忘
- session个某个属性多次赋值
- Android文件管理器
- 把数组排成最小的数
- 页面打印错误 抛出异常 公司内部用
- Android NDK/JNI入门
- iOS FMDB 事务的理解
- 汉诺塔(Hanoi)递归算法笔记记录