P3382 [template] trichotomy

Topic Link

It is strongly recommended with computing function zero dichotomy This question is to do comparison.

Title Description

If that is given a function of N times, to ensure that the range [l, r] in the memory point x, such that [l, x] on a monotone increasing, [x, R & lt] on a monotone decreasing. Test determined the value of x.

Input and output formats

Input formats:

The first line contains a first integer N and two real numbers l, as shown in r, the meanings as described in the title.

The second line contains N + 1 real numbers, the descending order of coefficients representing the function of N times.

Output formats:

Output line, comprising a real number, is the value of x. Rounded to five decimal places.

Sample input and output

Input Sample # 1:

3 -0.9981 0.5
1 -3 -3 1

Output Sample # 1:

-0.41421

Explanation

Constraints of time: 50ms, 128M

Data Scale:

To 100% of the data: 7 <= N <= 13

Sample Description:

As shown, the function f (x) = x ^ 3-3x ^ 2-3x + 1 is the image in the red segment [-0.9981,0.5] on interval.

When x = -0.41421 the highest point in the image, it is a monotonically increasing function in the case [l, x], [x, r] the monotonically decreasing, so that x = -0.41421, -0.41421 output.

(Range Tip.l & r is not very large ww no more than one digit)

 

Analysis of Algorithms

As the title, it is to use the three-point algorithms directly sets the template.

The so-called three-point algorithm I am a teacher Cao "Informatics Olympiad a pass-rise piece" divide and conquer algorithm chapters first contact. Text as shown in FIG.

Here is the code AC to this question:

 1 #include<stdio.h>
 2 #include<math.h>
 3 int N;
 4 double a[20];
 5 double f(double x)
 6 {
 7     double res=0;
 8     int i;
 9     for(i=N;i>=0;i--) res=res+a[i]*pow(x,i);
10     return res;
11 }
12 int main()
13 {
14     freopen("p3382.in","r",stdin);
15     int i;
16     double L,R,m1,m2;
17     double t1,t2;
18     scanf("%d%lf%lf",&N,&L,&R);
19     for(i=N;i>=0;i--) scanf("%lf",&a[i]);
20     while(L+1e-6<R)
21     {
22         m1=L+(R-L)/3; m2=R-(R-L)/3;
23         t1=f(m1);  t2=f(m2);
24         if(t1<t2) L=m1;
25         else R=m2;
26     }
27     printf("%.5lf",L);
28     return 0;
29 }

 

Guess you like

Origin www.cnblogs.com/huashanqingzhu/p/10960681.html