巨人与鬼问题

来源:互联网 发布:知微科技招聘信息 编辑:程序博客网 时间:2024/04/28 02:36
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <cstdlib>using namespace std;const int maxn = 110;int n, ans[maxn];struct Node {    int x, y, id;    double angle;    void creat_angle(const Node &node) {        angle = atan2(y - node.y, x - node.x);    }    bool operator < (const Node &node) const {        return angle < node.angle;    }    int type() const {return id < n ? 1 : -1;}}node[maxn*2];void solve(int left, int right) {    if (left >= right) return;    int i;    for (i = left + 1; i <= right; i++) {        if (node[i].y < node[left].y || (node[i].y == node[left].y && node[i].x < node[left].x) ) {            Node temp;            temp = node[i];            node[i] = node[left];            node[left] = temp;        }    }    for (i = left + 1; i <= right; i++) {        node[i].creat_angle(node[left]);    }    sort(node + left + 1, node + right + 1);    int cnt = node[left].type();    for (i = left + 1; i <= right; i++) {        cnt += node[i].type();        if (!cnt) {            ans[node[left].id] = node[i].id;            ans[node[i].id] = node[left].id;            solve(left + 1, i - 1);            solve(i + 1, right);            return;        }    }}int main(){    scanf("%d", &n);    int i, len = n << 1;    for (i = 0; i < len; i++) { scanf("%d %d", &node[i].x, &node[i].y); node[i].id = i; }    solve(0, len - 1);    for (int i = 0; i < n; i++) {        printf("%d\n", ans[i] - n + 1);    }    return 0;}

1 0