1874 football game(三分法and method to compute the area of trianngle)
FInd the max area.
1. 三分法
2. NAN (not comparable with number)
http://acm.timus.ru/problem.aspx?space=1&num=1874
#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> using namespace std; #define EPS 1e-9 //1874 to calculate the max area //divide quardnage into two areas //helper function to cal the area double cal_area(double a, double b, double c) { //check if theree exists triangle, otherwise, it will rturn nan, which is not comparable if(c + a <= b || c + b <=a || a+b <=c) return -1; double s = (a+b+c)/2.0; double s1 = c*c/4; double s2 = sqrt(s*(s-a)*(s-b)*(s-c)); return s1+s2; } int main(){ //freopen("input.txt","r",stdin); double a = 0, b = 0; cin >> a; cin >> b; double c = 0; //uble s1 = 0, s2 = 0; double max = 0; //the range of c is [0 a+b] double l = 0, r = a+b; double m,mm; double mv, mmv; while(l + EPS < r) { m = (r-l)/2 + l; mm = (r-m)/2 + m; mv = cal_area(a,b,m); //printf("%f\n", mv); mmv = cal_area(a, b, mm); //find first construct area if(mv <= mmv) l = m;// edge case if mv == mmv(move to bigger one tot get bigger c) else r = mm; } printf("%.7f\n",mv, mmv, mv > mmv ? mv : mmv); return 0; }