Cut The Cake
来源:互联网 发布:windows激活后不能退货 编辑:程序博客网 时间:2024/05/18 02:34
Cut The Cake
Li Lei and Han Meimei love each other so much that they can not be separated for even one minute. They have promised each other to share all the happy things and delicious foods. In the Saint Valentine's Day in 2009, they bought one big tasty cake and tried to divide it into two shares with only one cut. Here comes the problem:
The cake they bought is in the shape of rectangle which is made up of m*n units(each units is a 1*1 square). Since the cake is delicious, each unit in the cake has a nutrition_num. For each unit, the nutrition_num can be either positive or negative because there may be various kinds of nutrition in different kinds of food(either useful or harmful for our health). They want to divide the cake into two shares with the difference between the total nutrition of the two does not exceed an expeced value t(The total nutrition of a share is defined as the sum of all the units' nutrition_num in it). If it is possible, they'll eat their cake happily. Otherwise, they may buy another one =_=b
When cutting the cake, they'll keep their knife moving just along the edge between two units in order to save more nutrition(we can assume that the center of one unit contains the main nurition). They'll cut the cake from the upside to the downside(from line 1 to line m) without moving the knife off the cake. Each cutting must be forward, which means it is not allowed to move to the units in line k-1 next time when the knife is in line k(1 <= k <= m). And to make the two shares they finally get in good shape, they never take the knife up before it reachs the downside.
The picture below shows you a possible cut for a given cake:
In the picture, the two shares they get will be the two parts divided by the bold line. The total nutrition of the left part will be 1+2+4+5+(-6)+0+7+(-1)=12, and the nutrition of the right one will be (-9)+7+2+4+(-3)+1+3+2=7, so there difference is 5.
Now you are given the description for the cake they've got, could you write a program to tell them whether they can achieve their goal?
The input file will contain multiple test cases.
In each case, the first line contains two integers m and n(1 <= m,n <=7) showing the size of the cake.Then m lines follow, each line contains n integers.Every interger represents the nutrition_num for the corresponding unit in the cake.The last line for each case contains only one positive integer t, the expected value they wish the difference of the two shares be.
For each test case, if it is possible to find a way cutting the cake up to their expectation, output the minimum difference you can get in a single line. Output "You'd better buy another one!" in a single line otherwise.
2 21 23 453 31 -2 92 10 33-100 2 47
2You'd better buy another one!
分析:有一个矩阵,让你将其分成左右两个,其差值不能超过t,问能否满足并求其最小值。(左右两边每行都要有)
先预处理一下前缀和,然后直接搜索找最小值
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;#define cl(A) memset(A,0,sizeof A);LL mp[8][8];LL sum[8][8];LL mi;int n,m;LL sum1;void find(int index,int num){ if(index == n){ mi = min(mi,(LL)abs(sum1-2*num)); return ; } for(int i=0;i<m-1;i++){ find(index+1,num+sum[index][i]); }}int main(){ while(~scanf("%d%d",&n,&m)){ cl(mp); cl(sum); sum1 = 0; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ scanf("%lld",&mp[i][j]); if(j) sum[i][j] = mp[i][j] + sum[i][j-1]; else sum[i][j] = mp[i][j]; sum1 += mp[i][j]; } } mi = INF; find(0,0); LL k; scanf("%lld",&k); if(mi <= k) printf("%lld\n",mi); else printf("You'd better buy another one!\n"); } return 0;}
- hdu4762 Cut the Cake
- [概率]Cut the Cake
- Cut The Cake
- HDU 4328 - Cut the cake
- hdu 4328 Cut the cake
- hdu 4328 Cut the cake
- hdu 4328 Cut the cake
- HDU-4328-Cut the cake
- Cut the Cake(大数)
- HDU 4762 Cut the Cake
- HDU-4762 Cut the Cake
- HDU 4328 Cut the cake
- hdu - 4328 - Cut the cake - dp
- hdu 4762 - Cut the Cake(大数)
- hdu 4762 Cut the Cake(高精度&概率)
- hdu 4762 Cut the Cake(高精度)
- hdu4762 Cut the Cake(高次幂)
- hud4328 - Cut the cake(垂线法)
- java内部类读书笔记-链接到外部类
- 四大组件 — Activity显示样式
- 递推算法(顺推)- 斐波那契数列用C++实现
- 通过主动学习的方法进行自动语料标注
- 使用Libreswan配置ipsecvpn
- Cut The Cake
- HTML中的置换元素与非置换元素
- 四大组件 — BroadCastReceiver广播接收者
- 好老师
- 改变光标位置
- 方格选数
- Java综合框架<2>
- 四大组件 — BroadCastReceiver广播接收者-接收 | 发送
- HDU-4475 Downward paths(找规律)