Blocks poj 区间dp
Some of you may have played a game called 'Blocks'. There are n blocks in a row, each box has a color. Here is an example: Gold, Silver, Silver, Silver, Silver, Bronze, Bronze, Bronze, Gold.
The corresponding picture will be as shown below:
Figure 1
If some adjacent boxes are all of the same color, and both the box to its left(if it exists) and its right(if it exists) are of some other color, we call it a 'box segment'. There are 4 box segments. That is: gold, silver, bronze, gold. There are 1, 4, 3, 1 box(es) in the segments respectively.
Every time, you can click a box, then the whole segment containing that box DISAPPEARS. If that segment is composed of k boxes, you will get k*k points. for example, if you click on a silver box, the silver segment disappears, you got 4*4=16 points.
Now let's look at the picture below:
Figure 2
The first one is OPTIMAL.
Find the highest score you can get, given an initial state of this game.
Input
The corresponding picture will be as shown below:
Figure 1
If some adjacent boxes are all of the same color, and both the box to its left(if it exists) and its right(if it exists) are of some other color, we call it a 'box segment'. There are 4 box segments. That is: gold, silver, bronze, gold. There are 1, 4, 3, 1 box(es) in the segments respectively.
Every time, you can click a box, then the whole segment containing that box DISAPPEARS. If that segment is composed of k boxes, you will get k*k points. for example, if you click on a silver box, the silver segment disappears, you got 4*4=16 points.
Now let's look at the picture below:
Figure 2
The first one is OPTIMAL.
Find the highest score you can get, given an initial state of this game.
The first line contains the number of tests t(1<=t<=15).
Each case contains two lines. The first line contains an integer
n(1<=n<=200), the number of boxes. The second line contains n
integers, representing the colors of each box. The integers are in the
range 1~n.
Output
For each test case, print the case number and the highest possible score.
Sample Input
2 9 1 2 2 2 2 3 3 3 1 1 1Sample Output
Case 1: 29 Case 2: 1
对于贪心显然就不正确了;
那么考虑dp;
设dp[ i ][ j ][ k ]表示i~j区间,最后合并k个的最大值;
dp[ i ][ j ][ k ]=dp[ i ][ j-1 ][ 0 ]+( len[ j ]+k )^2;
第二种情况就是中间一段先消去,然后与后面那一段拼接消除;
dp[ i ][ j ][ k ]=dp[ i ][ k ][ len[ j ]+k ]+dp[ k+1 ][ j-1 ][ 0 ];
那么我们记忆化dfs即可;
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#include<cctype> //#pragma GCC optimize(2) using namespace std; #define maxn 400005 #define inf 0x7fffffff //#define INF 1e18 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdult(x) scanf("%lu",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const long long int mod = 1e9 + 7; #define Mod 1000000000 #define sq(x) (x)*(x) #define eps 1e-3 typedef pair<int, int> pii; #define pi acos(-1.0) const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) typedef pair<int, int> pii; inline ll rd() { ll x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == '-') f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } ll sqr(ll x) { return x * x; } /*ll ans; ll exgcd(ll a, ll b, ll &x, ll &y) { if (!b) { x = 1; y = 0; return a; } ans = exgcd(b, a%b, x, y); ll t = x; x = y; y = t - a / b * y; return ans; } */ ll mode; struct matrix { ll n, m, a[10][10]; matrix(ll n, ll m) { this->n = n; this->m = m; ms(a); } matrix(ll n, ll m, char c) { this->n = n; this->m = m; ms(a); for (int i = 1; i <= n; i++)a[i][i] = 1; } ll *operator [](const ll x) { return a[x]; } matrix operator *(matrix b) { matrix c(n, b.m); for (int i = 1; i <= n; i++) { for (int j = 1; j <= b.m; j++) { for (int k = 1; k <= m; k++) { c[i][j] = (c[i][j] + a[i][k] % mode*b[k][j] % mode) % mode; } } } return c; } void operator *=(matrix &b) { *this = *this *b; } matrix operator ^(ll b) { matrix ans(n, m, 'e'), a = *this; while (b) { if (b % 2)ans = ans * a; a *= a; b >>= 1; } return ans; } }; int dp[202][202][202]; int T; int n; int col[210]; int len[202]; int fg; int dfs(int x, int y, int k) { if (dp[x][y][k])return dp[x][y][k]; if (x == y)return (len[x] + k)*(len[x] + k); dp[x][y][k] = dfs(x, y - 1, 0) + (len[y] + k)*(len[y] + k); for (int i = x; i < y; i++) { if (col[i] == col[y]) { dp[x][y][k] = max(dp[x][y][k], dfs(x, i, len[y] + k) + dfs(i + 1, y - 1, 0)); } } return dp[x][y][k]; } int main() { //ios::sync_with_stdio(0); rdint(T); int cnt = 0; while (T--) { cnt++; ms(dp); ms(col); ms(len); fg = 0; int ans = 0; rdint(n); for (int i = 1; i <= n; i++) { int tmp; rdint(tmp); if (col[fg] == tmp)len[fg]++; else fg++, len[fg] = 1, col[fg] = tmp; } ans = dfs(1, fg, 0); cout << "Case " << cnt << ": " << ans << endl; } return 0; }
EPFL - Fighting