【bzoj1336/1337/2823】最小圆覆盖
题目描述:
给出平面上N个点,请求出一个半径最小的圆覆盖住所有的点
输入:
第一行给出数字N,现在N行,每行两个实数x,y表示其坐标.
输出:
输出最小半径,输出保留三位小数.
样例输入:
4
1 0
0 1
0 -1
-1 0
样例输出:
1.000
题解:
随机增量法大法好呀!!!看起来是O(n^3)的算法,实际上期望是O(n)的。
具体操作如下:
(1)把点的顺序打乱,
(2)一个一个点往里面加,如果当前的点不在当前的圆内,
(3)在当前圆内的点找到另一个点,然后以这两个点做圆,
(4)再判断剩下的点,如果还有不在这个圆内的,以当前的这三个点做圆。
代码也巨好写无比,简直跟暴力一样,但是就是跑得过!!!
(唯一的难点就是推三点外接圆的式子,我这个傻逼推了一整个晚上)
代码:(以bzoj1336为例)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> #ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif #ifdef CT
#define debug(...) printf(__VA_ARGS__)
#define setfile()
#else
#define debug(...)
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
#endif #define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
char B[1 << 15], *S = B, *T = B;
inline int FastIn()
{
R char ch; R int cnt = 0; R bool minus = 0;
while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
ch == '-' ? minus = 1 : cnt = ch - '0';
while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
return minus ? -cnt : cnt;
}
inline double FastIn2()
{
R char ch = getc(); R double cnt = 0, ee = 1; R bool minus = 0, e = 0;
while (ch != '-' && (ch < '0' || ch > '9')) ch = getc();
if (ch == '-') minus = 1, ch = getc();
while (ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0' , ch = getc();
if (ch == '.') e = 1, ee *= 0.1, ch = getc();
while (ch >= '0' && ch <= '9' && e) cnt += (ch - '0') * ee, ee *= 0.1, ch = getc();
return minus ? -cnt : cnt;
}
#define maxn 1000010
struct Point
{
double x, y;
}p[maxn], o;
Point cir(Point a, Point b, Point c)
{
R Point res;
R double a1 = b.x - a.x, b1 = b.y - a.y, c1 = (a1 * a1 + b1 * b1);
R double a2 = c.x - a.x, b2 = c.y - a.y, c2 = (a2 * a2 + b2 * b2);
R double d = (a2 * b1 - a1 * b2) * 2;
res.x = a.x + (c2 * b1 - c1 * b2) / d;
res.y = a.y + (c1 * a2 - c2 * a1) / d;
return res;
}
#define dist(_a, _b) (sqrt((_a.x - _b.x) * (_a.x - _b.x) + (_a.y - _b.y) * (_a.y - _b.y) ) )
int main()
{
// setfile();
R int n = FastIn();
for (R int i = 1; i <= n; ++i)
p[i] = (Point) {FastIn2(), FastIn2()};
std::random_shuffle(p + 1, p + n + 1);
o = p[1];
R double r = 0;
for (R int i = 2; i <= n; ++i)
if (dist(p[i], o) > r)
{
o = p[i]; r = 0;
for (R int j = 1; j < i; ++j)
if (dist(p[j], o) > r)
{
o.x = (p[i].x + p[j].x) / 2;
o.y = (p[i].y + p[j].y) / 2;
r = dist(p[i], o);
for (R int k = 1; k < j; ++k)
if (dist(p[k], o) > r)
{
o = cir(p[i], p[j], p[k]);
r = dist(p[i], o);
}
}
}
printf("%.6f\n%.6f %.6f", r, o.x, o.y);
return 0;
}
【bzoj1336/1337/2823】最小圆覆盖的更多相关文章
- 2018.07.04 BZOJ1336&&1337: Balkan2002Alien最小圆覆盖
1336: [Balkan2002]Alien最小圆覆盖 1337: 最小圆覆盖 Time Limit: 1 Sec Memory Limit: 162 MBSec Special Judge Des ...
- BZOJ1336 Balkan2002 Alien最小圆覆盖 【随机增量法】*
BZOJ1336 Balkan2002 Alien最小圆覆盖 Description 给出N个点,让你画一个最小的包含所有点的圆. Input 先给出点的个数N,2<=N<=100000, ...
- 【bzoj1336/1337/2823】[Balkan2002]Alien最小圆覆盖 随机增量法
题目描述 给出N个点,让你画一个最小的包含所有点的圆. 输入 先给出点的个数N,2<=N<=100000,再给出坐标Xi,Yi.(-10000.0<=xi,yi<=10000. ...
- Bzoj 1336&1337 Alien最小圆覆盖
1336: [Balkan2002]Alien最小圆覆盖 Time Limit: 1 Sec Memory Limit: 162 MBSec Special Judge Submit: 1473 ...
- bzoj1336: [Balkan2002]Alien最小圆覆盖
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1336 1336: [Balkan2002]Alien最小圆覆盖 Time Limit: 1 ...
- 【BZOJ-1336&1337】Alie最小圆覆盖 最小圆覆盖(随机增量法)
1336: [Balkan2002]Alien最小圆覆盖 Time Limit: 1 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 1573 ...
- 2018.07.04 BZOJ 2823: AHOI2012信号塔(最小圆覆盖)
2823: [AHOI2012]信号塔 Time Limit: 10 Sec Memory Limit: 128 MB Description 在野外训练中,为了确保每位参加集训的成员安全,实时的掌握 ...
- 【BZOJ1336】[Balkan2002]Alien最小圆覆盖 随机增量法
[BZOJ1336][Balkan2002]Alien最小圆覆盖 Description 给出N个点,让你画一个最小的包含所有点的圆. Input 先给出点的个数N,2<=N<=10000 ...
- bzoj 2823: [AHOI2012]信号塔 最小圆覆盖
题目大意: 给定n个点,求面积最小的园覆盖所有点.其中\(n \leq 10^6\) 题解: 恩... 刚拿到这道题的时候... 什么???最小圆覆盖不是\(O(n^3)\)的随机增量算法吗????? ...
随机推荐
- 【MM系列】SAP MM模块-关于批次特性的查看和获取
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-关于批次特性的查看 ...
- ugui点击穿透判断
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Eve ...
- 【Qt开发】V4L2 API详解 Camera详细设置
Camera的可设置项极多,V4L2 支持了不少.但Sam之前对这些设置的用法和涵义都是在看videodev2.h中边看边理解,感觉非常生涩.直到写这篇blog时,才发现v4l2有专门的SPEC来说明 ...
- 简述Vue的实例属性、实例方法
1.实例属性 组件树访问 $parent -----> 用来访问当前组件实例的父实例: $root -----> 用来访问当前组件树的根实例,如果当前组件没有父实例,则$root表示当前组 ...
- Node.js实战1:创建一个新的Node项目。
你也许在猜专业的Node开发如何创建一个新项目. 有Npm在 ,这会非常简单. 虽然你可以创建一个JS文件,并执行:node file.js,但我建议你使用npm init来先创建一个node项目,这 ...
- C语言第十一周作业
这个作业属于哪个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/computer-scienceclass3-2018/ ...
- Springboot2.x集成Redis集群模式
Springboot2.x集成Redis集群模式 说明 Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移.如果想了解更多集群模式的 ...
- CVE 2019 0708 安装重启之后 可能造成 手动IP地址丢失.
1. 最近两天发现 更新了微软的CVE 2019-0708的补丁之后 之前设置的手动ip地址会变成 自动获取, 造成ip地址丢失.. 我昨天遇到两个, 今天同事又遇到一个.微软做补丁也不走心啊..
- spark Master启动流程
spark Master是spark集群的首脑,负责资源调度,任务分配,负载平衡等功能 以下是master启动流程概述 通过shell进行对master进行启动 首先看一下启动脚本more start ...
- Spark Netty 通信框架解析
1.RpcEndpoint: RPC端点 Spark针对每个节点(Client.Master.Worker)都称之为一个RpcEndpoint,且都实现RpcEndpoint接口,内部根据不同端点的需 ...