UVA 1411

分治策略 紫书230

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 256;
struct node{
    int x,y;
    int num;
    bool flag;//flag为false则判断为蚂蚁   
};
int ans[maxn];
node vec[maxn];
bool cmp1(const node &n1,const node &n2)
{
    return n1.y < n2.y || (n1.y == n2.y && n1.x < n2.x);
}//返回最左下角的点
node s;
bool cmp2(const node &n1,const node &n2)
{
    return (n1.x - s.x) * (n2.y - s.y) < (n2.x - s.x) * (n1.y - s.y);
}//绝对角以大到小排列
void solve(int l,int r)
{
    if(l >= r)
        return;
    sort(vec+l,vec+r+1,cmp1);
    s = vec[l];//源点
    sort(vec+l+1,vec+r+1,cmp2);
    int cnt1 = 0,cnt2 = 0;
    int temp = r;//从绝对角小顺序依次顺序找
    while(s.flag == vec[temp].flag || cnt1 != cnt2){
        if(vec[temp].flag == s.flag)  ++cnt1;
        else    ++cnt2;
        --temp;
    }
    if(!s.flag) ans[s.num] = vec[temp].num;
    else    ans[vec[temp].num] = s.num; 
    solve(l+1,temp-1);
    solve(temp+1,r);    
}
int main(){
    int n,n2;
    while(cin >> n && n){
        for(int i=1;i<=n;++i){
            cin >> vec[i].x >> vec[i].y;
            vec[i].num = i;
            vec[i].flag = false;
        }
        n2 = 2*n;
        for(int i=n+1;i<=n2;++i){
            cin >> vec[i].x >> vec[i].y;
            vec[i].num = i - n;
            vec[i].flag = true;
        }
        solve(1,n2);
        for(int i=1;i<=n;++i)
            cout << ans[i] << endl;
    }   
}

 

posted on 2020-09-07 01:36  chengyulala  阅读(106)  评论(0编辑  收藏  举报

导航