【HDOJ】3007 Buried memory
1. 题目描述
有n个点,求能覆盖这n个点的半径最小的圆的圆心及半径。
2. 基本思路
算法模板http://soft.cs.tsinghua.edu.cn/blog/?q=node/1066定义Di表示相对于P[1]和P[i]组成的最小覆盖圆,如果P[2..i-1]都在这个圆内,那么当前的圆心和半径即为最优解。
如果P[j]不在这个圆内,那么P[j]一定在新的最小覆盖圆的边界上即P[1]、P[j]、P[i]组成的圆。
因为三点可以确定一个圆,因此只需要不断的找到不满足的P[j],进而更新最优解即可。
其实就是三层循环,不断更新最优解。然而,这个算法的期望复杂度是O(n)。这个比较难以理解。
3. 代码
/* 3007 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct {
double x, y;
} Point; const double eps = 1e-;
const int maxn = ;
Point P[maxn];
Point o;
double r;
int n; double Length(Point a, Point b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} double Cross(Point a, Point b, Point c) {
return (c.x-a.x)*(b.y-a.y) - (c.y-a.y)*(b.x-a.x);
} Point Intersect(Point a, Point b, Point c, Point d) {
Point ret = a;
double t = ((a.x - c.x) * (c.y - d.y) - (a.y - c.y) * (c.x - d.x)) /
((a.x - b.x) * (c.y - d.y) - (a.y - b.y) * (c.x - d.x));
ret.x += (b.x - a.x) * t;
ret.y += (b.y - a.y) * t;
return ret;
} Point circumcenter(Point a, Point b, Point c) {
Point ua, ub, va, vb; ua.x = (a.x + b.x) / 2.0;
ua.y = (a.y + b.y) / 2.0;
ub.x = ua.x - a.y + b.y;
ub.y = ua.y + a.x - b.x; va.x = (a.x + c.x) / 2.0;
va.y = (a.y + c.y) / 2.0;
vb.x = va.x - a.y + c.y;
vb.y = va.y + a.x - c.x;
return Intersect(ua, ub, va, vb);
} void min_center() {
o = P[];
r = ; rep(i, , n) {
if (Length(P[i], o)-r > eps) {
o = P[i];
r = ;
rep(j, , i) {
if (Length(P[j], o)-r > eps) {
o.x = (P[i].x + P[j].x) / ;
o.y = (P[i].y + P[j].y) / ;
r = Length(o, P[j]); rep(k, , j) {
if (Length(P[k], o)-r > eps) {
o = circumcenter(P[i], P[j], P[k]);
r = Length(o, P[k]);
}
}
}
}
}
}
} void solve() {
min_center();
printf("%.2lf %.2lf %.2lf\n", o.x, o.y, r);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%d", &n)!=EOF && n) {
rep(i, , n)
scanf("%lf%lf", &P[i].x, &P[i].y);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【HDOJ】3007 Buried memory的更多相关文章
- 【Linux】Swap与Memory
背景介绍 Memory指机器物理内存,读写速度低于CPU一个量级,但是高于磁盘不止一个量级.所以,程序和数据如果在内存的话,会有非常快的读写速度.但是,内存的造价是要高于磁盘的,且内存的断电丢失数据也 ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- hdu 3007 Buried memory 最远点对
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3007 Each person had do something foolish along with ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
随机推荐
- JavaScript 防止事件冒泡
在我们书写一个弹窗的时候,我们往往需要点击弹窗的其他地方来隐藏弹窗. 通常我们会写成: $(document).bind('click',function(){ $('.pop-box').hide( ...
- Linux编辑器vi使用方法详细介绍
vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的任何版本,vi编辑器是完全相 ...
- PHP webserver 之 soap wsdl
强势插入:http://pan.baidu.com/s/1jG62oKm
- SQL重复记录查询(转载)
1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people ) 例二: select * from testtable where ...
- HTML 菜单 a 标签跳转本页面并追加参数
代码如下: <!-- BEGIN 追加URL的参数 --> <script type="text/javascript"> function setUrl( ...
- 从JAVA多线程理解到集群分布式和网络设计的浅析
对于JAVA多线程的应用非常广泛,现在的系统没有多线程几乎什么也做不了,很多时候我们在何种场合如何应用多线程成为一种首先需要选择的问题,另外关于java多线程的知识也是非常的多,本文中先介绍和说明一些 ...
- linux源码分析2
linux源码分析 这里使用的linux版本是4.8,x86体系. 这篇是 http://home.ustc.edu.cn/~boj/courses/linux_kernel/1_boot.html ...
- C#学习笔记---基础入门(一)
C#中的变量: 一个变量就是存储区(内存)中的一个存储单元. 变量声明赋值:int money =1000;/int money;money=1000; 输出:console.writeLine(mo ...
- java第一课:环境、变量、数据类型
一.java编程注意事项1.java区分大小写2.每条语句结尾有分号3.上下级代码注意缩进4.大括号要成对出现5.标点符号要用英文半角(半角全角区别)二.eclipse1.eclipse是自编译及时编 ...
- 1195: [HNOI2006]最短母串 - BZOJ
Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串.Input 第一行是一个正整数n(n<=12), ...