By about two thirds fix points, determine the level of the two points, then give it a third of the distance, to find extreme points of convex function.
- If
lmid
andrmid
the same side of the maximum (minimum) value: then since monotonicity, both must be greater (smaller) value that best be near, far point interval corresponding to the not contain the most value, It can be discarded. - If both sides: As the most value in the middle of the two, we give up a range on both sides, it will not affect most value, it can be discarded.
Example 1: P3382 [template] trichotomy
Meaning of the questions: one-third template questions, find the n-th extreme points in [l, r] function, to five decimal places to ensure that there is an answer.
(This question is a very interesting solution to a problem, what the hell are out, obviously three-point template, but why there are a lot of big brothers to join in the fun, PSO, derivation + half, simulated annealing algorithm, golden section method, gradient descent or four grading, leaving tears ignorant)
#include<stdio.h> #include<iostream> #include<algorithm> #include<cstring> #include<math.h> #include<string> #include<map> #include<queue> #include<stack> #include<set> #include<ctime> #define ll long long #define inf 0x3f3f3f3f const double pi=3.1415926; using namespace std; double a[15]; int n; double l,r; double cal(double x){///计算n次函数在x点的值 double res=0; for(int i=n;i>=0;i--) res+=a[i]*pow(x, i); return res; } int main() { scanf("%d%lf%lf",&n,&l,&r); for(int i=n;i>=0;i--) scanf("%lf",&a[i]); Double EPS = 0.0000001 ; /// reduce the error the while (FABS (rl is an)> = EPS) { Double m = (rl is an) / . 3 ; /// third distance Double LM = L + m; /// left third point Double RM = RM; /// the right third point IF (CAL (LM) < CAL (RM)) L = LM; the else R & lt = RM; } the printf ( " % .5f \ n- " , L); return 0 ; }
In addition, I do not know why all the overtime Java code, copy the code exactly the same way, please advise.
import java.util.*; public class Main{ static double[] a=new double[15]; static int n; static double eps=0.0000001; public static void main(String[] args) { Scanner scan=new Scanner(System.in); n=scan.nextInt(); double l=scan.nextDouble(); double r=scan.nextDouble(); for(int i=n;i>=0;i--) a[i]=scan.nextDouble(); double ans=0; while(Math.abs(r-l)>eps) { double m=(r-l)/3; double lm=l+m;; double rm=r-m;; if(cal(lm)<=cal(rm)) l=lm; else r=rm; } System.out.printf("%.5f\n",l); } public static double cal(double x) { double res=0; for(int i=n;i>=0;i--) res+=a[i]*Math.pow(x, i); return res; } }
Example 2: Cattle-off practice match 59C synthesis equipment
The meaning of problems: there is a piece of material x and material y member b, 2 a piece of material and the material (b) 3 can be equipped with an A, 4 a piece of material and a material (b) may be a piece of equipment B, most do seek how much equipment. 1 <= a, b <= 1e9
Thinking: Suppose a member made equipment A, B are equipped min ((x-2a) / 4, y-3a), the answer ans = a + min ((x -2a) / 4, y-3a). Bigwigs said: easy to see is a convex function, then look for three points.
After the match up question, problem-third explanation is that one-third, says big brother playing table can be seen is the third, the question could be done much have experience of it, the code comes to play table function can be like big brothers the same test.
Pit discovered by accident: one-third the third, a beginner like me would like to set conditions while set to rl <= 3, just 3 may be in addition to, the satisfaction of all . But for an integer, rounding problem to be considered, if this condition is provided while, if the judge added an equal sign and an equal sign does not lead to the same result, can modify the code to check, detailed look at the code. To ensure accuracy, the final out of the l and r the best was a little interval slightly larger, go to traverse extreme value, steady!
Import Classes in java.util *. ; public class Main { // cow-off exercise 59C static int the X-, the y-; public static void main (String [] args) { Scanner Scan = new new Scanner (System.in); / * bigwigs He said playing table, such a thing is really dabiao (300, 300); dabiao (60, 10); * / int T = scan.nextInt (); the while (T = 0! ) { T - ; X = Scan .nextInt (); Y =scan.nextInt (); int L = 0, R & lt Math.min = (X / 2, Y / 3 ); the while (rl is an> =. 9) { // this difference not use 3, with a slightly larger section int m = (rl is an) /. 3 ; int LM = L + m; int RM = RM ; // System.out.printf ( "% D = CAL LM (LM) =% D% RM = D CAL (RM) % D = \ n-", LM, CAL (LM), RM, CAL (RM)); IF (CAL (LM) <CAL (RM)) // the while set to rl> = 3 after the equal sign plus without The fourth set of data to be wrong. = L LM; the else R & lt = RM; } //System.out.printf ( "L% DR = D =% \ n-", L, R & lt); int ANS = 0 ; for ( int I = L; I <= R & lt; I ++) // just above a locking section , could not find the best value, the interval is not more than about 3, then traverse at ANS = Math.max (ANS, CAL (I)); System.out.println (ANS); } } public static int CAL ( int A) { int RES Math.min = a + ((X-2 * a) /. 4, Y *. 3- a); return RES; } public static void dabiao ( int X, int Y) { // big brother said playing table // from 0 A to make all the equipment to make the equipment A System.out.printf ( "playing table Dy =% =% X D \ n-" , X, Y); for ( int I = 0; I <Math.min (X / 2, Y /. 3); I ++ ) { int B Math.min = ((2 * X-I) /. 4, Y *. 3- I); System.out.printf ( "DB% =% A = % D = SUM D \ n-", I, B, I + B); } System.out.println (); } }