ural 1207 計算幾何

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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章