HDU1007


layout: post
title: "HDU 1007"
date: 2017-04-23 14:27:18 +0800
categories: ACM
tags: Partition
author: SteveDevin

mathjax: true

  • content
    {:toc}

思路:

分治裸题, 求平面最接近点对: 对于要求最接近点对的区间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);
    }
}

Minds:

感觉自己越来越水了, 之前刷了那么长时间的题, 才不过两个月左右没刷题就废了.
距离省赛还有两个周, 好慌啊……

转载于:https://www.cnblogs.com/QQ-1615160629/p/HDU1007.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值