Codeforces 598E Chocolate Bar 【区间dp + 打表】

来源:互联网 发布:诛仙手游数据互通吗 编辑:程序博客网 时间:2024/06/07 17:25

E. Chocolate Bar
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar.

In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length.

For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 32 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 22 = 4).

For several given values nm and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - ksquares are not necessarily form a single rectangular piece.

Input

The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values nm and k to process.

Each of the next t lines contains three integers nm and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively.

Output

For each nm and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly ksquares.

Sample test(s)
input
42 2 12 2 32 2 22 2 4
output
5540
Note

In the first query of the sample one needs to perform two breaks:

  • to split 2 × 2 bar into two pieces of 2 × 1 (cost is 22 = 4),
  • to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 12 = 1).

In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.



自己花了1个半小时AC了,还是很有收获的。


题意:给定n*m块连在一起的巧克力,现在要取出k块,已知每次可以水平或竖直的分割(切到底),代价是分割线的平方。问取出k块巧克力需要的最少代价。


思路:设置dp[k][n][m]为在n*m块连在一起的巧克力上取出k块需要的最少代价。

状态转移方程 (其中0 <= i <= k 因为在分开的两大块中可以任意选取要取多少块来使得取的总块数 = k)

在row位置横切:1 <= row < n  可以分为n-row * m  和  row * m这两大块,然后任选需要取的数目。

dp[k][n][m] = min(dp[k-i][n-row][m]+dp[i][row][m], dp[i][n-row][m]+dp[k-i][row][m]) + m*m;

在cul位置竖切:1 <= cul < m  可以分为n * m-cul  和  n * cul这两大块,然后任选需要取的数目。 

dp[k][n][m] = min(dp[k-i][n][m-cul]+dp[i][n][cul], dp[i][n][m-cul]+dp[k-i][n][cul]) + n*n;

中间有重复的判断没有剪掉。蒟蒻,跑了405ms,不打表过不了。 o(╯□╰)o


AC代码:

#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <vector>#define INF 0x3f3f3f3f#define eps 1e-4#define MAXN (10000+10)#define MAXM (1000000)#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\n", (a))#define Pf(a) printf("%lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 100000007#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|1using namespace std;int ans[51][31][31];int dp(int k, int n, int m){    if(ans[k][n][m] != -1)        return ans[k][n][m];    else if(k > n*m)        ans[k][n][m] = INF;    else if(k == n*m || k == 0)        ans[k][n][m] = 0;    else    {        ans[k][n][m] = INF;        for(int i = 0; i <= k; i++)        {            for(int row = 1; row < n; row++)                ans[k][n][m] = min(ans[k][n][m], min(dp(k-i, n-row, m)+dp(i, row, m), dp(k-i, row, m)+dp(i, n-row, m)) + m*m);            for(int cul = 1; cul < m; cul++)                ans[k][n][m] = min(ans[k][n][m], min(dp(k-i, n, m-cul)+dp(i, n, cul), dp(k-i, n, cul)+dp(i, n, m-cul)) + n*n);        }    }    return ans[k][n][m];}int main(){    CLR(ans, -1);    for(int i = 1; i <= 30; i++)        for(int j = 1; j <= 30; j++)            for(int k = 0; k <= min(i*j, 50); k++)                dp(k, i, j);    int t; Ri(t);    W(t)    {        int n, m, k;        Ri(n); Ri(m); Ri(k);        Pi(ans[k][n][m]);    }    return 0;}



0 0