Niu Ke Winter Holiday Algorithm Basic Training Camp 5 The training location of Niu Niu Team (three points)

Topic: https://ac.nowcoder.com/acm/contest/3006/B
Reference problem solution: https://ac.nowcoder.com/acm/problem/blogs/201956 Three-section
method reference blog: https://blog .csdn.net/beiyouyu/article/details/7875480?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

The method for this question is to
divide the answer into three points. Next, let's talk about the rule of thirds in detail. We all know that two points can find a certain value in a monotonic function.
The rule of thirds is mainly used to solve the problem of finding the extreme value of the unimodal function , and for this topic, we analyze the range and we can roughly conclude that as x increases, the maximum distance first increases and then decreases.
So the specific method of three-pointing is that we have to divide into two halves, and then divide one of the changes into two halves. The overall form is 2:1:1. Here, we use m to mean half and mm to mean half of half. To request the minimum value, you only need to move the boundary to the larger side each time.
Then there is another question, how many times should you loop to find the answer? How to express it?

There are two ways: 1. Use the for loop directly for a certain number of times.
It is used 100 times here. Because every time the cycle is repeated, the remaining interval is divided into three points again, which is a decrease of the negative power of 2. Therefore, the value of 2 to the 100th power multiplied by the interval length should be accurate enough.
2.
While loop while (l + EPS <r)
where EPS is an infinitely small number, this is well understood, so I won’t say more.
code show as below:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f;
const int N=100005;
using namespace std;
typedef long long ll;
struct node{
    
    
    int x,y;
}a[N];
int n;
double maxn;
double check(double c){
    
    
    maxn=0.0;
    for(int i=1;i<=n;i++){
    
    
        double sum=a[i].y*a[i].y+(a[i].x-c)*(a[i].x-c);
        maxn=max(maxn,sum);
    }
    return maxn;
}

void slove(double l,double r){
    
    
    for(int i=1;i<=100;i++){
    
    
        double m=l+(r-l)/2;
        double mm=l+(m-l)/2;
        if(check(mm)>check(m))
            l=mm;
        else
            r=m;
    }
}
int main ()
{
    
    
    //freopen("D:\\input.txt", "r", stdin);
    //freopen("D:\\output.txt", "w", stdout);
    cin>>n;
    for(int i=1;i<=n;i++)
        scanf("%d %d",&a[i].x,&a[i].y);
    slove(-10000,10000);
    double ans=sqrt(maxn);
    cout<<ans<<endl;
	return 0;
}


Guess you like

Origin blog.csdn.net/u011612364/article/details/104758220