poj 3565 ants
/*
poj 3565 递归分治
还有用KM的做法
这里写的分治 按紫书上的方法
不过那里说的有点冗杂了
可以简化一下
首先为啥可以分治 也就是分成子问题解决
只要有一个集合 黑白的个数相等 就一定能一一匹配
这个应该比较明显
因为是special judge
所以我们只要保证每次处理的集合黑白相等就好了
关键是怎么分
我们找到最下最左的点 作为基点
然后将其他的按照连线与横坐标夹角的大小排序
这样就相当于那个线逆时针扫一遍
当扫过去的黑白相等(都为0也可以)并且当前这个可以和基点不同色
也就是他俩能匹配 那显然当前这三部分 (扫过去的 没扫的 还有这条线)
都满足黑白相等 也就是说都能独立的匹配
然后分治就好了
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define maxn 210
using namespace std;
int n,match[maxn];
struct node{
int x,y,c,v;
}p[maxn],bas;
int cmp1(const node &a,const node &b){
return (a.y==b.y&&a.x<b.x)||a.y<b.y;
}
int cmp2(const node &a,const node &b){
double A=atan2(double(a.y-bas.y),double(a.x-bas.x));
double B=atan2(double(b.y-bas.y),double(b.x-bas.x));
return A<B;
}
void Dfs(int l,int r){
if(l>r)return;
sort(p+l,p+r+,cmp1);
bas=p[l];
sort(p+l+,p+r+,cmp2);
int s1=,s2=,k=l+;
while(s1!=s2||p[k].c==bas.c){
if(p[k].c==bas.c)s1++;
else s2++;k++;
}
if(bas.c==)match[bas.v]=p[k].v;
else match[p[k].v]=bas.v;
Dfs(l+,k-);Dfs(k+,r);
}
int main()
{
scanf("%d",&n);
memset(match,,sizeof(match));
for(int i=;i<=n;i++){
scanf("%d%d",&p[i].x,&p[i].y);
p[i].c=;p[i].v=i;
}
for(int i=+n;i<=n+n;i++){
scanf("%d%d",&p[i].x,&p[i].y);
p[i].c=;p[i].v=i-n;
}
Dfs(,n+n);
for(int i=;i<=n;i++)
printf("%d\n",match[i]);
return ;
}
poj 3565 ants的更多相关文章
- POJ 3565 Ants 【最小权值匹配应用】
传送门:http://poj.org/problem?id=3565 Ants Time Limit: 5000MS Memory Limit: 65536K Total Submissions: ...
- POJ 3565 Ants(最佳完美匹配)
Description Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on ...
- POJ 3565 Ants (最小权匹配)
题意 给出一些蚂蚁的点,给出一些树的点,两两对应,使他们的连线不相交,输出一种方案. 思路 一开始没想到怎么用最小权匹配--后来发现是因为最小权匹配的方案一定不相交(三角形两边之和大于第三边)--还是 ...
- POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题
两题很有趣挺经典的蚂蚁问题. 1.n只蚂蚁以1cm/s的速度在长为L的竿上爬行,当蚂蚁爬到竿子的端点就会掉落.当两只蚂蚁相撞时,只能各自反向爬回去.对于每只蚂蚁,给出距离左端的距离xi,但不知道它的朝 ...
- POJ 1852 Ants(贪心)
POJ 1852 Ants 题目大意 有n只蚂蚁在木棍上爬行,每只蚂蚁的速度都是每秒1单位长度,现在给你所有蚂蚁初始的位置(蚂蚁运动方向未定),蚂蚁相遇会掉头反向运动,让你求出所有蚂蚁都·掉下木棍的最 ...
- poj 3565 uva 1411 Ants KM算法求最小权
由于涉及到实数,一定,一定不能直接等于,一定,一定加一个误差<0.00001,坑死了…… 有两种事物,不难想到用二分图.这里涉及到一个有趣的问题,这个二分图的完美匹配的最小权值和就是答案.为啥呢 ...
- [POJ 3565] Ant
[题目链接] http://poj.org/problem?id=3565 [算法] KM算法求最小匹配 [代码] #include <algorithm> #include <bi ...
- poj 1852 ants 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1852 题目描述 Description An army of ants walk on a horizontal pole of len ...
- poj 2565 Ants (KM+思维)
Ants Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4125 Accepted: 1258 Special Ju ...
随机推荐
- select 模型
http://www.cnblogs.com/Anker/p/3258674.html http://www.cnblogs.com/cozy/articles/2088128.html http:/ ...
- android学习小例子——验证码倒计时按钮
1.activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro ...
- 网页错误404 or 500
HTTP 错误 400 400 请求出错 由于语法格式有误,服务器无法理解此请求.不作修改,客户程序就无法重复此请求. HTTP 错误 401 401.1 未授权:登录失败 此错误表明传输给服务器的证 ...
- 也说说EM
也说说EM [本文链接:http://www.cnblogs.com/breezedeus/archive/2012/08/12/2634466.html,转载请注明出处] 前几天看Andrew Ng ...
- Loading CSS without blocking render
The principles behind these techniques aren't new. Filament group, for example, have published great ...
- New Distinct Substrings
spoj705:http://www.spoj.com/problems/SUBST1/ 题意:和spoj694一样,只是数据范围变大了. 题解:同spoj694. #include<iostr ...
- QT 读写二进制 (数值)高位在前
在人们的计数规则中,一般都认为高位在前,即往前的地位大,如123,我们认为是一百二十三, 但在计算机中数值是以二进制存储的,字节是最小的存储单位,如int(32位),占4个字节,每个字节有八位, 24 ...
- (转)Mono for Android 优势与劣势
最近有兴趣了解一下Mono for Andriod,也就是使用.NET平台来开发Andriod程序.Mono for Android API 几乎映射标准的Andriod API.例如,两边API几乎 ...
- Oracle RAC的五大优势及其劣势
Oracle RAC的五大优势及其劣势 不同的集群产品都有自己的特点,RAC的特点包括如下几点: 双机并行.RAC是一种并行模式,并不是传统的主备模式.也就是说,RAC集群的所有成员都可以同时接收客户 ...
- [LeetCode#202] Roman to Integer
Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...