HDU1007 Quoit Design 【分治】
Quoit Design
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30505 Accepted Submission(s): 8017
Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a
configuration of the field, you are supposed to find the radius of such a ring.
Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered
to be 0.
of a toy. The input is terminated by N = 0.
2
0 0
1 1
2
1 1
1 1
3
-1.5 0
0 0
0 1.5
0
0.71
0.00
0.75
题意:给定n个点,求距离最短的两点的距离的一半。
题解:開始用暴力法。结果超时。然后换成分治就过了,分治的过程是先将每一个点的坐标读入到数组里,再将数组依照x坐标排序,然后分治找最小值。递归终止条件是仅仅剩两个元素或三个元素,可是若仅依照x排序终于结果不一定是最小值,由于有可能左边的元素与右边的元素构成最小值,所以须要再依据y值进行一次排序,此时数据规模已经相当小了。能够用暴力直接求解。
分治代码:
#include <stdio.h>
#include <math.h>
#include <algorithm>
#define maxn 100002
using std::sort; struct Node{
double x, y;
} arr[maxn], temp[maxn]; bool cmpx(Node a, Node b)
{
return a.x < b.x;
} bool cmpy(Node a, Node b)
{
return a.y < b.y;
} double calDist(int i, int j)
{
double x = arr[i].x - arr[j].x;
double y = arr[i].y - arr[j].y;
return sqrt(x * x + y * y);
} double divideAndConquer(int l, int r)
{
if(r - l == 1) return calDist(l, r);
else if(r - l == 2){
double a = calDist(l, l + 1);
double b = calDist(l + 1, r);
double c = calDist(l, r);
if(b > c) b = c;
return a < b ? a : b;
}
int mid = (l + r) >> 1, i, j, id = 0;
double a = divideAndConquer(l, mid);
double b = divideAndConquer(mid + 1, r);
double min = a < b ? a : b;
for(i = l; i <= r; ++i)
if(fabs(arr[i].x - arr[mid].x) < min) temp[id++] = arr[i];
sort(temp, temp + id, cmpy);
for(i = 0; i < id; ++i)
for(j = i + 1; j < id; ++j){
a = temp[j].y - temp[i].y;
if(a >= min) break;
b = temp[j].x - temp[i].x;
a = sqrt(a * a + b * b);
if(a < min) min = a;
}
return min;
} int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n, i, j;
double ans, x, y, len;
while(scanf("%d", &n), n){
for(i = 0; i < n; ++i)
scanf("%lf%lf", &arr[i].x, &arr[i].y);
sort(arr, arr + n, cmpx);
printf("%.2lf\n", divideAndConquer(0, n - 1) / 2);
}
return 0;
}
原TLE代码:
#include <stdio.h>
#include <math.h>
#define maxn 100002 struct Node{
double x, y;
} arr[maxn]; double cal(int i, int j)
{
double x = arr[i].x - arr[j].x;
double y = arr[i].y - arr[j].y;
return sqrt(x * x + y * y);
} int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n, i, j;
double ans, x, y, len;
while(scanf("%d", &n), n){
for(i = 0, ans = -1; i < n; ++i){
scanf("%lf%lf", &arr[i].x, &arr[i].y);
for(j = 0; j < i; ++j){
len = cal(i, j);
if(len < ans || ans < 0) ans = len;
}
}
printf("%.2lf\n", ans / 2);
}
return 0;
}
HDU1007 Quoit Design 【分治】的更多相关文章
- HDU1007 Quoit Design掷环游戏
Quoit Design 看懂题意以后发现就是找平面最近点对间距离除以2. 平面上最近点对是经典的分治,我的解析 直接上代码 #include<bits/stdc++.h> using n ...
- hdu 1007 Quoit Design 分治求最近点对
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- (hdu1007)Quoit Design,求最近点对
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
- HDU-1007 Quoit Design 平面最近点对
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 简单裸题,测测模板,G++速度快了不少,应该是编译的时候对比C++优化了不少.. //STATU ...
- HDU1007.Quoit Design
-- 点我 -- 题目大意 :给你一堆点,求一个最小圆能够覆盖两个点的半径(最近两点距离的一半): 最多100000个点,暴力即O(n^2)会超时,考虑二分,先求左边最短距离dl,右边dr, 和一个点 ...
- Quoit Design(最近点对+分治)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...
- HDU 1007 Quoit Design【计算几何/分治/最近点对】
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Quoit Design(hdu1007)
---恢复内容开始--- Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- ACM-计算几何之Quoit Design——hdu1007 zoj2107
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
随机推荐
- Swift - 20 - 字典的基础操作
//: Playground - noun: a place where people can play import UIKit var dict = [1:"one", 2:& ...
- 完数c实现
完数,顾名思义,就是一个数如果恰好等于它的因子之和.例如6=1+2+3.编写找出1000以内的所有完数 #include <stdio.h> #include <stdlib.h&g ...
- MapReduce概念(转)
昨天,我在Xebia印度办公室发表了一个关于MapReduce的演说.演说进行得很顺利,听众们都能够理解MapReduce的概念(根据他们的反馈).我成功地向技术听众们(主要是Java程序员,一些Fl ...
- 初涉JavaScript模式系列 阶段总结及规划
总结 不知不觉写初涉JavaScript模式系列已经半个月了,没想到把一个个小点进行放大,竟然可以发现这么多东西. 期间生怕对JS的理解不到位而误导各位,读了很多书(个人感觉JS是最难的oo语言),也 ...
- bzoj4306: 玩具厂
Description 在JIH考察的地图中有N个城市,被公路依次连成了一个环,JIH想在这些城市中建一个玩具厂.城市和公路都被编号为1..N,i号公路连接i-1号城市与i号城市(1号公路连接N号城市 ...
- net core 依赖注入问题
net core 依赖注入问题 最近.net core可以跨平台了,这是一个伟大的事情,为了可以赶上两年以后的跨平台部署大潮,我也加入到了学习之列.今天研究的是依赖注入,但是我发现一个问题,困扰我很久 ...
- laravel框架——上传、下载文件
文件上传 在config文件夹下新建一个 项目名.php return [ 'title' => 'My Test', 'posts_per_page' => 5, 'uploads' = ...
- iOS开发之WKWebView简单使用
iOS开发之WKWebView简单使用 iOS开发之 WKWebVeiw使用 想用UIWebVeiw做的,但是突然想起来在iOS8中出了一个新的WKWebView,算是UIWebVeiw的升级版. ...
- UGUI穿透3D世界判断&&UGUI全事件监听
public bool isPointUI(){ PointerEventData eventDataCurrnt = new PointerEventData (EventSystem.curren ...
- 尽历磨难,搞定OPEN VSWITCH安装
参考贴: http://sudomakeinstall.com/linux-systems/installing-openvswitch-on-centos-6-6-5 yum install ker ...