Codeforces Round #336 (Div. 2) D 區間dp

CrossDolphin發表於2016-07-12



連結:戳這裡


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;
}



相關文章