codeforces 598E E. Chocolate Bar(区间dp)
题目链接:
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.
AC代码:
- #include <bits/stdc++.h>
- using namespace std;
- const int inf=0x7f7f7f7f;
- int dp[][][];
- int n,m,k;
- int get_ans()
- {
- memset(dp,inf,sizeof(dp));
- for(int i=;i<=;i++)
- {
- for(int j=;j<=;j++)
- {
- dp[i][j][]=;
- dp[][i][j]=;
- dp[i][][j]=;
- }
- }
- for(int i=;i<=;i++)
- {
- for(int j=;j<=;j++)
- {
- int x=min(i*j,);
- if(i*j<=)dp[i][j][i*j]=;
- for(int k=;k<=x;k++)
- {
- for(int v=;v<=i/;v++)
- {
- int num=min(k,v*j);
- for(int u=;u<=num;u++)
- {
- dp[i][j][k]=min(dp[i][j][k],dp[v][j][u]+dp[i-v][j][k-u]+j*j);
- }
- }
- for(int v=;v<=j/;v++)
- {
- int num=min(k,v*i);
- for(int u=;u<=num;u++)
- {
- dp[i][j][k]=min(dp[i][j][k],dp[i][v][u]+dp[i][j-v][k-u]+i*i);
- }
- }
- }
- }
- }
- }
- int main()
- {
- int t;
- scanf("%d",&t);
- get_ans();
- while(t--)
- {
- scanf("%d%d%d",&n,&m,&k);
- printf("%d\n",dp[n][m][k]);
- }
- return ;
- }
codeforces 598E E. Chocolate Bar(区间dp)的更多相关文章
- Codeforces 598E:Chocolate Bar
E. Chocolate Bar time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Educational Codeforces Round 61 F 思维 + 区间dp
https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...
- Codeforces Gym100543L Outer space invaders 区间dp 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/CF-Gym100543L.html 题目传送门 - CF-Gym100543L 题意 $T$ 组数据. 有 $n ...
- Codeforces 508E Arthur and Brackets 区间dp
Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...
- codeforces 1101F Trucks and Cities 区间dp+单调优化 好题
题目传送门 题意简述:(来自洛谷) 有n个城市坐落在一条数轴上,第ii个城市位于位置ai. 城市之间有m辆卡车穿行.每辆卡车有四个参数:si为起点编号,fi为终点编号,ci表示每行驶1个单位长 ...
- Codeforces 633F The Chocolate Spree 树形dp
The Chocolate Spree 对拍拍了半天才知道哪里写错了.. dp[ i ][ j ][ k ]表示在 i 这棵子树中有 j 条链, 是否有链延伸上来. #include<bits/ ...
- Codeforces 509F Progress Monitoring:区间dp【根据遍历顺序求树的方案数】
题目链接:http://codeforces.com/problemset/problem/509/F 题意: 告诉你遍历一棵树的方法,以及遍历节点的顺序a[i],长度为n. 问你这棵树有多少种可能的 ...
- CodeForces 149D Coloring Brackets (区间DP)
题意: 给一个合法的括号序列,仅含()这两种.现在要为每对括号中的其中一个括号上色,有两种可选:蓝or红.要求不能有两个同颜色的括号相邻,问有多少种染色的方法? 思路: 这题的模拟成分比较多吧?两种颜 ...
- 【非原创】codeforces 1025D - Recovering BST【区间dp+二叉搜索树】
题目:戳这里 题意:给一个不下降序列,有n个数.问能否构造一个二叉搜索树,满足父亲和儿子之间的gcd>1. 解题思路:其实这题就是构造个二叉搜索树,只不过多了个条件.主要得了解二叉搜索树的性质, ...
随机推荐
- unittest 结合 ddt
数据驱动测试ddt,使用的重点: 1.@ddt.ddt2.@ddt.data(*zip(range(10),range(10,20))) 注意一定要带* 3.@ddt.unpack # c ...
- HDU 3397 Sequence operation(区间合并 + 区间更新)
题目链接:pid=3397">http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意:给定n个数,由0,1构成.共同拥有5种操作. 每一个操 ...
- SpringMVC基于代码的配置方式(零配置,无web.xml)
基于配置文件的web项目维护起来可能会更方便,可是有时候我们会有一些特殊的需求,比方防止客户胡乱更改配置,这时候我们须要给配置隐藏到代码中. 1.创建一个动态web项目(无需web.xml) 2.右键 ...
- PDP开发环境搭建
1. 安装git 2.创建SSH-rsa钥匙 3. 写入 gitlab 4. 克隆分支 git clone -b dev_pdp_minz_ep_metting git@gitlab.csvw ...
- 【HTML5开发系列】HTML元素总结
HTML元素汇总,包含HTML4元素和HTML5新增元素.Y表示有变化,N则表示没有变化,N/A表示未知 文档和元数据元素 包括说明HTML文档的结构,向浏览器说明文档的情况,定义脚本程序和css样式 ...
- iOS开发之获取系统相册ALAssetLibrary
注:当你选择看这篇博客时想必你的应用还支持iOS8一下系统,如果你的应用要求最低版本大于iOS8,建议使用PhotoKit框架,效率更高 ALAssetsLibrary包含,ALAssetsLibra ...
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之游戏效果预览(一)
今天看完了李刚老师的<疯狂Android讲义>一书中的第18章<疯狂连连看>,从而学会了如何编写一个简单的Android疯狂连连看游戏. 开发这个流行的小游戏,难度适中,而且能 ...
- Python学习笔记_Python基础
Python 基础 语句和语法 凝视 继续 代码组 代码的缩进 在一行书写多个语句 模块 变量赋值 赋值操作符 增量赋值 多重赋值 多元赋值 python编写的基本风格 模块的结构和布局 内存管理 变 ...
- linux 9 -- 交互式使用Bash Shell
二十二. 交互式使用Bash Shell: 1. 用set命令设置bash的选项: 下面为set主要选项的列表及其表述: 选项名 开关缩写 描述 allexport -a 打开此开关 ...
- Hive与Hbase关系整合
近期工作用到了Hive与Hbase的关系整合,虽然从网上参考了很多的资料,但是大多数讲的都不是很细,于是决定将这块知识点好好总结一下供大家分享,共同掌握! 本篇文章在具体介绍Hive与Hbase整合之 ...