Learn the Extreme Value Problem of the third method with cainiao

Source: Internet
Author: User
Tags cmath

The afternoon sun is so bright. If the temperature is not too high, it would be nice to go to the West Lake to see the scenery. I thought, now the West Lake should be a peaceful lake, with countless notifications playing a symphony on willow branches. The boy gave the girl Tang Jing seven years. Alas, but he was wrong. The girl didn't love him at all, but his perseverance and dedication made me very touched, maybe you are not like him to make yourself in the current situation. Maybe. With emotion. But now it is quite good. When I go to work, I write articles, and the weather is cool, I can play around. Hangzhou is a good place for tourism and leisure. It's a bit difficult to get back after dinner.

Back to the question, now that the Division is over, let's continue with the three-way approach. The bipartite method is used to obtain monotonic values. For example, if an array is sorted, It is ascending or descending. How can I find the greatest value of a quadratic function?

His meaning has been lost for a long time. However, we can still use the three-way method to achieve this, that is, to use the second to second. For example, we define L and R, M = (L + r)/2, mm
= (Mid + r)/2; If mid is near the extreme point, r = mm; otherwise, if mm is near the extreme point, L = m; in this case, the extreme value can still be obtained. Let's take a look at the specific question.

 

Zoj 3203 light bulb

--------------------------------------------------------------------------------

Time Limit: 1 second memory limit: 32768 KB

 

--------------------------------------------------------------------------------

Compared to wildleopard's wealthiness, his brother mildleopard is rather poor. His house is narrow and he has only one light bulb in his house. Every night, he is wandering in his incommodious house,
Thinking of how to earn more money. one day, he found that the length of his shadow was changing from time to time while walking between the light bulb and the wall of his house. A sudden thought ran through his mind and he wanted to know the maximum length
Of his shadow.

 

Input

 

The first line of the input contains an integer T (t <= 100), indicating the number of cases.

 

Each test case contains three real numbers H, H and D in one line. H is the height of the light bulb while H is the height of mildleopard. D is distance between the light bulb and the wall. all numbers
Are in range from 10-2 to 103, both wide Sive, and h-h> = 10-2.

 

Output

 

For each test case, output the maximum length of mildleopard's shadow in one line, accurate up to three decimal places ..

 

Sample Input

 

 

3

2 1 0.5

2 0.5 3

4 3 4

 

Sample output

 

 

1.000

0.750

4.000

The meaning of the question is very simple, that is, people move around and find the longest length of the Shadow L.

It can be seen from the figure that when a person walks in, the shadow is the longest when he does not cast on the wall. Then the Shadow becomes smaller on the wall, so you can use the three-way method to calculate the value. Of course, you can still solve the problem by using the high-number derivation, but pay attention to the definition domain.

#include <stdio.h>int main(){ int t; scanf("%d", &t); while(t--) {  double h, H, D;  scanf("%lf%lf%lf", &H, &h, &D);  double left = 0, right = D * h / H;   int size = 100;  double mid, midmid, ans1, ans2;         while(size--)  {    mid = (left + right) / 2;    midmid = (mid + right) / 2;      ans1 = ((h*D-H*mid)/(H-h)*H)/((h*D-H*mid)/(H-h)+D)+mid;     ans2 = ((h*D-H*midmid)/(H-h)*H)/((h*D-H*midmid)/(H-h)+D)+midmid;    if(ans1 > ans2) right = midmid;             else left = mid;  }  printf("%.3lf\n", ans1); }return 0;}

 

This is basically a template, but the function needs to be written by itself. As long as the function is solved, it will always let him calculate the Extreme Value cyclically. This point can be found almost 100 times.

 

Let's take a look at a question.

HDU 2438 turn the corner
Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 404    Accepted Submission(s): 103
Problem Description
Mr. West bought a new car! So he is travelling around the city.One day he comes to a vertical corner. The street he is currently in has a width x, the street he wants to turn to has a width y. The car has alength l and a width d. Can Mr. West go across the corner?
 
Input
Every line has four real numbers, x, y, l and w. Proceed to the end of file.
 
Output
If he can go across the corner, print "yes". Print "no" otherwise.
 
Sample Input
10 6 13.5 4
10 6 14.5 4
 
 
Sample Output
yes
no

 

 

In fact, it is the question of whether a car can be bent. tell you whether X, Y, L, and D can be bent. We know that if D is greater than X and Y, then the car will not be able to pass. The next step is to find the best value by three points. If H is smaller than Y at this point, it can be turned.

 

