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 ;
}

最新文章

  1. python的继承
  2. Java 读取xlsx
  3. 关于安装Visual Studio 2015 RC版卡主不动的解决方案
  4. aspcms常见问题解决方案
  5. 利用vba将excel中的图片链接直接转换为图片
  6. windows远程桌面连接配置
  7. root运行/media可运行文件权限不够,chmod改动权限无效
  8. [code]高精度运算
  9. python实现断点续传下载文件
  10. [模板] 动态树/LCT
  11. css 兼容各种iPhone
  12. Windbg程序调试系列4-Live Debugging
  13. Android Studio 管理所有程序退出
  14. python基础之 编码进阶,文件操作和深浅copy
  15. while +for+字符串
  16. JS数组和对象的浅拷贝和深拷贝
  17. Shell操作mysql数据库
  18. PhpDocumentor
  19. APUE-文件和目录(二)函数access,mask,chmod和粘着位
  20. pcd转obj

热门文章

  1. 001-window版redis安装
  2. 005-SpringBoot2.x整合Security5(解决 There is no PasswordEncoder mapped for the id &quot;null&quot;)
  3. 使用PHP创建一个REST API(译)
  4. linux 启动引导流程
  5. sql中字符串如何比大小
  6. workerman定时任务使用
  7. 登陆跳板机每天只输入一次token的方法——ssh clone session
  8. SV中的线程
  9. Codeforces Round #246 (Div. 2) D E
  10. 【kafka学习之三】kafka集群运维