【分治 求最近點對】hdu 1007 Quoit Design
Link:http://acm.split.hdu.edu.cn/showproblem.php?pid=1007
#include <bits/stdc++.h>
using namespace std;
/*
hdu 1007
題意:給出物品在平面上的點座標,求一個環不能一次套到兩的最大半徑,即最近點對距離的一半。
題解:先以x排序,用分治將問題分成左邊部分的最近點對,和左邊的最近點對,左邊右邊各一個點的
最近點對(在算這個的時候應按y排序,將兩點y差大於當前最小值的優化掉)。
*/
const int N = 1e5+5;
const double INF = 1e18;
struct Point{double x,y; } a[N];
bool cmp(Point a,Point b){return a.x < b.x; }
bool cmp2(Point a,Point b){return a.y < b.y; }
double dist(Point a,Point b){return sqrt((a.y-b.y)*(a.y-b.y)+(a.x-b.x)*(a.x-b.x))/2; }
Point temp[N];
double solve(int l, int r){
if(l == r)
return INF;
int mid = (l+r)>>1;
double mi;
mi = solve(l,mid);
mi = min(mi,solve(mid+1,r));
// printf("L=%f R=%f\n",solve(l,mid),solve(mid+1,r));
int top = 0;
temp[top++] = a[mid];
for(int i = mid-1; i >= l; i--){
if(a[mid].x-a[i].x>=mi)
break;
temp[top++] = a[i];
}
for(int i = mid+1; i <= r; i++){
if(a[i].x-a[mid].x>=mi)
break;
temp[top++] = a[i];
}
sort(temp,temp+top,cmp2);
for(int i = 0; i < top; i++){
for(int j = i+1; j < top; j++)
{
if(temp[j].y-temp[i].y>=mi)
break;
mi = min(mi,dist(temp[i],temp[j]));
}
}
return mi;
}
int main()
{
int n;
while(~scanf("%d",&n) && n)
{
for(int i = 0; i < n; i++)
scanf("%lf%lf",&a[i].x,&a[i].y);
sort(a,a+n,cmp);
printf("%.2f\n",solve(0,n-1));
}
return 0;
}
相關文章
- HDU1007Quoit Design(最小點對)
- HDU 1330 Nearest Common Ancestors(求兩個點的最近公共祖先)REST
- 樹分治 - 點分治
- 點分治
- HDU 2689 【歸併排序求逆序對】排序
- HDU 2689 Sort it【樹狀陣列求逆序對】陣列
- 點分治學習筆記筆記
- LeetCode169求眾數——分治LeetCode
- HDU 4460Friend Chains(對每個點BFS)AI
- 2014北京網路賽1007||hdu5038 模擬
- 「學習筆記」tarjan 求最近公共祖先筆記
- [題解] [洛谷P7883] 平面最近點對(加強版)
- 【KMP求字串匹配次數】 hdu 1686KMP字串匹配
- CC Prime Distance On Tree (樹的點分治 + FFT)FFT
- POJ-2299 Ultra-QuickSort-分治法排序求交換速度UI排序
- CDQ分治
- 分治法求眾數和重數(含檔案輸入輸出)
- 我對大家尋求oracle培訓的一點看法Oracle
- HDU 4667 Building Fence(求凸包的周長)UI
- HDU 4911 Inversion(歸併排序求逆序數)排序
- 最近關於工作的幾點思考
- Leetcode刷題之 【最近的請求次數】LeetCode
- 分治—快速排序排序
- HDU 2732 Leapin' Lizards(拆點+最大流)API
- 【KMP求字串第一個匹配位置】hdu 1711KMP字串
- HDU2689 Sort it (樹狀陣列求逆序數)陣列
- 分治法演算法學習(一)——歸併排序、求最大子陣列和演算法排序陣列
- 迭代最近點演算法 Iterative Closest Points演算法
- hdu5452 || 瀋陽網路賽1003題 最近公共祖先問題
- HDU-3461 Code Lock 並查集 + 二分求冪並查集
- hdu3415 單調佇列求區間最大和佇列
- HDU 1394 Minimum Inversion Number (樹狀陣列求逆序數)陣列
- HDU 2020 絕對值排序排序
- 1007(優先佇列)佇列
- 分治FFT小記?FFT
- 分治演算法演算法
- BZOJ4598: [Sdoi2016]模式字串(點分治 hash)模式字串
- HDU4722Good Numbers熱身賽2 1007題(思維題 不用數位dp照樣可以做的)Go