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

BZOJ 1024: [SCOI2009]生日快樂

程式碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <cassert>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define rrep(i,a,b) for(int i=(a);i>=(b);--i)
#define clr(a,x) memset(a,(x),sizeof(a));
#define eps 1e-8
#define LL long long
#define mp make_pair
double avg_area;
const double inf=1e15;
typedef pair<double,double> State;

int dcmp(double a,double b)
{
    if(a<b-eps) return -1;
    else if(a>b+eps) return 1;
    return 0;
}

map<State,double> dp;

inline double min(double a,double b) { return a<b?a:b; }
inline double max(double a,double b) { return a>b?a:b; }

double gao(double,double);
double dfs(double,double);
double gao(double X,double Y)
{
    double diff=avg_area/X;
    double d=inf;
    for(int i=1;;++i) {
        if(dcmp(diff*i,Y)>=0) break;
        d=min(d,max(dfs(X,diff*i),dfs(X,Y-diff*i)));
    }
    return d;
}

double dfs(double X,double Y)
{
    if(X>Y) swap(X,Y);
    if(dcmp(X*Y,avg_area)==0) return Y/X;
    State s=mp(X,Y);
    if(dp.count(s)) return dp[s];
    double d=gao(X,Y);
    d=min(d,gao(Y,X));
    return dp[s]=d;
}


int main()
{
    int X,Y,N;
    while(scanf("%d%d%d",&X,&Y,&N)==3) {
        dp.clear();
        avg_area=(double)(X*Y)/N;
        double ans=dfs(X,Y);
        printf("%.6lf\n",ans+eps);
    }
}