poj-3565 Ants(计算几何+调整算法)

题目链接:点击打开链接

Ants
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5917   Accepted: 1833   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.

poj-3565 Ants(计算几何+调整算法)_第1张图片

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

题意:给n个白点和n个黑点。将每个白点对应一个黑点,要求所有线段不能相交,输入确保会有解。

思路:枚举出几乎所有的情况,遇到两条线段相交就交换就可以。 满足情况就跳出。 

题目说的是整形输入,不过用int会WA,要么用double要么用long long...我也不知道为什么,明明说的-10000到10000 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 510
#define eps 1e-8
struct point
{
    double x,y;
} a[N],b[N];
double mulit(point a,point b,point c)
{
    return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
int check(point a,point b,point c,point d)
{
    if((max(a.x,b.x)<min(c.x,d.x))||(max(a.y,b.y)<min(c.y,d.y))||
            (max(c.x,d.x)<min(a.x,b.x))||(max(c.y,d.y)<min(a.y,b.y)))
                return 0;
        if(mulit(a,c,b)*mulit(a,d,b)>eps||mulit(c,a,d)*mulit(c,b,d)>eps)
            return 0;
    return 1;
}
int main()
{
    int n,ans[N];
    while(~scanf("%d",&n))
    {
        for(int i=0; i<n; i++)
            scanf("%lf %lf",&a[i].x,&a[i].y);
        for(int i=0; i<n; i++)
        {
            scanf("%lf %lf",&b[i].x,&b[i].y);
            ans[i]=i;
        }
        while(1)
        {
            int ok=1;
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<n; j++)
                {
                    if(i==j) continue;
                    if(check(a[i],b[ans[i]],a[j],b[ans[j]]))
                    {
                        swap(ans[i],ans[j]);
                        ok=0;
                    }
                }
            }
            if(ok)
                break;
        }
        for(int i=0; i<n; i++)
            printf("%d\n",ans[i]+1);
    }
    return 0;
}


你可能感兴趣的:(ACM,HDU)