【UvaOJ】【基础题目】【Maths - Misc】 10970 - Big Chocolate
来源:互联网 发布:mysql数据库技术 编辑:程序博客网 时间:2024/05/22 04:58
Big Chocolate
Mohammad has recently visited Switzerland. As he loves his friends very much, he decided to buy some chocolate for them, but as this fine chocolate is very expensive(You know Mohammad is a little BIT stingy!), he could only afford buying one chocolate, albeit a very big one (part of it can be seen in figure 1) for all of them as a souvenir. Now, he wants to give each of his friends exactly one part of this chocolate and as he believes all human beings are equal (!), he wants to split it into equal parts.
The chocolate is an rectangle constructed from unit-sized squares. You can assume that Mohammad has also friends waiting to receive their piece of chocolate.
To split the chocolate, Mohammad can cut it in vertical or horizontal direction (through the lines that separate the squares). Then, he should do the same with each part separately until he reaches unit size pieces of chocolate. Unfortunately, because he is a little lazy, he wants to use the minimum number of cuts required to accomplish this task.
Your goal is to tell him the minimum number of cuts needed to split all of the chocolate squares apart.
Figure 1. Mohammad’s chocolate
The Input
The input consists of several test cases. In each line of input, there are two integers , the number of rows in the chocolate and , the number of columns in the chocolate. The input should be processed until end of file is encountered.
The Output
For each line of input, your program should produce one line of output containing an integer indicating the minimum number of cuts needed to split the entire chocolate into unit size pieces.
Sample Input
2 2
1 1
1 5
Sample Output
3
0
4
——————————————————————
解题思路:
本题是非常简单的一题,但要注意不能用递归法,否则会超时。
正确的方法其实就是
假设上面就是我们要切的巧克力,橙色的边表示切痕,而且是只有橙色边的位置才会有切痕,如果每条短边都视作一刀的话,那么上面那个巧克力显然要切17刀,这也是最坏的情况了,那么刀数为什么可以比17刀少呢?我们不妨观察红点的位置,如果左右刀痕连在了一起那么可以少一刀,同理如果上下刀痕连一起,也可以少一刀,但是上下和左右的刀痕不能同时连一起。那么也就是说如果有一个红色的这样的点,那么就可以使上下刀痕连一起,或者左右刀痕连一起,从而减少一刀,那么一共有6个这样的点,就可以减少6刀,所以最优就是11刀。
————————————————————————————-
代码:
#include<iostream>#include<fstream>#include<vector>using namespace std;int split(int M, int N){if (M == 1 && N == 1)return 0;else if (M >= N)return 1 + split(M / 2, N) + split(M - M / 2, N);elsereturn 1 + split(M, N / 2) + split(M, N - N / 2);}int main(){int M, N;while (cin >> M >> N){//cout<<split(M, N)<<endl;<span style="white-space:pre"></span>cout << M*N - 1<<endl;}}
- 【UvaOJ】【基础题目】【Maths - Misc】 10970 - Big Chocolate
- 【UvaOJ】【基础题目】【Maths - Misc】 846 - Steps
- 【UvaOJ】【基础题目】【Maths - Misc】 10916 - Factstone Benchmark
- 【UvaOJ】【基础题目】【Maths - Misc】 10014 - Simple calculations
- 【UvaOJ】【基础题目】【Maths - Misc】 10790 - How Many Points of Intersection?
- 【UvaOJ】【基础题目】【Maths - Misc】 107 - The Cat in the Hat
- 【UvaOJ】【基础题目】【Maths - Number Theory】 10110 - Light, more light
- 【UvaOJ】【基础题目】【Maths - Number Theory】 408和350 伪随机数
- 【UvaOJ 重要!】【基础题目】【Maths - Number Theory】 10392 - Factoring Large Numbers
- UVa 10970 - Big Chocolate
- uva 10970 Big Chocolate
- UVA 10970 - Big Chocolate
- uva 10970 - Big Chocolate
- 10970 - Big Chocolate
- Uva-10970-Big Chocolate
- UVA 10970 Big Chocolate
- 10970 - Big Chocolate
- UVA 10970 Big Chocolate
- 大数据学习网站
- 为什么要在addsubview:一个view对象后,release它
- 读好书是如此的重要,因为好书往往带领你去到更好的书,更大的世界--书单
- hadoop集群监控工具Apache Ambari安装配置教程
- Oracle删除当前用户下所有的表的方法
- 【UvaOJ】【基础题目】【Maths - Misc】 10970 - Big Chocolate
- 使用无觅APP工场:你也可以拥有自己的APP
- Jackknife,Bootstraping, bagging, boosting, AdaBoosting, Rand forest 和 gradient boosting的区别
- FusionCharts破解方法
- QT动态翻译
- Centos安装Memcached
- UML类图关系大全
- python Queue进阶
- SpringMVC3的ResponseBody返回字符串(JSON)乱码问题解决