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 ...
随机推荐
- [转]hive中order by,distribute by,sort by,cluster by
转至http://my.oschina.net/repine/blog/296562 order by,distribute by,sort by,cluster by 查询使用说明 1 2 3 4 ...
- error proc
/*************************************************************************\ * Copyright (C) Michael ...
- bzoj 1002 [FJOI2007]轮状病毒 高精度&&找规律&&基尔霍夫矩阵
1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2234 Solved: 1227[Submit][Statu ...
- Matlab聚类分析[转]
Matlab聚类分析[转] Matlab提供系列函数用于聚类分析,归纳起来具体方法有如下: 方法一:直接聚类,利用clusterdata函数对样本数据进行一次聚类,其缺点为可供用户选择的面较窄,不能更 ...
- 【HDU4552】 怪盗基德的挑战书(后缀数组)
怪盗基德的挑战书 Problem Description “在树最美丽的那天,当时间老人再次把大钟平均分开时,我会降临在灯火之城的金字塔前,带走那最珍贵的笑容.”这是怪盗基德盗取巴黎卢浮宫的<蒙 ...
- AppDomain
AppDomain http://www.cnblogs.com/xiashengwang/archive/2012/07/17/2594623.html http://www.cnblogs.com ...
- [LeetCode#244] Shortest Word Distance II
Problem: This is a follow up of Shortest Word Distance. The only difference is now you are given the ...
- -_-#flash播放器自适应
设置断点,几个断点下的固定布局
- implicitly_wait()隐式等待
# -*- coding:utf-8 -*- """ implicitly_wait():隐式等待 当使用了隐士等待执行测试的时候,如果 WebDriver没有在 DOM ...
- selenium 启动ie 浏览器
selenium 启动ie 浏览器 var driver = new InternetExplorerDriver(@"IEDriverServer.exe路径"); driver ...