#include <cstdio>#include <cmath>const double pi = 3.1415926535;int main(){      double x, y, l, w;      while(scanf("%lf%lf%lf%lf", &x, &y, &l, &w) != EOF)      {            int size = 100;            double left = 0, right = pi / 2;            double mid = 0, midmid = 0, ans1, ans2;            if(x < w || y < w){printf("no\n"); continue;}              while(size--)            {                  mid = (left+right) / 2;                  midmid = (mid+right) / 2;                  ans1 = (x-l*sin(mid)-w/cos(mid)) / tan(mid);                  ans2 = (x-l*sin(midmid)-w/cos(midmid)) / tan(midmid);                  if(ans1 < ans2) right = midmid;                  else left = mid;            }            if(fabs(ans1) > y) printf("no\n");            else printf("yes\n");      }return 0;}

 

These two questions are almost a three-point template, followed by a simple formula derivation, which can be easily implemented. Let's take a look at the complicated points.
Xmu 1125 SUV Competition

Description

Thebeet is participating in an off-road vehicle competition. The venue is shown on the right: there are three parts in total. The length and width of each part of the ground are N and M, but the surface conditions are different, and the maximum speed of off-road vehicles on this section is also different. The blue line indicates the possible route of thebeet.

The requirement of the competition is to require contestants to drive from the upper left corner of the Competition Venue to the lower right corner. Thebeet wants to know how long he can complete the game if he is driving at the fastest speed on all road sections (regardless of the acceleration phase.

Input

The first line of the input data is two positive integers, n m (n <= 3000, m <= 1000), indicating the length and width of a pavement.
The second behavior contains three positive integers S1, S2, and S3 (0 <S1, S2, S3 <= 100), which indicate the highest speed of an off-road vehicle on each road.

Output

Output a real number to indicate the shortest time that thebeet can complete the competition. Please output a number that is as accurate as possible and the control error is within ± 0. 000001.

Sample Input

30 10
2 5 3

Sample output

13.7427361525

Hint

If the difference between your output and the result is within 0.000001, the answer is correct.

 

After analyzing this question, we actually need to find the most value. However, the most value is to meet the requirements of two places. What is the relationship between this question and the three points? In fact, it is the same. It only takes two or three minutes to meet not only the first requirement, but also the second requirement, then the most important value is what the question requires. The following is the code:

 

#include <iostream>#include <cstdio>#include <cmath>using namespace std;const double pi = 3.141592654;inline double dis(double x1, double y1, double x2, double y2){         return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));} int main(){         double n, m, a, b, c;         while(cin >> n >> m)         {                  cin >> a >> b >> c;                  double left1 = 0, right1 = pi / 2;                  double mid1, midmid1, mid2, midmid2, ans1, ans11, ans2, ans3, ans4, ans5, ans44, ans55;                  int size = 30;                  while(size--)                  {                           mid1 = (left1 + right1) / 2;                           midmid1 = (mid1 + right1) / 2;                           ans1 = dis((n - m / tan(mid1)), m, n, 0);                           ans11 = dis((n - m / tan(midmid1)), m, n, 0);                           int size1 = 30;                           double left2 = 0, right2 = pi / 2;                           while(size1--)                           {                                    mid2 = (left2 + right2) / 2;                                    midmid2 = (mid2 + right2) / 2;                                    ans2 = dis((n - m / tan(mid1)), m, (n - m / tan(mid1) - m / tan(mid2)), 2*m);                                    ans3 = dis((n - m / tan(mid1)), m, (n - m / tan(mid1)- m / tan(midmid2)), 2*m);                                    ans4 = ans1 / c + ans2 / b + dis(0, 3*m, (n - m / tan(mid1) - m / tan(mid2)), 2*m) / a;                                    ans5 = ans1 / c + ans3 / b + dis(0, 3*m, (n - m / tan(mid1) - m / tan(midmid2)), 2*m) / a;                                    if(ans4 > ans5) left2 = mid2;                                    else right2 = midmid2;                           }                           left2 = 0, right2 = pi / 2;         size1 = 30;         while(size1--)         {                  mid2 = (left2 + right2) / 2;                  midmid2 = (mid2 + right2) / 2;                   ans2 = dis((n - m / tan(midmid1)), m, (n - m / tan(midmid1) - m / tan(mid2)), 2*m);                  ans3 = dis((n - m / tan(midmid1)), m, (n - m / tan(midmid1) - m / tan(midmid2)), 2*m);                  ans44 = ans11 / c + ans2 / b + dis(0, 3 * m, (n - m / tan(midmid1) - m / tan(mid2)), 2*m) / a;                  ans55 = ans11 / c + ans3 / b + dis(0, 3 * m, (n - m / tan(midmid1) - m / tan(midmid2)),2*m) /a;                  if(ans44 > ans55) left2 = mid2;                  else right2 = midmid2;   }   if(ans4 > ans44) left1 = mid1;   else right1 = midmid1;  }  printf("%.10lf\n", ans4); }return 0;}

 

At this point, the three-way method is like this. It is very simple. In summary, the formula is required and then the framework is called. As long as you understand the principle, the question is not changed. Take a nap and go back to the company to read the book. In this era, if you do not read books or learn more, you will be eliminated by the times ......

 

 

 

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.