三分模板
凸函数的极大值
ll l, r; while(l < r) { ll lmid = l + (r - l) / 3; ll rmid = r - (r - l) / 3; if(calc(lmid) <= calc(rmid)) l = lmid + 1; else r = rmid - 1; } printf("%lld\n", max(calc(l), calc(r)));
凹函数的极小值
ll l, r; while(l < r) { ll lmid = l + (r - l) / 3; ll rmid = r - (r - l) / 3; if(calc(rmid) >= calc(lmid)) r = midr - 1; else l = midl + 1; } printf("%lld\n", min(calc(l), calc(r)));
浮点数模板
凸函数的极大值
double l, r; for(int i = 0; i < 300; i++) { double lmid = l + (r - l) / 3; double rmid = r - (r - l) / 3; if(calc(lmid) <= calc(rmid)) l = lmid; else r = rmid; } printf("%.6f\n", calc(l));
凹函数的极小值
double l, r; for(int i = 0; i < 300; i++) { double lmid = l + (r - l) / 3; double rmid = r - (r - l) / 3; if(calc(rmid) >= calc(lmid)) r = rmid; else l = lmid; } printf("%.6f\n", calc(l));