这题上来看以为是dp,其实是搜索,我顺便把搜索的路径存下来了(这样才看明白样例......)。 —— by VANE
#include<bits/stdc++.h> #define MP make_pair #define xx first #define yy second using namespace std; double a,b; int n; typedef pair<pair<double,double>,int> pa; map<pa,pa>pre; double dfs(double x,double y,int k) { if(k==1) return max(x/y,y/x); double ans=1e9; for(int i=1;i<=k/2;++i) { double nx=x/k*i,ny=y/k*i; double t=max(dfs(nx,y,i),dfs(x-nx,y,k-i)); if(t<ans) { ans=t; pre[MP(MP(x,y),k)]=MP(MP(nx,y),i); } t=max(dfs(x,ny,i),dfs(x,y-ny,k-i)); if(t<ans) { ans=t; pre[MP(MP(x,y),k)]=MP(MP(x,ny),i); } } return ans; } void print(double x,double y,int k) { double na,nb; int nn; if(k<=1) return; pa p=MP(MP(x,y),k); na=pre[p].xx.xx; nb=pre[p].xx.yy; nn=pre[p].yy; printf("%.2lf %.2lf %d\n",na,nb,nn); print(na,nb,nn); if(nb==y) { printf("%.2lf %.2lf %d\n",x-na,nb,k-nn); print(x-na,nb,k-nn); } else { printf("%.2lf %.2lf %d\n",x,y-nb,k-nn); print(na,y-nb,k-nn); } } int main() { scanf("%lf%lf%d",&a,&b,&n); printf("%.6lf\n",dfs(a,b,n)); print(a,b,n); }