Jzzhu and Chocolate(CF-449A)
Problem Description
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 exactly k 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.
Examples
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,k,代表一個 n*m 的矩陣要切 k 次,問切完后最小的單元格的最大值
思路:
假設橫向切了 x-1 次,縱向切了 y-1 次,那么橫著就分為 x 份,縱著就分為 y 份
于是結果即為:res=(n/x)*(m/y),而題目所給范圍為:1<=x<=n,1<=y<=m,(x-1)+(y-1)=k
顯然:(n+m-2)<k 時無解,(n+m-2)=k 時值為 1
可以考慮先枚舉 n/x 的值,那么對于固定的取值,可以選出最大的 x,使得此時 m/y 最大;然后再枚舉? n/y?的值,那么對于固定的取值,可以選出最大的 y,使得此時 n/x?最大,最后取兩者最大值即可
我們將 y=k+2-x 代入?res=(n/x)*(m/y) 中可得到分母是一個二元函數:-y^2+2*y+ky,那么要讓原式最大,就要讓分母最小,因此不需枚舉 x、y 的值,直接令 x、y 分別最小即可
Source Program
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define PI acos(-1.0) #define E 1e-9 #define INF 0x3f3f3f3f #define LL long long const int MOD=1e9+7; const int N=10+5; const int dx[]= {-1,1,0,0}; const int dy[]= {0,0,-1,1}; using namespace std; int main(){int n,m,k;cin>>n>>m>>k;LL res=0;if( (n-1)+(m-1)<k )//刀數多于切口數res=-1;else if( (n-1)+(m-1)==k )//刀數等于切口數res=1;else{LL x=min(n-1,k);//令x最小res=max( res, (n/(x+1))*(m/(k+1-x)) );LL y=min(m-1,k);//令y最小res=max( res, (m/(y+1))*(n/(k+1-y)) );}cout<<res<<endl;return 0; }?
新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!總結
以上是生活随笔為你收集整理的Jzzhu and Chocolate(CF-449A)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: B君的圆锥(51Nod-1629)
- 下一篇: 数列分块入门 6(LibreOj-628