Codeforces Round #257 (Div. 1)449A - Jzzhu and Chocolate(贪心、数学)
2014-07-21 10:44
495 查看
题目链接:http://codeforces.com/problemset/problem/449/A
A. Jzzhu and Chocolate
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Jzzhu has a big rectangular chocolate bar that consists of n × m unit squares. He wants to cut this bar exactly k times.
Each cut must meet the following requirements:
each cut should be straight (horizontal or vertical);
each cut should go along edges of unit squares (it is prohibited to divide any unit chocolate square with cut);
each cut should go inside the whole chocolate bar, and all cuts must be distinct.
The picture below shows a possible way to cut a 5 × 6 chocolate for 5 times.
Imagine Jzzhu have made k cuts and the big chocolate is splitted into several pieces. Consider the smallest (by area) piece of the chocolate, Jzzhu wants
this piece to be as large as possible. What is the maximum possible area of smallest piece he can get with exactlyk cuts? The area of a chocolate piece
is the number of unit squares in it.
Input
A single line contains three integers n, m, k (1 ≤ n, m ≤ 109; 1 ≤ k ≤ 2·109).
Output
Output a single integer representing the answer. If it is impossible to cut the big chocolate k times, print -1.
Sample test(s)
input
output
input
output
input
output
Note
In the first sample, Jzzhu can cut the chocolate following the picture below:
In the second sample the optimal division looks like this:
In the third sample, it's impossible to cut a 2 × 3 chocolate 4 times.
题意:给出一个 n * m 大小的chocolate
bar,你需要在这个bar上切 k 刀,使得最小的部分面积尽可能大,求出这个被划分后的最小部分面积最大可以为多少。如果这个chocolate bar 不能切成 k 部分,则输出-1。注意,每一刀需要符合3个条件:1、打横切或打竖切; 2、每一刀只能经过unit square(即1*1的单元bar)的边,也就是说不能把一个单元bar损坏,要完整; 3、每一刀只能在整个chocolate bar的里面操作,也就是说,外围的四条边是不允许切的。4、每一刀都是不相同的。
思路转载:
首先要知道什么时候这个大bar不能切成 k 刀。很容易知道是如果k > (n-1)+(m-1) 的情况,因为外围的四条边是不允许操刀的!排除这个不能切的情况后,那么就要根据是从n(打横切)还是从m(打竖切)来进行讨论了。不过由于我们不能一眼看出哪种方案更优,所以两者都要讨论下,我一开始只想到 if 中的两条式子,n/(k+1)的意思表示这 k 刀都打横切,而分母为什么是k+1而不是k,是因为一刀可以把一个区域分成两部分,两刀就三个部分,依次类推。而我们需要求的是面积,就需要用到部分,而不是刀数了。m/(k+1)依此类推。
不过问题出现了,我根据test10返回的wa结果来想出的^_^。有可能完全打横切或者打竖切都没有切够k刀!那么就需要把剩余的刀数分到打竖切(对应之前完全打横切)或者打横切(对应之前的完全打竖切)中了。也就是代码中else的部分。其实完整的a1表达式是这样的:n/(n-1) * m/(k-(n-1)+1),意思:完全打横切,只能切n-1刀,那么它划分的最小部分的面积就充当1了,至于m/(k-(n-1)+1)
表示 打竖切还能切多少刀,+1是因为是求分成的部分,而不是多少刀,与if中的n/(k+1)中的+1意思是相同的。
(好开心这道题目排到最少用时的26名,继续努力! ^_^)
代码如下:
----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋 :http://user.qzone.qq.com/593830943/main
----------------------------------------------------------------------------------------------------------------------------------------------------------
A. Jzzhu and Chocolate
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Jzzhu has a big rectangular chocolate bar that consists of n × m unit squares. He wants to cut this bar exactly k times.
Each cut must meet the following requirements:
each cut should be straight (horizontal or vertical);
each cut should go along edges of unit squares (it is prohibited to divide any unit chocolate square with cut);
each cut should go inside the whole chocolate bar, and all cuts must be distinct.
The picture below shows a possible way to cut a 5 × 6 chocolate for 5 times.
Imagine Jzzhu have made k cuts and the big chocolate is splitted into several pieces. Consider the smallest (by area) piece of the chocolate, Jzzhu wants
this piece to be as large as possible. What is the maximum possible area of smallest piece he can get with exactlyk cuts? The area of a chocolate piece
is the number of unit squares in it.
Input
A single line contains three integers n, m, k (1 ≤ n, m ≤ 109; 1 ≤ k ≤ 2·109).
Output
Output a single integer representing the answer. If it is impossible to cut the big chocolate k times, print -1.
Sample test(s)
input
3 4 1
output
6
input
6 4 2
output
8
input
2 3 4
output
-1
Note
In the first sample, Jzzhu can cut the chocolate following the picture below:
In the second sample the optimal division looks like this:
In the third sample, it's impossible to cut a 2 × 3 chocolate 4 times.
题意:给出一个 n * m 大小的chocolate
bar,你需要在这个bar上切 k 刀,使得最小的部分面积尽可能大,求出这个被划分后的最小部分面积最大可以为多少。如果这个chocolate bar 不能切成 k 部分,则输出-1。注意,每一刀需要符合3个条件:1、打横切或打竖切; 2、每一刀只能经过unit square(即1*1的单元bar)的边,也就是说不能把一个单元bar损坏,要完整; 3、每一刀只能在整个chocolate bar的里面操作,也就是说,外围的四条边是不允许切的。4、每一刀都是不相同的。
思路转载:
首先要知道什么时候这个大bar不能切成 k 刀。很容易知道是如果k > (n-1)+(m-1) 的情况,因为外围的四条边是不允许操刀的!排除这个不能切的情况后,那么就要根据是从n(打横切)还是从m(打竖切)来进行讨论了。不过由于我们不能一眼看出哪种方案更优,所以两者都要讨论下,我一开始只想到 if 中的两条式子,n/(k+1)的意思表示这 k 刀都打横切,而分母为什么是k+1而不是k,是因为一刀可以把一个区域分成两部分,两刀就三个部分,依次类推。而我们需要求的是面积,就需要用到部分,而不是刀数了。m/(k+1)依此类推。
不过问题出现了,我根据test10返回的wa结果来想出的^_^。有可能完全打横切或者打竖切都没有切够k刀!那么就需要把剩余的刀数分到打竖切(对应之前完全打横切)或者打横切(对应之前的完全打竖切)中了。也就是代码中else的部分。其实完整的a1表达式是这样的:n/(n-1) * m/(k-(n-1)+1),意思:完全打横切,只能切n-1刀,那么它划分的最小部分的面积就充当1了,至于m/(k-(n-1)+1)
表示 打竖切还能切多少刀,+1是因为是求分成的部分,而不是多少刀,与if中的n/(k+1)中的+1意思是相同的。
(好开心这道题目排到最少用时的26名,继续努力! ^_^)
代码如下:
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; #define LL __int64 int main() { LL n, m, k; LL ans1, ans2; while(~scanf("%I64d%I64d%I64d",&n,&m,&k)) { if(n-1+m-1 < k) { printf("-1\n"); continue; } if(n >= k+1 || m >= k+1) { ans1 = n/(k+1)*m; ans2 = m/(k+1)*n; } else { ans1 = n/(k-(m-1)+1)*1; ans2 = m/(k-(n-1)+1)*1; } LL ans = max(ans1, ans2); printf("%I64d\n",ans); } return 0; }
相关文章推荐
- CF Codeforces Round #257 (Div. 1) A (449A) Jzzhu and Chocolate
- Codeforces Round #253 (Div. 2) D. Andrey and Problem (贪心+数学概率)
- Codeforces Round #257 (Div. 1) D. Jzzhu and Numbers
- Codeforces Round #257 (Div. 2) B Jzzhu and Sequences
- Codeforces Round #272 (Div. 2) C. Dreamoon and Sums (数学 思维)
- Codeforces Round #280 (Div. 2) C. Vanya and Exams 贪心
- 【Codeforces Round 332 (Div 2)D】【数学 公式推导】Spongebob and Squares 正方形数量恰好为x个的所有大矩形
- Codeforces Round #288 (Div. 2)C. Anya and Ghosts(模拟+贪心)
- Codeforces Round #280 (Div. 2) C. Vanya and Exams 贪心
- Codeforces Round #293 (Div. 2) C. Anya and Smartphone 数学题
- 【Codeforces Round 330 (Div 2)E】【贪心 暴力】Edo and Magnets 给定矩形最多去除m个,最小面积矩形使得覆盖所有小矩形重心
- 【Codeforces Round 326 (Div 2)A】【贪心】Duff and Meat 屯肉前溯花费最低
- Codeforces Round #280 (Div. 2) E. Vanya and Field (数学GCD)
- Codeforces Round #331 (Div. 2)-Wilbur and Array(贪心模拟)
- Codeforces Round #253 Div2 D.Andrey and Problem 概率+贪心
- 【Codeforces Round 331 (Div 2)B】【水题 贪心】Wilbur and Array 修改对p位置后的所有位置生效 最少修改次数使得数列变成b
- Codeforces Round #280 (Div. 2) D. Vanya and Computer Game 数学+预处理
- Codeforces Round #169 (Div. 2) D. Little Girl and Maximum XOR(贪心,中等)
- Codeforces Round #277.5 (Div. 2)——C贪心—— Given Length and Sum of Digits
- Codeforces Round #330 (Div. 2)——B 数学——Pasha and Phone