Num 23:hdoj:2199 Can solve this equation? + hdoj:1969 Pie [dichotomy]

Source: Internet
Author: User


We usually do this when we are dealing with 0 points of a function in computational mathematics:

1. First judge the 0 point in a monotone interval [a, b] (assuming an increment interval);

2. Then determine if f (x) is zero at (a+b)/2 [A/b midpoint ];

3. If the midpoint is greater than 0, b= (a+b)/2; otherwise, similarly, a= (a+b)/2;

4. Repeat 2.3. process until 0 points are found, or the accuracy is satisfied;


(Image from Baidu):


C language based on this principle, but also has its own dichotomy;



two-part method:

The method of finding a particular value [the maximum minimum value of the condition] in a particular sequence [ usually an ordered sequence];

In contrast to the usual way of finding, the dichotomy method uses the method of binary lookup, which is more convenient than the usual way to travel all the data;

However, it is important to note that the binary method can only deal with the data of the order, unsorted can not directly use the dichotomy!!


Implementation code:

int Q (int left, int right, int path) {      int mid;      while (left <= right) {        mid = (left + right)/2;          if (Mid < path) left = mid + 1;          else right  = mid-1;     } return right;  }  
you need to define at least four variables: left, right, mid; path;

Left: The right boundary of the interval;

Right: The boundary of the interval;

Mid: Midpoint of the interval;

Path: The extremum that can be obtained;

Code Analysis:

when Left<right, execute the procedure;

Through the change of mid, keep approaching path until right<left;

left=mid+1; Right=mid-1, in order to ensure that the program in the Left=right=mid, will not cause a cycle of death;


Example: HDOJ2199:


Can you solve this equation? Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 13146 Accepted Submission (s): 5886


Problem Descriptionnow,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 = = Y,can you find its solution between 0 and 100 ;
Now try your lucky.
Inputthe first line of the input contains an integer T (1<=t<=100) which means the number of test cases. Then T-lines follow, each line has a real number Y (Fabs (Y) <= 1e10);
Outputfor Each test case, you should just output one real number (accurate up to 4 decimal places), which is the solution of The Equation,or "No solution!", if there is No solution for the equation between 0 and 100.
Sample Input
2100-4

Sample Output
1.6152No solution!
purely mathematical problems, directly referencing the previous piece of code can:

AC Code:

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h>double fab (double X ) {    return x<=0?-x:x;} Double fun (double x) {    return 8*x*x*x*x+7*x*x*x+2*x*x+3*x+6.0;} int main () {    int t;    scanf ("%d", &t);    while (t--)    {        double y,mid,left,right;;        scanf ("%lf", &y);        if (y<fun (0) | | y>807020306) printf ("No solution!\n");        else        {            left=0.0;right=100.0;            while (Fab (right-left) >=1e-10)            {                mid=0;                Mid= (Left+right)/2;                if (Fab (mid-y) <=1e-10) break;                else if (Fun (mid)-y>0) Right=mid;                else Left=mid;            }            printf ("%.4lf\n", mid);        }    }    return 0;}



There is another similar question:


Pie Time limit:5000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 6644 Accepted Submission (s): 2517


Problem Descriptionmy birthday is coming up and traditionally I ' m serving pie. Not just one pie, no, I had a number N of them, of various tastes and of various sizes. F of my friends is coming to my party and each of the them gets a piece of pie. This should is one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though.

My Friends is very annoying and if one of them gets a bigger piece than the others, they start complaining. Therefore all of them should get equally sized (and not necessarily equally shaped) pieces, even if this leads to some pie Getting spoiled (which is better than spoiling the party). Of course, I want a piece of pie for myself too, and that piece should also is of the same size.

What's the largest possible piece size all of us can get? All the Pies is cylindrical in shape and they all has the same height 1, but the radii of the Pies can is different.

Inputone line with a positive integer:the number of test cases. Then to each test case:
---One line with a integers n and F with 1 <= N, F <= 000:the number of pies and the number of friends.
---one line with N integers ri with 1 <= ri <= 000:the radii of the Pies.

Outputfor each test case, output one line with the largest possible volume V such this me and my friends can all get a pie Piece of Size v. The answer should is given as a floating point number with an absolute error of in most 10^ (-3).
Sample Input
33 34 3 31 24510 51 4 2 3 4 5 6 5 4 2

Sample Output
25.13273.141650.2655

Topic Analysis:

The same as the previous question, but to determine the number of cakes to be allocated; (the key to the general dichotomy is the judgment of thetermination condition)

There is a point, this problem is more than the pit, and then the allocation of cakes, but also to consider themselves;

So enter the number of people to people++;


AC Code:

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #define PI ACOs (- 1.0) Double cake[10010];d ouble maxfun (int a,int b) {return a>b?a:b;} int main () {int c;scanf ("%d", &c), while (c--) {int num,people;double r,max,sum,mid,left,right;sum=0.0;max=0.0; scanf ("%d%d", &num,&people);p eople++;for (int i=1;i<=num;i++) {scanf ("%lf", &r); cake[i]=pi*r*r;sum+= Cake[i];max=maxfun (Max,cake[i]);} Left=max/people;right=sum/people;while (right-left>=0.000001) {int peoplenum=0;mid= (right+left)/2;for (int p=1;p <=num;p++) peoplenum+= (int) (CAKE[P]/MID); if (peoplenum<people) Right=mid;else Left=mid;} printf ("%.4lf\n", left);} return 0;}


Some functions for binary lookup are also available in the STL for C + +, as described here:

The STL contains four different binary search algorithms:

Binary_search

Lower_bound

Upper_bound

Equal_range

can only be processed if the data is sorted;

     Binary_search: Finds the element path in the sorted [Left,right]. If [left, right] has an element equal to path, it returns TRUE, otherwise it returns false, and it does not return a lookup location.

     Lower_bound: It's in a sorted[Left,right]Search for the element path. If[Left,right]With an element equal to path, Lower_bound returns a iterator that points to the first element. If no such element exists, it returns where it would appear if such an element were present. That points to the first element that is not less than path. If path is greater than[Left,right], the last is returned.

     Upper_bound: It's in a sorted[Left,right], and return to the last suitable location where the path can be planted. If path exists, Lower_bound returns the iterator that points to the element. In contrast, Upper_bound does not do this, it returns the last suitable location where path can be placed.If path exists, the iterator it returns will point to the next location of the path, not the path itself.

     Equal_range: The return value essentially combines the return value of both Lower_bound and Upper_bound. The return value is a pair of iterator I and J, where I is the first location where path can be placed, and J is the last position where path can be placed. Can be performed: each element in [I,j] is equivalent to path, and [I, J] is[Left,right]One of the largest sub-ranges that conform to the above properties. The algorithm Lower_bound returns the first iterator of the range, the algorithm Upper_bound returns the Past-the-end iterator of the range, and the algorithm Equal_range returns both in the form of pair.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Num 23:hdoj:2199 Can solve this equation? + hdoj:1969 Pie [dichotomy]

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.