• 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;
        }   
    }
  • 相关阅读:
    opencv目录
    qt5-编译并添加opencv库
    java版gRPC实战之二:服务发布和调用
    java版gRPC实战之一:用proto生成代码
    github搜索技巧小结
    client-go实战之五:DiscoveryClient
    client-go实战之四:dynamicClient
    client-go实战之三:Clientset
    client-go实战之二:RESTClient
    client-go实战之一:准备工作
  • 原文地址:https://www.cnblogs.com/newstartCY/p/13624565.html
Copyright © 2020-2023  润新知