Codeforces Round #336 Zuma

D. 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.

思考:

对于这种题目,显然只能考虑搜索、动态规划、贪心。

搜索复杂度太高、全局贪心显然不对,因而从可行解法来看需要考虑动态规划。

实际上,我们在做每一步决策时只能靠猜,即试错,通过归纳得出能导出整体最优的局部最优解。

即try->assert->conclude。

虽然不同的操作非常之多,但满足整体操作数最少的序列是有规律可循的,这也是能使用动态规划的原因。

注意到回文串的性质,若在回文串两端分别添加相同字符,新串仍为回文串。

我们设dp[i][j]表示[i, j]区间的最小操作数,考虑递推关系:

考虑最左端的字符,显然它最终需要被消除,因此有两种被消除的可能,一种是独自消除,

这种情形下dp[i][j] <= 1 + dp[i + 1][j].

另一种情况是该元素作为某非平凡子串的左端元素被消除,设为s[i] + substring + s[k]。

那么显然应该有s[i] == s[k], 同时substring为回文串。那么注意到substring被串s[i+1...k-1]所包含,

因此当完成substring的消除,即相当于同时完成对s[i] + subtring + s[k]的消除,那么有:

dp[i][j] <= dp[i+1][[k-1] + dp[k+1][j],

注意到substring可能为空串,此时dp[i][j] <= 1 + dp[i + 2][j].

有以上dp[i][j] = min(1 + dp[i + 1][j], dp[i + 1][k-1] + dp[k+1][j], 1 + dp[i + 2][j]).

动态规划的神奇之处,或者说与人脑解决问题的不同之处(与此相对,搜索更加直观)是它并不直观地给出推导出

最终答案的逻辑脉络,而是按照正确的“规则”,通过不断缩小问题规模,不断试错并且更正从

而最终达到解决问题的目的,这或许可以作为人工智能终将超越人脑的依据之一。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 const int maxn = 5e2 + 10;
 6 const int inf = 0x3f3f3f3f;
 7 int a[maxn];
 8 int dp[maxn][maxn];
 9 int n;
10 
11 int getAns(int l, int r){
12     if(l > r) return 0;
13     if(l == r) return 1;
14     if(dp[l][r] != -1) return dp[l][r];
15     int tem = inf;
16     tem = min(tem, 1 + getAns(l + 1, r));
17     if(a[l] == a[l + 1]) tem = min(tem, getAns(l + 2, r) + 1);
18     for(int i = l + 2; i <= r; i++){
19         if(a[i] == a[l]) tem = min(tem, getAns(l + 1, i - 1) + getAns(i + 1, r));
20     }
21     return dp[l][r] = tem;
22 }
23 
24 int solve(){
25     memset(dp, -1, sizeof dp);
26     int ans = getAns(0, n - 1);
27     return ans;
28 }
29 
30 int main(){
31     freopen("in.txt", "r", stdin);
32     while(~scanf("%d", &n)){
33         for(int i = 0; i < n; i++) scanf("%d", &a[i]);
34         int ans = solve();
35         printf("%d
", ans);
36     }
37     return 0;
38 }
View Code
原文地址:https://www.cnblogs.com/astoninfer/p/5107409.html