【动态规划专练-区间DP】CF607B (1900)

B. ZumaB. 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
inputCopy
3
1 2 1
outputCopy
1
inputCopy
3
1 2 3
outputCopy
3
inputCopy
7
1 4 4 2 3 2 1
outputCopy
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.

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <stack>
#include <cstdio>
#include <queue>
#include <set>
#include<sstream>
#include <cstring>
#include <cmath>
#include <bitset>
//#pragma GCC optimize(2);
#define IOS ios::sync_with_stdio(false);
#define mm(a, b) memset(a, b, sizeof(a))
const double PI = acos(-1.0);
typedef long long ll;
const int N = 2e6+5;
const int M = N*2;
const double eps =1e-8;
const ll mod =  998244353;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double maxd = -1e9;
const int maxn = 500;
using namespace std;
typedef pair<string,int> PII;
//处理回文串类的区间DP:
//因为区间DP的转移顺序是小区间到大区间,所以
//初始化单个珠子消耗为1,重点:处理相邻两个的消耗,相同f[i][i+1] =1 ,! f[i][i+1] =2
//因为把中间消去,两端合并可以得到新的回文串,
//所以if(a[i] == a[j]) f[i][j] = (f[i][j],f[i-1][j-1]);
int a[505]; int f[505][505];
int main(){
    int n;
    cin>>n;
    memset(f,0x3f,sizeof f);
    for(int i=1;i<=n;i++) cin>>a[i],f[i][i] = 1;
    for(int i=1;i<=n-1;i++) if(a[i] == a[i+1]) f[i][i+1] = 1; else f[i][i+1] = 2;
    for(int len = 2;len <= n;len ++){
        for(int i = 1;i + len <= n;i ++){
            int j = i+len;
            if(a[i] == a[j]) f[i][j] = f[i+1][j-1];
            for(int k = i; k < j; k++){
                f[i][j] = min(f[i][j] , f[i][k]+f[k+1][j]);
            }
        }
    }
    cout<<f[1][n]<<endl;
    return 0;
}
posted @ 2021-09-14 11:10  qingyanng  阅读(25)  评论(0编辑  收藏  举报