弱校联萌十一大决战之厉兵秣马A ants【八皇后拓展】poj3565

Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees. Each ant colony needs its own apple tree to feed itself.

Bill has a map with coordinates of n ant colonies and n apple trees. He knows that ants travel from their colony to their feeding places and back using chemically tagged routes. The routes cannot intersect each other or ants will get confused and get to the wrong colony or tree, thus spurring a war between colonies.

Bill would like to connect each ant colony to a single apple tree so that all n routes are non-intersecting straight lines. In this problem such connection is always possible. Your task is to write a program that finds such connection.

On this picture ant colonies are denoted by empty circles and apple trees are denoted by filled circles. One possible connection is denoted by lines.

Input

The first line of the input file contains a single integer number n (1 ≤ n ≤ 100) — the number of ant colonies and apple trees. It is followed by n lines describing n ant colonies, followed by n lines describing n apple trees. Each ant colony and apple tree is described by a pair of integer coordinates x and y (− 10 000  ≤ xy ≤  10 000 ) on a Cartesian plane. All ant colonies and apple trees occupy distinct points on a plane. No three points are on the same line.

Output

Write to the output file n lines with one integer number on each line. The number written on i-th line denotes the number (from 1 to n) of the apple tree that is connected to the i-th ant colony.

Sample Input

5
-42 58
44 86
7 28
99 34
-13 -59
-47 -44
86 74
68 -75
-68 60
99 -60

Sample Output

4
2
1
5
3

这套题是2007欧洲某区域赛的原题== 在poj上都有 source:Northeastern Europe 2007 

第一题找蚂蚁和树配对连直线 给出坐标要求连线们都不相交 看完题觉得是二分图匹配 觉得难就没做 读第2题的时候发现过得人多了才做的 后来听后面那个队伍也这么想的 哈哈可惜n<=100 这么小的数据量用的着那么复杂的算法吗

O(n*n)的算法分分钟搞定 才79ms~

#include <cstdio>
#include <algorithm>
using namespace std;
#define N 101

#define DATATYPE double
#define PRECISION 1e-8
struct POINT {
    DATATYPE x, y;

    POINT(DATATYPE x, DATATYPE y) :
        x(x), y(y) {
    }

    POINT() :
        x(0), y(0) {
    }

};
POINT p1[N], p2[N];
inline int sgn(double x) {
    return (x < -PRECISION) ? -1 : x > PRECISION;
}

double dotdet(double x1, double y1, double x2, double y2) {
    return x1 * x2 + y1 * y2;
}

double crossdet(double x1, double y1, double x2, double y2) {
    return x1 * y2 - x2 * y1;
}

//右手螺旋定则,1:a在cd右侧,-1:a在cd左侧,0:三点共线
int cross(const POINT &a, const POINT &c, const POINT &d) {
    return sgn(crossdet(a.x - c.x, a.y - c.y, d.x - c.x, d.y - c.y));
}

//在cross(a,c,d)==0的基础上,可判断点a是否在cd内部
//使用点积判断前后的典型例子。若v(CA)•v(DA) < 0, 则点A必在CD间
int between(const POINT &a, const POINT &c, const POINT &d) {
    return sgn(dotdet(c.x - a.x, c.y - a.y, d.x - a.x, d.y - a.y)) != 1;
}

//两线段相交情况:0:不相交,1:规范相交,2:不规范相交(交于端点或重合)
int seg_intersect(const POINT &a, const POINT &b, const POINT &c, const POINT &d) {
    int a_cd = cross(a, c, d);
    if (a_cd == 0 && between(a, c, d)) return 2;
    int b_cd = cross(b, c, d);
    if (b_cd == 0 && between(b, c, d)) return 2;
    int c_ab = cross(c, a, b);
    if (c_ab == 0 && between(c, a, b)) return 2;
    int d_ab = cross(d, a, b);
    if (d_ab == 0 && between(d, a, b)) return 2;
    if ((a_cd ^ b_cd) == -2 && (c_ab ^ d_ab) == -2) return 1;
    return 0;
}
int main()
{
    int n, i, j, ans[N];
    scanf("%d", &n);
    for(i = 0; i < n ;i++)
        scanf("%lf%lf", &p1[i].x, &p1[i].y);
    for(i = 0; i < n ;i++)
        scanf("%lf%lf", &p2[i].x, &p2[i].y);
    for(i = 0; i < n; i++)
        ans[i] = i;
    while(1)
    {
        int ok = 1;
        for(i = 0; i < n; i++)
        {
            for(j = i+1; j < n; j++)
            {
                if(seg_intersect(p1[i], p2[ans[i]], p1[j], p2[ans[j]]))
                {
                    swap(ans[i], ans[j]);
                    ok = 0;
                    break;
                }
            }
        }
        if(ok)
            break;
    }
    for(i = 0; i < n; i++)
        printf("%d\n", ans[i]+1);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值