标签:lan partition mat 思路 cst amp -- algo nts
layout: post
title: "HDU 1007"
date: 2017-04-23 14:27:18 +0800
categories: ACM
tags: Partition
author: SteveDevin
分治裸题, 求平面最接近点对: 对于要求最接近点对的区间S[l, r], 取 m = (l + r) >> 1, 则最短距离为以x或y排序后的区间 S[l, m], S[m + 1, r], 以及左右两个区间之间某两个点距离的最小值.
设 min(S[l, m], S[m + 1, r]) = ret; 则两个区间之间的最短距离只需考虑 x 坐标 在 [m - ret, m + ret]的点即可, 进而可以证明只需考虑[m - ret, m + ret]中的6个点即可.
我先是枚举[m - ret, m + ret] 所有的点求最短距离, TLE. 换成区间内最近的6个点后AC (感觉这样不严谨, 数据太水?)
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 100000 + 2;
struct Point{
double x;
double y;
}points[maxn];
int n;
bool comp(Point &a, Point &b)
{
if(a.x == b.x) return a.y < b.y;
return a.x < b.x;
}
double calc(int a, int b)
{
return sqrt(fabs(pow(points[a].x - points[b].x, 2) + pow(points[a].y - points[b].y, 2)));
}
double CPair(int l, int r)
{
if(r - l + 1 < 2) return 1 << 28;
int m = (l + r) >> 1;
double ret = 1 << 28;
ret = CPair(l, m);
ret = min(ret, CPair(m + 1, r));
int L = m, R = 0;
//while(L && points[L].x > points[m].x - ret) L--;
// while(R < r && points[R].x < points[m].x + ret) R++;
L = max(m - 3, l);
R = min(m + 3, r);
for(int j = L; j <= m; j++)
for(int k = m + 1; k <= r; k++)
ret = min(ret, calc(j, k));
return ret;
}
int main()
{
while(~scanf("%d", &n) && n)
{
for(int i = 0; i < n; i++)
scanf("%lf%lf", &points[i].x, &points[i].y);
sort(points, points + n, comp);
printf("%.2f\n", CPair(0, n - 1) / 2);
}
}
感觉自己越来越水了, 之前刷了那么长时间的题, 才不过两个月左右没刷题就废了.
距离省赛还有两个周, 好慌啊……
标签:lan partition mat 思路 cst amp -- algo nts
原文地址:https://www.cnblogs.com/QQ-1615160629/p/HDU1007.html