hdu 1007 Quoit Design 平面最近点对
2014-04-08 12:38
267 查看
难度:5
最近听说inline可以让非递归的函数更快,但是试了一下感觉不是很好。
平面最近点对,计算机和模板题
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 100010;
struct node {
double x , y;
};
int n , a[maxn];
node p[maxn];
double cmpx(node a, node b) {
return a.x < b.x;
}
double cmpy(int a , int b) {
return p[a].y < p[b].y;
}
double dis(node a , node b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double min3(double a,double b,double c) {
return (a < b)?(a < c ? a : c ) : (b < c ? b : c);
}
double find(int l, int r) {
if(r == l+1)
return dis(p[l] , p[r]);
if(r == l+2)
return min3(dis(p[l],p[r]) , dis(p[l],p[l+1]) , dis(p[l+1],p[r]));
int mid = (l + r) >> 1;
double ans = min(find(l,mid) , find(mid+1,r));
int cnt = 0;
for(int i=l;i<=r;i++)
if(p[i].x>=p[mid].x-ans && p[i].x<=p[mid].x+ans)
a[cnt++] = i;
sort(a , a+cnt,cmpy);
for(int i=0;i<cnt;i++)
for(int j=i+1;j<cnt;j++) {
if(p[a[j]].y-p[a[i]].y >= ans) break;
ans = min(ans , dis(p[a[i]] , p[a[j]]));
}
return ans;
}
int main() {
while(~scanf("%d" , &n) && n) {
for(int i=0;i<n;i++) scanf("%lf %lf" , &p[i].x , &p[i].y);
sort(p ,p +n , cmpx);
printf("%.2lf\n" , find(0,n-1)/2.0);
}
return 0;
}
最近听说inline可以让非递归的函数更快,但是试了一下感觉不是很好。
平面最近点对,计算机和模板题
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 100010;
struct node {
double x , y;
};
int n , a[maxn];
node p[maxn];
double cmpx(node a, node b) {
return a.x < b.x;
}
double cmpy(int a , int b) {
return p[a].y < p[b].y;
}
double dis(node a , node b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double min3(double a,double b,double c) {
return (a < b)?(a < c ? a : c ) : (b < c ? b : c);
}
double find(int l, int r) {
if(r == l+1)
return dis(p[l] , p[r]);
if(r == l+2)
return min3(dis(p[l],p[r]) , dis(p[l],p[l+1]) , dis(p[l+1],p[r]));
int mid = (l + r) >> 1;
double ans = min(find(l,mid) , find(mid+1,r));
int cnt = 0;
for(int i=l;i<=r;i++)
if(p[i].x>=p[mid].x-ans && p[i].x<=p[mid].x+ans)
a[cnt++] = i;
sort(a , a+cnt,cmpy);
for(int i=0;i<cnt;i++)
for(int j=i+1;j<cnt;j++) {
if(p[a[j]].y-p[a[i]].y >= ans) break;
ans = min(ans , dis(p[a[i]] , p[a[j]]));
}
return ans;
}
int main() {
while(~scanf("%d" , &n) && n) {
for(int i=0;i<n;i++) scanf("%lf %lf" , &p[i].x , &p[i].y);
sort(p ,p +n , cmpx);
printf("%.2lf\n" , find(0,n-1)/2.0);
}
return 0;
}
相关文章推荐
- apache .htaccess 禁止访问某目录方法
- MySQL与MongoDB设计实例对比
- Data Structure Array: Maximum of all subarrays of size k
- Codeforces Round #129 (Div. 2)
- CentOS配置本地yum仓库源
- windows编程从菜鸟到新手(一)
- Python逻辑行与物理行
- N皇后问题 【回溯法】
- opencv实现导向滤波(GuidedFilter)
- Oracle SQL Trace 和 10046 事件
- java UUID生成唯一标识符
- error: No resource identifier found for attribute ‘backIcon’ in package
- 小明求素数积
- JavaScript之常用方法讲解
- Set Matrix Zeroes
- 测试,使用windows live writer写blog
- TreeFTL:Efficient RAM Management for High Performance of NAND Flash-based Storage Systems-论文注释笔记
- android中 httpClient的3种超时时间
- Codeforces Round #128 (Div. 2)
- Android中解析XML