poj 3565 (弱校联萌十一大决战之厉兵秣马 A. Ants )

简介:
Ants
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5357   Accepted: 1660   Special Judge

Description

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


题目大意:

给定一个数m,表示有m个白点和m个黑点,然后给出2*m个点(先黑点,后白点)的坐标,要求

将白点和黑点连线,但是不能交叉,输出连线的标号(只输出白点的)

解题思路:

借鉴冒泡的思想,判断是否有交叉,如果有交叉的话就交换match[i]和match[j],

只需要写一个判断的 函数就OK了,


上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

#define MM(a) memset(a,0,sizeof(a))

typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 100000+5;
const int mod = 1000000007;
const double eps = 1e-7;

struct Point
{
    double x, y;
}white[105], black[105];
///叉积
double xmul(Point p0, Point p1, Point p2)
{
    return (p1.x-p0.x)*(p2.y-p0.y) - (p1.y-p0.y)*(p2.x-p0.x);
}
///判断条件
bool Judge(Point a, Point b, Point aa, Point bb)
{
    if(max(a.x,b.x)>=min(aa.x,bb.x) && max(aa.x,bb.x)>=min(a.x,b.x)
            &&max(a.y,b.y)>=min(aa.y,bb.y) && max(aa.y,bb.y)>=min(a.y,b.y))
        if(xmul(a,b,aa)*xmul(a,b,bb) < -eps)
            if(xmul(aa,bb,a)*xmul(aa,bb,b) < -eps)
                return true;
    return false;
}
int n, match[105];
void Init()
{
    for(int i=1; i<=n; i++)
            match[i]=i;
}
int main()
{
    while(~scanf("%d",&n))
    {
        Init();
        for(int i=1; i<=n; i++)
            scanf("%lf%lf",&white[i].x,&white[i].y);
        for(int i=1; i<=n; i++)
            scanf("%lf%lf",&black[i].x,&black[i].y);
        while(1)
        {
            bool ok=true;
            ///冒泡
            for(int i=1; i<=n; i++)
                for(int j=1; j<=n; j++)
                {
                    if(i==j)
                        continue;
                    if(Judge(white[i],black[match[i]],white[j],black[match[j]]))
                    {
                        swap(match[i],match[j]);
                        ok=false;
                    }
                }
            if(ok)
                break;
        }
        for(int i=1; i<=n; i++)
            printf("%d\n",match[i]);
    }
    return 0;
}


目录
相关文章
|
9月前
UVa11157 - Dynamic Frog(动态规划)
UVa11157 - Dynamic Frog(动态规划)
37 0
|
人工智能 BI
[UVA 1599] Ideal Path | 细节最短路
Description New labyrinth attraction is open in New Lostland amusement park. The labyrinth consists of n rooms connected by m passages. Each passage is colored into some color ci .
177 0
POJ-2492,A Bug's Life(分类并查集)
POJ-2492,A Bug's Life(分类并查集)