Codeforces 607B Zuma[구간 dp]

3872 단어 구간DPcodeforces
B. Zuma
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard 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.
Sample test(s)
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.
제목: 매번 회문 서열을 제거할 수 있는 서열을 하나 줍니다. 최소한 몇 번의 조작이 필요하면 이 서열을 삭제할 수 있는지 물어보십시오.
사고방식: 구간 dp, 경계 상태의 추이 처리가 좀 번거롭고 직접적으로 기억화되었다.
일반적으로 dp[i][j]=min(dp[i][k]+dp[k+1][j])이 있다.(i <= k <= j)
특히 a[i]=a[j]:dp[i][j]=min(dp[i+1][j-1])이 있다. 
AC 코드:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (500+10)
#define MAXM (500000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d
", (a)) #define Pf(a) printf("%.2lf
", (a)) #define Pl(a) printf("%lld
", (a)) #define Ps(a) printf("%s
", (a)) #define W(a) while(a--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define LL long long #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 #define PI acos(-1.0) using namespace std; int dp[MAXN][MAXN]; int a[MAXN]; int DFS(int i, int j) { if(dp[i][j] != -1) return dp[i][j]; if(i == j) return 1; if(i > j) return 0; if(i + 1 == j) { if(a[i] == a[j]) return 1; else return 2; } dp[i][j] = INF; if(a[i] == a[j]) dp[i][j] = DFS(i+1, j-1); for(int k = i; k <= j; k++) dp[i][j] = min(dp[i][j], DFS(i, k) + DFS(k+1, j)); return dp[i][j]; } int main() { int n; Ri(n); for(int i = 1; i <= n; i++) Ri(a[i]); CLR(dp, -1); Pi(DFS(1, n)); return 0; }

좋은 웹페이지 즐겨찾기