ural 1207 计算几何

本文探讨了在二维平面上选择两个点的问题,使得这两点所在的直线能够将点集分为两个等大小的部分。通过极角排序的方法,有效地解决了这一难题。介绍了关键算法步骤,包括如何找到最左下方的点作为起始点,以及如何通过逆时针排序剩余点来确定最终的两个点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1207. Median on the Plane

Time Limit: 1.0 second
Memory Limit: 16 MB
The are N points on the plane (N is even). No three points belong to the same strait line. Your task is to select two points in such a way, that strait line they belong to divides the set of points into two equal-sized parts.

Input

First line contains one integer N (2 ≤ N ≤ 10000). Each of next N lines contains pair of integersxiyi (−109 ≤ xiyi ≤ 109), the coordinates of i-th point.

Output

Print the numbers of selected points.

Sample

input output
4
0 0
1 0
0 1
1 1
1 4
对于这题,首先想到的是暴搜,无疑是超时的。想了很久没有思路,最终偷窥了别人的思路,利用极角排序,思路豁然开朗。
首先找到最左下方的点p0,将剩下的按逆时针排序(相对于最左下方的点),点p0和中间的那个点mid,即所求的点。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

struct Point {
    double x, y;
    int rank;
}p[10009];

double direction(Point p1, Point p2) {
    return ((p1.x-p[0].x)*(p2.y-p[0].y)-(p2.x-p[0].x)*(p1.y-p[0].y));
}

int cmp(Point p1, Point p2) {
    if (direction(p1, p2)>0)
        return 0;
    return 1;
}

int main()
{
    int i, n, k = 0;
    scanf("%d", &n);
    for (i = 0; i < n; ++i) {
        scanf("%lf %lf", &p[i].x, &p[i].y);
        p[i].rank = i+1;
        if (p[i].x < p[k].x || (p[i].x == p[k].x &&p[i].y < p[k].y))
            k = i;
    }
    swap(p[0], p[k]);
    sort(p+1, p+n, cmp);
    printf("%d %d\n", p[0].rank, p[n/2].rank);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值