UVa 1411 Ants(分治)
2024-10-16 13:33:58
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 ;
}
最新文章
- python的继承
- Java 读取xlsx
- 关于安装Visual Studio 2015 RC版卡主不动的解决方案
- aspcms常见问题解决方案
- 利用vba将excel中的图片链接直接转换为图片
- windows远程桌面连接配置
- root运行/media可运行文件权限不够,chmod改动权限无效
- [code]高精度运算
- python实现断点续传下载文件
- [模板] 动态树/LCT
- css 兼容各种iPhone
- Windbg程序调试系列4-Live Debugging
- Android Studio 管理所有程序退出
- python基础之 编码进阶,文件操作和深浅copy
- while +for+字符串
- JS数组和对象的浅拷贝和深拷贝
- Shell操作mysql数据库
- PhpDocumentor
- APUE-文件和目录(二)函数access,mask,chmod和粘着位
- pcd转obj
热门文章
- 001-window版redis安装
- 005-SpringBoot2.x整合Security5(解决 There is no PasswordEncoder mapped for the id ";null";)
- 使用PHP创建一个REST API(译)
- linux 启动引导流程
- sql中字符串如何比大小
- workerman定时任务使用
- 登陆跳板机每天只输入一次token的方法——ssh clone session
- SV中的线程
- Codeforces Round #246 (Div. 2) D E
- 【kafka学习之三】kafka集群运维