POJ 3714 Raid(平面近期点对)
解题思路:
分治法求平面近期点对。点分成两部分,加个标记就好了。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <string.h>
#define LL long long
using namespace std;
const int MAXN = 200000 + 10;
const double INF = 1e100;
struct Point
{
double x, y;
int flag;
}P[MAXN];
int N;
Point vec[MAXN];
bool cmp_x(Point a, Point b)
{
return a.x < b.x;
}
bool cmp_y(Point a, Point b)
{
return a.y < b.y;
}
double dis(Point a, Point b)
{
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}
double solve(Point *a, int l, int r)
{
if(l == r) return INF;
if(l + 1 == r)
{
if(P[l].flag == P[r].flag)
return INF;
return dis(P[l], P[r]);
}
int m = (l + r) >> 1;
double d = solve(a, l, m);
d = min(d, solve(a, m + 1, r));
int sz = 0;
for(int i=l;i<=r;i++)
{
if(fabs(P[i].x - P[m].x) <= d)
vec[sz++] = P[i];
}
sort(vec, vec + sz, cmp_y);
for(int i=0;i<sz;i++)
{
for(int j=i+1;j<sz;j++)
{
if(fabs(vec[i].y - vec[j].y) >= d)
break;
if(vec[i].flag != vec[j].flag)
{
double rs = dis(vec[i], vec[j]);
if(rs < d) d = rs;
}
}
}
return d;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &N);
for(int i=0;i<N;i++)
{
scanf("%lf%lf", &P[i].x, &P[i].y);
P[i].flag = 0;
}
for(int i=0;i<N;i++)
{
scanf("%lf%lf", &P[i + N].x, &P[i + N].y);
P[i + N]. flag = 1;
}
N <<= 1;
sort(P, P + N, cmp_x);
double ans = solve(P, 0, N - 1);
printf("%.3f\n", ans);
}
return 0;
}
POJ 3714 Raid(平面近期点对)的更多相关文章
- 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design
题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...
- poj 3714 Raid(平面最近点对)
Raid Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7473 Accepted: 2221 Description ...
- POJ 3714 Raid 近期对点题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- POJ 3714 Raid
Description After successive failures in the battles against the Union, the Empire retreated to its ...
- poj 3714 Raid【(暴力+剪枝) || (分治法+剪枝)】
题目: http://poj.org/problem?id=3714 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#prob ...
- POJ 3714 Raid(计算几何の最近点对)
Description After successive failures in the battles against the Union, the Empire retreated to its ...
- (洛谷 P1429 平面最近点对(加强版) || 洛谷 P1257 || Quoit Design HDU - 1007 ) && Raid POJ - 3714
这个讲的好: https://phoenixzhao.github.io/%E6%B1%82%E6%9C%80%E8%BF%91%E5%AF%B9%E7%9A%84%E4%B8%89%E7%A7%8D ...
- 【POJ 3714】 Raid
[题目链接] http://poj.org/problem?id=3714 [算法] 分治求平面最近点对 [代码] #include <algorithm> #include <bi ...
- 【POJ 3714】Raid
[题目链接]:http://poj.org/problem?id=3714 [题意] 给你两类的点; 各n个; 然后让你求出2*n个点中的最近点对的距离; 这里的距离定义为不同类型的点之间的距离; [ ...
随机推荐
- 98.TCP通信传输文件
客户端 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include <stdlib.h> #include <s ...
- FFmpegh.264解码
- (int)DecodeH264Frames: (unsigned char*)inputBuffer withLength:(int)aLength { ; ; av_init_packet(&a ...
- hibernate 注解配置<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/X
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- CC2530定时器使用
定时器学习 文件夹 说明 依据数据手冊可知CC2530总共同拥有4个定时器,可是定时器2被系统占用,可用的仅仅有三个,分别为定时器1/3/4 Timer在协议栈的代码位置为hal_timer ...
- Valgrind的用法
Valgrind是执行在Linux上一套基于仿真技术的程序调试和分析工具,它包括一个内核──一个软件合成的CPU,和一系列的小工具,每一个工具都能够完毕一项任务──调试.分析,或測试等. Valgri ...
- Javascript和jquery事件--鼠标事件的小结
1.鼠标事件的主要事件应该是mouseup, mousedown, mousewheel, mousemove, mouseover, moveout. <1>其中mouseup和mous ...
- css函数——calc()和attr()
css也有函数?好吧,我孤陋寡闻了.这里记录一下学习情况. calc()函数 定义:用于动态计算长度值 支持版本:css3 运算符前后都需要保留一个空格,例如:width: calc(100% - 1 ...
- 苹果手机wifi代理设置方法--用于抓包
杯具了!@@@@@@@变态的公司不能直接上网了,但是经过我的研究.可以用代理上网,电脑是可以了,但是的iphone肿么办,哇咔咔,不要捉急,我来告诉你怎么让你的iphone通过代理上网.动起来吧. 请 ...
- 【Codeforces Round #443 (Div. 2) B】Table Tennis
[链接] 我是链接,点我呀:) [题意] n个人站在一排. 每次第一个人和第二个人打架. 输的人跑到队列的尾巴去. 然后赢的人继续在队首.和第三个人打. 谁会先赢K次. [题解] 会发现,一轮之后就一 ...
- python使用matplotlib画图
python使用matplotlib画图 matplotlib库是python最著名的画图库.它提供了一整套和matlab类似的命令API.十分适合交互式地进行制图. 先介绍了怎样使用matplotl ...