Time Limit: 1 Sec Memory Limit: 162 MB
Submit: 3237 Solved: 2361
[Submit][Status][Discuss]
Description
windy的生日到了,为了庆祝生日,他的朋友们帮他买了一个边长分别为 X 和 Y 的矩形蛋糕。现在包括windy
,一共有 N 个人来分这块大蛋糕,要求每个人必须获得相同面积的蛋糕。windy主刀,每一切只能平行于一块蛋糕
的一边(任意一边),并且必须把这块蛋糕切成两块。这样,要切成 N 块蛋糕,windy必须切 N-1 次。为了使得
每块蛋糕看起来漂亮,我们要求 N块蛋糕的长边与短边的比值的最大值最小。你能帮助windy求出这个比值么?
Input
包含三个整数,X Y N。1 <= X,Y <= 10000 ; 1 <= N <= 10
Output
包含一个浮点数,保留6位小数。
Sample Input
5 5 5
Sample Output
1.800000
解题思路
此题思考的出发点应该是一个矩形,如果这个矩形长为x,宽为y,还能切k刀,那么切出来的小矩形一定最小为(x/k,y/k) 因为如果不是这样的话肯定不如这个优或是肯定达不到相等的面积,所以dfs求解,每次枚举切的位置,然后转化成子问题递归求解。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const double eps = 1e-8;
int x,y,n;
double s;
inline double dfs(double x,double y,int k){
if(k==1) return max(x,y)/min(x,y);
double ans=1e9;
for(register int i=1;i<=k/2;i++){
double sum1=max(dfs(i*(x/k),y,i),dfs(x-i*(x/k),y,k-i));
double sum2=max(dfs(x,i*y/k,i),dfs(x,y-i*y/k,k-i));
ans=min(ans,min(sum1,sum2));
}
return ans;
}
int main(){
scanf("%d%d%d",&x,&y,&n);
// cout<<s<<endl;
printf("%.6lf",dfs((double)x,(double)y,n));
return 0;
}