Obtain the extreme value using the three-way Method

Source: Internet
Author: User
Tags cmath


The bipartite method is used to solve the Extreme Value Problem of monotonic functions.Method is used to solve the Extreme Value Problem of convex functions.

Therefore, when using the three-point function, you must note that the function is concave and convex.

Template:

Double CAL (){//..... return 0.0 according to the requirement of the question.} void solve () {double left, right, mid1, mid2, ANS, flg1, flg2; left = 0.0; Right = 1000.0; while (right-left> EPS) {mid1 = (2 * left + right)/3; mid2 = (left + 2 * right)/3; flg1 = CAL (); // specific implementation of flg2 = CAL (); If (flg1> flg2) Right = mid2; elseleft = mid1;} ans = CAL (); // left}
 
 
 

Example:

HDU 3400

There are two line segments AB and CD on the plane. The walking speed on AB is P, the walking speed on CD is Q, and the walking speed in other areas is R, the shortest time from A to D.

// ================================================ ==================================================================== // Name: hdu3400 three points. CPP // Author: xinge008 // version: // copyright: Your copyright notice // Description: Hello world in C ++, ANSI-style // ========================================== ========================================================== = # include <iostream> # include <cstring> # include <cstdio> # include <cmath> # include <algorithm> # include <string> using namespace STD; const double EPS = 1e-12; Double P, Q, R; struct node {Double X, Y ;}; node A, B, C, D; node dist (node N1, node N2) {node ans; ans. X = (n1.x + n2.x)/2; ans. y = (n1.y + n2.y)/2; return ans;} double CAL (node N1, node N2) {double T1 = (n1.x-n2.x) * (n1.x-n2.x ); double t2 = (n1.y-n2.y) * (n1.y-n2.y); Return SQRT (t1 + T2);} double run1 (node da) {node left = C, Right = D; double max = 100, min = 0; // double max = CAL (left, right)/Q, min = 0; while (FABS (max-min)> EPS) {node mid1 = dist (left, right); node mid2 = dist (mid1, right); min = CAL (mid1, DA)/R + CAL (mid1, d) /Q; max = CAL (mid2, DA)/R + CAL (mid2, d)/Q; If (Min <= max) Right = mid2; elseleft = mid1 ;} returnmax;} double run2 () {node left = A, right = B; double max = 100, min = 0; // double min = 0, max = CAL (, b)/P; while (FABS (max-min)> EPS) {node mid1 = dist (left, right); node mid2 = dist (mid1, right ); min = CAL (mid1, A)/P + run1 (mid1); max = CAL (mid2, A)/P + run1 (mid2); If (Min <= max) right = mid2; elseleft = mid1;} return Max;} void Gao () {int t; scanf ("% d", & T); While (t --) {scanf ("% lf", &. x, &. y, & B. x, & B. y); scanf ("% lf", & C. x, & C. y, & D. x, & D. y); scanf ("% lf", & P, & Q, & R); printf ("%. 2lf \ n ", run2 () ;}} int main () {Gao (); Return 0 ;}

Hdu3714

Function f [x] = max {Si (x )}
(I =,..., n) where Si (X) is a quadratic function, and the minimum value of function f [x] is obtained.

// ================================================ ==================================================================== // Name: 3. CPP // Author: xinge008 // version: // copyright: Your copyright notice // Description: Hello world in C ++, ANSI-style // ========================================== ========================================================== = # include <iostream> # include <cstring> # include <string> # include <cmath> # include <algorithm> # include <cstdio> using namespace STD; const int maxn = 10010; const double EPS = 1e-12; struct node {double A, B, C;} data [maxn]; double _ max (double A, double B) {If (A> B) return a; return B;} double cal1 (int I, double da) {return data [I]. A * da + data [I]. B * da + data [I]. c;} double cal2 (Double X, int N) {double ans =-1e10; For (INT I = 1; I <= N; I ++) ans = _ max (ANS, cal1 (I, x); Return ans;} void CAL (INT N) {double left, right, ANS, mid1, mid2; left = 0; Right = 1000.0; while (right-left> EPS) {// here is the high precision requirement mid1 = (2 * left + right)/3; mid2 = (left + 2 * right)/3; double flg1 = cal2 (mid1, n); double flg2 = cal2 (mid2, n); If (flg1> flg2) {left = mid1;} else {right = mid2;} ans = cal2 (left, n); printf ("%. 4lf \ n ", ANS) ;}int main () {int t; scanf (" % d ", & T); While (t --) {int num; scanf ("% d", & num); For (INT I = 1; I <= num; I ++) {scanf ("% lf ", & Data [I]. a, & Data [I]. b, & Data [I]. c) ;}cal (Num) ;}return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.