Binary algorithm and triples Algorithm

Source: Internet
Author: User

Binary algorithm:

Hdu1969 pie

Question: F + 1 person points to N points to pie. pie can only be cut and cannot be from the group. That is to say, each person must have a whole block, not multiple fragments. The size of each person must be the same. The pie size of N parts is different. The goal is to find the maximum value that nobody can eat.

Abstract analysis: first, the condition is that N people must be enough to eat, and the maximum value is V when the size is the same. The relationship is as V increases, the fewer people can support. There is a non-incremental relationship here. Therefore, we only need to consider the binary method. The requirement on accuracy is the key to the use of the binary method. It is required to keep four decimal places, so we can multiply the number by 1000000 for calculation.

#include<iostream>#include<cstdio>#include<cmath>#define PI acos(long double(-1))using namespace std;long long a[10000+1];long long low, high, mid, res;int N, F;bool judge(){    int num = 0;    for(int i =0; i<N; i++)    {        num += a[i]/mid;    }    return num>=F+1;}int main(){    int T;    long long t;    int i;    cin>>T;    while(T-->0)    {        cin>>N>>F;        long long max = 0;        for(i=0; i<N; i++)        {            cin>>t;            a[i] = t*t*PI*1000000;            if(a[i]>max)                max = a[i];        }        low = 0;        high = max;        while(low<=high)        {            mid = (high + low)/2;            if(judge())            {                low = mid+1;                res = mid;            }            else                high = mid-1;        }        printf("%.4lf\n",(double)res/1000000);    }    return 0;}

Summary: The binary method can still be used in this way. It seems that for monotonous relationships, you can consider the binary method to solve specific value problems that satisfy certain relationships.

There are also discussions about this issue:

This is a graph drawn from the first group of test data. The red points in the graph are the required values. But what should I do if I need to change the meaning of the question to the minimum value corresponding to when P = 4.

In fact, this problem becomes the problem of finding the first and last target values in binary search.

For example, if the numbers 6, 8, 8, 9, and 10 exist in a [], write the algorithm code for the first 8 and the last 8 respectively;

Calculate the first one:

 1 l = 0, r = 6; 2 while(l<r) 3 { 4     mid = (l + r)/2; 5     if(a[mid]>=8) 6         r = mid - 1; 7     else 8         l = mid + 1; 9 }10 ans = l;

Find the last one:

l = 0, r = 6;while(l<r){    mid = (l + r)/2;    if(a[mid]<=8)        l = mid + 1;    else        r = mid - 1;}ans = r;

The latter is used in this question.

It can be seen that as long as the actual problem is abstracted into a mathematical problem, it is much simpler to analyze.

 

Three-Point Algorithm:

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.