Topic links: https://www.luogu.org/problem/CF1114D
Meaning of the title: the n-cubes in a row, the i th color is C_i . Unicom define a color block [l, r] if and only if l and (comprised between R & lt l, R & lt ) all the same color of the box. Now you can select a starting position p , each time p where all colors Unicom block box color into another. This operation may be combined into a plurality of color blocks of a link. A minimum number of steps to ask, let [1, n] into a color block Unicom.
Analysis: This section DP is easy to see
Pretreatment of the same color initially into an easily think
When I think of the interval dp is an array with a DP structure, stored val value and this one color (in fact, no longer satisfied with the after-effect)
For the two sandwiching the same color is not satisfied
#include<bits/stdc++.h> using namespace std; int n, x, a[5010], f[5010][5010], y, cnt; int main() { scanf("%d", &n); for (int i = 1; i <= n; i ++) { scanf("%d", &x); if (x != y) a[++ cnt] = x; y = x; } for (int i = 1; i <= cnt; i ++) for (int j = 1; j + i <= cnt; j ++) if (a[j] == a[j + i]) f[j][j + i] = f[j + 1][j + i - 1] + 1; else f[j][j + i] = std::min(f[j + 1][j + i], f[j][j + i - 1]) + 1; return printf("%d\n", f[1][cnt]), 0; }