Educational Codeforces Round 1 E. Chocolate Bar dp
题目链接:http://codeforces.com/contest/598/problem/E
2 seconds
256 megabytes
standard input
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 n, m 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.
The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process.
Each of the next t lines contains three integers n, m 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.
For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly ksquares.
4
2 2 1
2 2 3
2 2 2
2 2 4
5
5
4
0
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.
题意:给你一块n*m的巧克力,取出k单位面积最小需要花费的价值;
思路:%q的代码
一直不知道该怎么写,然后看他dfs,记忆话搜索,果然好写多了;
dp,dp[i][j][k]表示当前i行j列大小的巧克力,取出大小为k的最小花费;
枚举切割的行或者列,两个不同面,所取的面积大小;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e7+,M=1e7+,inf=1e9+;
const ll INF=1e18+,mod=1e9+;
/// 数组大小
int dp[][][];
int dfs(int n,int m,int k)
{
if(k>n*m)return inf;
if(dp[n][m][k]<inf)return dp[n][m][k];
if(k==||k==n*m)return dp[n][m][k]=;
for(int i=;i<=n/;i++)
{
for(int j=;j<=(i*m,k);j++)
{
int x=dfs(n-i,m,j);
int y=dfs(i,m,k-j);
dp[n][m][k]=min(dp[n][m][k],x+y+m*m);
}
}
for(int i=;i<=m/;i++)
{
for(int j=;j<=(i*n,k);j++)
{
int x=dfs(n,m-i,j);
int y=dfs(n,i,k-j);
dp[n][m][k]=min(dp[n][m][k],x+y+n*n);
}
}
return dp[n][m][k];
}
int main()
{
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
for(int k=;k<=min(i*j,);k++)
dp[i][j][k]=inf;
}
}
int T;
scanf("%d",&T);
while(T--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
printf("%d\n",dfs(n,m,k));
}
return ;
}
最新文章
- MySQL 安装 + 精简 + 配置
- 需求:输入一个年份和月份 ,显示当前月日情况 ,星期数要对应准确 * 1.1900年1月1号开始 * 2.星期 : 直接用总天数对7求余数 31 28 59 / 7 = 5 * 3.以\t来个开
- Java接口中的方法
- 背水一战 Windows 10 (8) - 控件 UI: StateTrigger
- HDOJ(HDU) 2521 反素数(因子个数~)
- Android手势识别(单击 双击 抬起 短按 长按 滚动 滑动)
- JavaScript中遍历数组 最好不要使用 for in 遍历
- Java设计模式之《组合模式》及应用场景
- 一种基于主板BIOS的身份认证方案及实现
- ANT不完全总结,包含各种命令,ant例子等,转自:http://lavasoft.blog.51cto.com/62575/87306
- Elasticsearch: 权威指南 &#187; 深入搜索 &#187; 多字段搜索 &#187; 多数字段 good
- 2.2Bind建立配置文件和实体的映射「深入浅出ASP.NET Core系列」
- 漫画 | Redis常见面试问题(一)
- Vue2.0 新手完全填坑攻略—从环境搭建到发布
- Haproxy+Heartbeat 高可用集群方案操作记录
- IDEA如何刷新pom文件
- 【BZOJ】【4152】【AMPZZ2014】The Captain
- Extjs 中callParent的作用
- 【BZOJ】4293: [PA2015]Siano 线段树上二分
- [实战]MVC5+EF6+MySql企业网盘实战(5)——页面模板