二分法只适用与线性函数,当函数脱离线性而呈现凸性或者凹性的时候,三分是很有必要的。
三分过程如下图:
凸函数:
凹函数:
实现方法:
double Calc(double p) { /*...*/ } double Solve(double MIN, double MAX) { double Left, Right; double mid, midmid; double mid_area = 0, midmid_area = 0; //*** Left = MIN; Right = MAX; while (Left + eps < Right) { mid = (Left + Right) / 2; midmid = (mid + Right) / 2; mid_area = Calc(mid); midmid_area = Calc(midmid); if (midmid_area - mid_area > eps) Right = midmid; else Left = mid; } return mid_area; }
例题:HDU 4355 ( Party All the Time )
View Code
#include#include #include #include #include #include #include <string> #include <set> #include #include #include
POJ 3301
方法,对坐标系进行(0, 180]度的旋转,然后每个点得到新的坐标,找到最上面,最下面,最左面和最右面的点,然后就行确定当前旋转角度的面积。
x' = x*cos(th) + y*sin(th);
y' = y*cos(th) - x*sin(th);
View Code
//#pragma comment(linker,"/STACK:327680000,327680000") #include#include #include #include #include #include #include <string> #include <set> #include #include #include #include #include