1. 程式人生 > >BZOJ1024: [SCOI2009]生日快樂

BZOJ1024: [SCOI2009]生日快樂

print () 搜索 include pre urn its clas zoj1024

這題上來看以為是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); }

BZOJ1024: [SCOI2009]生日快樂