UVa 1411 Ants(分治)

时间:2024-05-20 20:34:08

https://vjudge.net/problem/UVA-1411

题意:n只蚂蚁和n颗苹果树,一一配对并且不能交叉。

思路:这就是巨人与鬼的问题。用分治法就行了。

 #include<iostream>
#include<algorithm>
#include<set>
using namespace std; int n;
const int maxn = ; int vis[maxn]; struct node
{
int x, y;
int id;
int flag;
}ans[maxn]; node s; bool cmp1(node a, node b) //按y坐标从小到大排序
{
return a.y < b.y || (a.y == b.y && a.x < b.x);
} bool cmp2(node a, node b) //极角从小到大排序
{
return ((a.x - s.x)*(b.y - s.y) - (a.y - s.y)*(b.x - s.x))<;
} void solve(int l,int r)
{
if (l>r) return;
sort(ans + l, ans + r + , cmp1);
s = ans[l];
sort(ans + l + , ans + r + , cmp2);
int cnt1 = , cnt2 = ;
int k = r;
while (!(s.flag != ans[k].flag && cnt1 == cnt2))
{
if (ans[k].flag == s.flag) cnt1++;
else cnt2++;
k--;
}
if (!s.flag) vis[s.id] = ans[k].id;
else vis[ans[k].id] = s.id;
solve(l + , k - );
solve(k + , r);
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
while (cin >> n && n)
{
for (int i = ; i <= n; i++)
{
cin >> ans[i].x >> ans[i].y;
ans[i].id = i; //蚂蚁编号
ans[i].flag = ; //0代表蚂蚁
}
for (int i = n + ; i <= * n; i++)
{
cin >> ans[i].x >> ans[i].y;
ans[i].id = i - n; //苹果树编号
ans[i].flag = ; //1代表苹果树
}
solve(,*n);
for (int i = ; i <= n; i++)
cout << vis[i] << endl;
}
return ;
}