[bzoj1336] [Balkan2002]Alien最小圆覆盖

  最小圆覆盖。。三个for是O(n)的QAQ。。因为随机化后新的点不在当前圆内的几率不大。。

  学习了下求中垂线的姿势...

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<iostream>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #define d double
 7 using namespace std;
 8 const int maxn=1e5+233;
 9 const d eps=1e-8;
10 struct poi{d x,y;}a[maxn];
11 struct cir{poi O;d r;}now;
12 poi operator +(poi a,poi b){return (poi){a.x+b.x,a.y+b.y};}
13 poi operator -(poi a,poi b){return (poi){a.x-b.x,a.y-b.y};}
14 poi operator /(poi a,d b){return (poi){a.x/b,a.y/b};}
15 int i,j,k,n,m;
16
17 inline d dis(poi a,poi b){
18     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
19 }
20 inline poi rev(poi a){return (poi){a.y,-a.x};}
21 inline poi center(poi a,poi b,poi c){
22     if(fabs((b.y-a.y)*(c.x-a.x)-(c.y-a.y)*(b.x-a.x))<eps){
23         if(a.x>b.x)swap(a,b);
24         if(a.x>c.x)swap(a,c);
25         if(b.x>c.x)swap(b,c);
26         return (a+c)/2.0;
27     }
28     poi x1=(a+b)/2.0,x2=x1+rev(a-b),y1=(a+c)/2.0,y2=y1+rev(a-c);
29     if(fabs(y1.x-y2.x)<eps)swap(x1,y1),swap(x2,y2);
30     d k2=(y1.y-y2.y)/(y1.x-y2.x),b2=y2.y-y2.x*k2;
31     if(fabs(x1.x-x2.x)<eps)return (poi){x1.x,k2*x1.x+b2};
32     d k1=(x1.y-x2.y)/(x1.x-x2.x),b1=x2.y-x2.x*k1;
33     d x=(b2-b1)/(k1-k2);
34     return (poi){x,k1*x+b1};
35 }
36 inline bool oncir(poi a){
37     return dis(a,now.O)<now.r+eps;
38 }
39
40 int main(){
41     scanf("%d",&n);srand(n+233);
42     for(i=1;i<=n;i++)scanf("%lf%lf",&a[i].x,&a[i].y);
43     random_shuffle(a+1,a+n+1);
44     now.O=(a[1]+a[2])/2.0,now.r=dis(now.O,a[1]);
45     for(i=3;i<=n;i++)if(!oncir(a[i])){
46         now.O=(a[1]+a[i])/2.0,now.r=dis(a[i],a[1])/2.0;
47         for(j=2;j<i;j++)if(!oncir(a[j])){
48             now.O=(a[i]+a[j])/2.0,now.r=dis(a[i],a[j])/2.0;
49             for(k=1;k<j;k++)if(!oncir(a[k])){
50                 now.O=center(a[i],a[j],a[k]);
51                 now.r=dis(now.O,a[k]);
52             }
53         }
54     }
55     printf("%.6lf\n",now.r);
56     printf("%.6lf %.6lf\n",now.O.x,now.O.y);
57     return 0;
58 }

时间: 2024-08-01 17:43:50

[bzoj1336] [Balkan2002]Alien最小圆覆盖的相关文章

【BZOJ1336】[Balkan2002]Alien最小圆覆盖 随机增量法

[BZOJ1336][Balkan2002]Alien最小圆覆盖 Description 给出N个点,让你画一个最小的包含所有点的圆. Input 先给出点的个数N,2<=N<=100000,再给出坐标Xi,Yi.(-10000.0<=xi,yi<=10000.0) Output 输出圆的半径,及圆心的坐标 Sample Input 6 8.0 9.0 4.0 7.5 1.0 2.0 5.1 8.7 9.0 2.0 4.5 1.0 Sample Output 5.00 5.00 5

【bzoj1336/1337/2823】[Balkan2002]Alien最小圆覆盖 随机增量法

题目描述 给出N个点,让你画一个最小的包含所有点的圆. 输入 先给出点的个数N,2<=N<=100000,再给出坐标Xi,Yi.(-10000.0<=xi,yi<=10000.0) 输出 输出圆的半径,及圆心的坐标 样例输入 6 8.0 9.0 4.0 7.5 1.0 2.0 5.1 8.7 9.0 2.0 4.5 1.0 样例输出 5.00 5.00 5.00 题解 随机增量法求最小圆覆盖裸题 求法:设初始圆为某空圆,先枚举第一个点,如果不在当前圆内,则令当前圆为这一个点的最小圆

BZOJ 1336 Balkan2002 Alien最小圆覆盖

题目大意:最小圆覆盖. 思路:再拍一份模板.做法见:http://blog.csdn.net/jiangyuze831/article/details/43950601 CODE: #define _CRT_SECURE_NO_WARNINGS #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include

BZOJ 1336 Balkan2002 Alien最小圆覆盖 随机增量法

题目大意:求最小圆覆盖 随机增量法裸题 注意多输出几位小数不然过不去= = #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm> #define M 100100 #define EPS 1e-7 using namespace std; struct Point

bzoj2823: [AHOI2012]信号塔&amp;&amp;1336: [Balkan2002]Alien最小圆覆盖&amp;&amp;1337: 最小圆覆盖

首先我写了个凸包就溜了 这是最小圆覆盖问题,今晚学了一下 先随机化点,一个个加入 假设当前圆心为o,半径为r,加入的点为i 若i不在圆里面,令圆心为i,半径为0 再重新从1~i-1不停找j不在圆里面,令圆心为ij中点,直径为ij距离 再重新在1~j-1不停找k不在圆里面,三点可确定一圆,初中数学 复杂度看似O(n^3)实则O(n),好玄学 坑点:注意如果用点斜式表示方程有斜率为不存在的情况,需要特判 #include<cstdio> #include<iostream> #incl

Bzoj 1336&amp;1337 Alien最小圆覆盖

1336: [Balkan2002]Alien最小圆覆盖 Time Limit: 1 Sec  Memory Limit: 162 MBSec  Special Judge Submit: 1473  Solved: 648 [Submit][Status][Discuss] Description Input 先给出点的个数N,2<=N<=100000,再给出坐标Xi,Yi.(-10000.0<=xi,yi<=10000.0) Output Sample Input 6 8.0

【BZOJ-1336&amp;1337】Alie最小圆覆盖 最小圆覆盖(随机增量法)

1336: [Balkan2002]Alien最小圆覆盖 Time Limit: 1 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 1573  Solved: 697[Submit][Status][Discuss] Description 给出N个点,让你画一个最小的包含所有点的圆. Input 先给出点的个数N,2<=N<=100000,再给出坐标Xi,Yi.(-10000.0<=xi,yi<=10000.0) Outpu

BZOJ1337: 最小圆覆盖

题目:求n个点的最小圆覆盖. 题解:最小圆覆盖,上模板.复杂度证明可以戳:这里 代码: 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #inclu

最小圆覆盖模板

//最小圆覆盖 //输入: 从下标0开始的点集_ps和大小_n //输出: 覆盖所有点的最小圆 //复杂度: O(n) //注意: 会对_ps进行随机处理操作,将会改变点集的内部顺序 Circle MinCoverCir(Point _ps[],int _n) { //随机处理,但是会改变传入的点集. random_shuffle(_ps, _ps+_n);//复杂度O(_n) Circle rec; rec.r = 0; rec.c = _ps[0]; for(int i=1;i<_n;i++