Codeforces Round #336 (Div. 2) D 區間dp
連結:戳這裡
D. Zuma
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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.
題意:
給出n個寶石的顏色ci,現在有一個操作,就是子串的顏色是迴文串的區間可以通過一次操作消去
問最少需要多少次操作可以消除所有的寶石
思路:
很明顯是要dp,而且n=500
那麼設定狀態的時候dp[i][j] 表示區間[i,j]的最優值
那麼怎麼轉移呢?直接看程式碼吧!
程式碼:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int dp[550][550]; /// 區間[i,j]的最小值
int a[550],n;
int main(){
mst(dp,127);
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%d",&a[i]);
for(int i=0;i<n;i++) dp[i][i]=1; /// 初始化 本身到本身為1
for(int i=1;i<n;i++){ /// 列舉當前區間的長度
for(int j=0;j+i<n;j++){ /// 當前區間[j,j+i]的最小值
if(i==1){ /// 當前長度為1的時候直接判斷 後面的區間裡面至少長度>2
if(a[j]==a[j+i]) dp[j][j+i]=1;
else dp[j][j+i]=2;
} else {
if(a[j]==a[j+i]) dp[j][j+i]=dp[j+1][j+i-1]; ///首尾相等的話轉移到[j+1,j+i-1]區間上去取最優值
for(int k=j;k<j+i;k++){
dp[j][j+i]=min(dp[j][j+i],dp[j][k]+dp[k+1][i+j]);
/// 列舉這個k,有最優的值就更新區間最優值
}
}
}
}
printf("%d\n",dp[0][n-1]);
return 0;
}
相關文章
- Codeforces Round #336 (Div. 2) C 二分+dp
- Codeforces Round #358 (Div. 2) D dp
- Codeforces Round #321 (Div. 2) D 狀壓dp
- Codeforces Round #336 (Div. 2) B 暴力
- Educational Codeforces Round 167 (Rated for Div. 2) D(dp,貪心)
- Codeforces Round #452 (Div. 2) D
- Codeforces Round 941 (Div. 2) D
- Codeforces Round 960 (Div. 2)(A - D)
- Codeforces Round #325 (Div. 2) D bfs
- Codeforces Round #250 (Div. 2) A-D
- Codeforces Round #251 (Div. 2) A/B/D
- Codeforces Round #283 (Div. 2) D,E
- Codeforces Round #256 (Div. 2)A-D
- Codeforces Round #316 (Div. 2) E dp
- Codeforces Round #359 (Div. 2) D DFS
- Educational Codeforces Round 34 (Rated for Div. 2) D
- Codeforces Round #290 (Div. 2) A,B,C,D
- Codeforces Round #263 (Div. 2) A-D
- Codeforces Round 720 (Div. 2) A-D
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) D 雙向dp
- Codeforces Round #673 (Div. 2)(A-D)題解
- Codeforces Round #401 (Div. 2)(C,D,E)
- Codeforces Round #360 (Div. 2) D 數學題
- Codeforces Round #288 (Div. 2) A,B,C,D,E
- Codeforces Round #287 (Div. 2)A,B,C,D,E
- Codeforces Round #253 (Div. 2) D. Andrey and Problem
- Codeforces Round #280 (Div. 2 A,B,C,D,E)
- Educational Codeforces Round 170 (Rated for Div. 2) A-D
- 【數論】Codeforces Round #404 (Div. 2)(D)Anton and School - 2
- Educational Codeforces Round 142 (Rated for Div. 2) A-D
- Codeforces Round 960 (Div. 2) 補題記錄(A~D)
- CodeForces Round #951(Div. 2) 補題記錄(A~D)
- Codeforces Round 986 (Div. 2)題解記錄(A~D)
- Codeforces Round 987 (Div. 2)題解記錄(A~D)
- Codeforces Round #360 (Div. 2) E dp 類似01揹包
- 樹形dp - Codeforces Round #322 (Div. 2) F Zublicanes and Mumocrates
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)