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 ;
}
Educational Codeforces Round 1 E. Chocolate Bar dp的更多相关文章
- Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索
E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...
- Educational Codeforces Round 61 F 思维 + 区间dp
https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...
- Educational Codeforces Round 51 D. Bicolorings(dp)
https://codeforces.com/contest/1051/problem/D 题意 一个2*n的矩阵,你可以用黑白格子去填充他,求联通块数目等于k的方案数,答案%998244353. 思 ...
- Educational Codeforces Round 9 D. Longest Subsequence dp
D. Longest Subsequence 题目连接: http://www.codeforces.com/contest/632/problem/D Description You are giv ...
- Educational Codeforces Round 17 D. Maximum path DP
题目链接:http://codeforces.com/contest/762/problem/D 多多分析状态:这个很明了 #include<bits/stdc++.h> using na ...
- Educational Codeforces Round 39
Educational Codeforces Round 39 D. Timetable 令\(dp[i][j]\)表示前\(i\)天逃课了\(j\)节课的情况下,在学校的最少时间 转移就是枚举第\ ...
- [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)
Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
随机推荐
- poj1753Flip Game(dfs)
Flip Game 思想很不成熟, #include <stdio.h>#include <string.h>#include <stdlib.h>int map ...
- [LeetCode] 243. Shortest Word Distance_Easy
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- ListView列宽自适应,设置ListView.Column[0].Width := -1;
使用TListView列表显示内容,如果列内容过长,就会显示成‘XXX…’形式,此时如果双击列标题,列宽将变为自适应.用代码设置如下: 1.设置ListView.Column[0].Width := ...
- SQL备份数据库代码
SQL备份数据库代码 #region 服务每天备份一次数据库 /// <summary> /// 服务每天备份一次数据库 /// </summary> public void ...
- jmeter Bean Shell的使用(二)
BeanShell的用法 在此介绍下BeanShell PreProcessor的用法,其它的beahshell可以类推.在此我们使用beahshell调用自己写的工具类,工具类实现了密码的加.解密功 ...
- Qt setMargin()和setSpacing() 的含义
mainLayout=newQVBoxLayout(this); mainLayout->setMargin(30); //表示控件与窗体的左右边距 mainLayout->setSpac ...
- 摘要JSR168 PORLET标准手册汉化整理
本规范汉化资源搜集整理于网上并由我作了些修改和添加,主要为适应大陆的语辞.用语及其他未译之处. 由于本人于水平有限,如有错误,请各位高手指正:若有高见,希望不吝言辞,同为中国开源作项献. 特此严重感谢 ...
- Django初级手册3-视图层与URL配置
设计哲学 在Django中一个视图有指定函数和指定模版组成.对于某些特定的应用应该分成若干视图.例如博客系统 Blog主页面 详细页面入口 基于年的页面展示 基于月的页面展示 基于天的页面展示 评论行 ...
- XML—代码—DOM4J解析
什么是xml: 众所周知,xml常用语数据存储和传输,文件后缀为 .xml: 它是可扩展标记语言(Extensible Markup Language,简称XML),是一种标记语言. 如何定义这些标记 ...
- Promise学习探究
学习熟知吧,原理还是继续吧 例子1: var isGeted; function getRet(){ return new Promise(function(resolve, reject) { // ...