zoj 3386 Trick or Treat 三分 求最大值的 最小值

题目来源:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3963

题意:  给定 N 个不同的点, 求在x轴上的 一点,  使 这点到N个点的 距离 最大 的 最小值。

f(x) =  max(i){ (xi - x) ^2 + yi ^2 }

求 x  使  min(f(x)) , f(x)为凹函数   ,  采用三分的形式

代码如下:


const double EPS = 1e-10 ;
const int Max_N = 50005 ;
int n;
double add(double a, double b){
return (fabs(a + b) < EPS * (fabs(a) + fabs(b))) ? 0 : (a + b) ;
}
struct Point {
double x, y;
double dist(double a){
return sqrt(add((x - a)*(x -a) ,(y)*(y) )) ;
}
};
Point pt[Max_N] ;
double f(double x){
int i ;
double Max = 0 ;
for( i = 0 ; i < n; i++){
Max = pt[i].dist(x) > Max ? pt[i].dist(x) : Max ;
}
return Max ;
}
double tri_search(){
double Mid, Midmid , L, R ;
L = -400000.0 , R = 400000.0 ;
while(L + EPS < R){
Mid = (L + R) * 0.5 ;
Midmid = (Mid + R) *0.5 ;
if(f(Mid) <= f(Midmid ) )
R = Midmid ;
else L = Mid ;
}
return L ;
}
int main(){
while(scanf("%d", &n) && n){
for(int i =0 ; i < n ; i++)
scanf("%lf%lf" , &pt[i].x ,&pt[i].y ) ;
double xx = tri_search() ;
double Max = f(xx) ;
printf("%.9lf %.9lf\n" , xx + EPS , Max) ;
}
}

时间: 2024-08-04 18:19:54

zoj 3386 Trick or Treat 三分 求最大值的 最小值的相关文章

数组用法----求最大值、最小值和平均数

public class d { /** * @param args */ public static void main(String[] args) { // TODO 自动生成的方法存根 //数组 求最大值.最小值.平均分 int a[]={70,80,90,75,84,88}; int n; int min =100; int max = 1; for(n=0;n<6;n++) { if(max<a[n]) { max=a[n];//循环比较max和a[n]的大小 }else if(m

分组求最大值,最小值 使用开窗函数经验总结

select distinct TT.prod_id, tt.creteTime, tt.inspection_time, tt.cnt from (select s.prod_id, min(s.datetime_created) over(partition by s.prod_id) as creteTime, max(s.inspection_time) over(partition by s.prod_id) as inspection_time, sum(s.complete_cou

POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

使用Python scipy linprog 线性规划求最大值或最小值(使用Python学习数学建模笔记)

函数格式 scipy.optimize.linprog(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None, bounds=None, method='simplex', callback=None, options=None) 今天阅读数据建模第一章线性规划问题,问题描述如下: 通过介绍我们知道了线性规划,就是目标函数及约束条件均为线性函数. 通过画图我们可知,X1,X2的最优解为2,6,目标值为26. 我们如何时候这个scipy的公式来计算这个值呢:

java中数组求最大值,最小值,中和,平均

public class LxJavaCX { public static void main(String[] args) { // 定义一个整型数组,并赋初值 int[] nums = new int[] { 49,61, 23, 4, 74,160, 13, 148, 20 ,150}; int max = nums[0]; // 假定最大值为数组中的第一个元素 int min = nums[0]; // 假定最小值为数组中的第一个元素 double sum = 0;// 累加值 doub

C++数组求最大值及最小值最快方法(3[n/2]的时间效率)

#include <iostream> using namespace std; //最小值和最大值的求解,时间复杂度最多是3[n/2],依据算法导论第九章. void swap(int &a,int &b) { int temp = a; a = b; b = temp; } void Grial(int a[],int n) { int i = 0; if(a[i]>a[i+1]) { swap(a[i],a[i+1]); } for(int j = i+2;j<

根据状态变化情况,求最大值和最小值

题目1:根据第一二列,计算出第三列.即:求每组KH_VALUE状态(1和0)变化的最小时间 --创建测试表 create table tmp as select to_date('2017-04-21 16:22:00','yyyy-mm-dd hh24:mi:ss') dt, 1 kv from dual union all select to_date('2017-04-21 16:23:00','yyyy-mm-dd hh24:mi:ss') dt, 1 kv from dual unio

JS数组求最大值和最小值

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

js数组操作 求最大值,最小值,正序、倒叙大小值排序,去重复

var arr = [1,5,2,56,12,34,21,3,5] Math.min.apply({},arr) // 1 Math.max.apply({},arr) // 56 arr.sort((m,n)=>m-n) // [1, 2, 3, 5, 5, 12, 21, 34, 56] arr.sort((m,n)=>n-m) //[56, 34, 21, 12, 5, 5, 3, 2, 1] 去重复 var arr = [2,1,4,3,2,4,2,3,4,2,6,5,5] var o