poj 3565 (弱校联萌十一大决战之厉兵秣马 A. Ants )
2015-10-05 13:06
513 查看
Ants
Description
Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees. Each ant colony needs its own apple tree to feed itself.
Bill has a map with coordinates of n ant colonies and n apple trees. He knows that ants travel from their colony to their feeding places and back using chemically tagged routes. The routes cannot intersect each other or ants will get confused
and get to the wrong colony or tree, thus spurring a war between colonies.
Bill would like to connect each ant colony to a single apple tree so that all n routes are non-intersecting straight lines. In this problem such connection is always possible. Your task is to write a program that finds such connection.
On this picture ant colonies are denoted by empty circles and apple trees are denoted by filled circles. One possible connection is denoted by lines.
Input
The first line of the input file contains a single integer number n (1 ≤ n ≤ 100) — the number of ant colonies and apple trees. It is followed by n lines describing n ant colonies, followed by n lines describing n apple trees. Each ant
colony and apple tree is described by a pair of integer coordinates x and y (−10 000 ≤ x, y ≤ 10 000) on a Cartesian plane. All ant colonies and apple trees occupy distinct points on a plane. No
three points are on the same line.
Output
Write to the output file n lines with one integer number on each line. The number written on i-th line denotes the number (from 1 to n) of the apple tree that is connected to the i-th ant colony.
Sample Input
Sample Output
题目大意:
给定一个数m,表示有m个白点和m个黑点,然后给出2*m个点(先黑点,后白点)的坐标,要求
将白点和黑点连线,但是不能交叉,输出连线的标号(只输出白点的)
解题思路:
借鉴冒泡的思想,判断是否有交叉,如果有交叉的话就交换match[i]和match[j],
只需要写一个判断的 函数就OK了,
上代码:
Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 5357 | Accepted: 1660 | Special Judge |
Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees. Each ant colony needs its own apple tree to feed itself.
Bill has a map with coordinates of n ant colonies and n apple trees. He knows that ants travel from their colony to their feeding places and back using chemically tagged routes. The routes cannot intersect each other or ants will get confused
and get to the wrong colony or tree, thus spurring a war between colonies.
Bill would like to connect each ant colony to a single apple tree so that all n routes are non-intersecting straight lines. In this problem such connection is always possible. Your task is to write a program that finds such connection.
On this picture ant colonies are denoted by empty circles and apple trees are denoted by filled circles. One possible connection is denoted by lines.
Input
The first line of the input file contains a single integer number n (1 ≤ n ≤ 100) — the number of ant colonies and apple trees. It is followed by n lines describing n ant colonies, followed by n lines describing n apple trees. Each ant
colony and apple tree is described by a pair of integer coordinates x and y (−10 000 ≤ x, y ≤ 10 000) on a Cartesian plane. All ant colonies and apple trees occupy distinct points on a plane. No
three points are on the same line.
Output
Write to the output file n lines with one integer number on each line. The number written on i-th line denotes the number (from 1 to n) of the apple tree that is connected to the i-th ant colony.
Sample Input
5 -42 58 44 86 7 28 99 34 -13 -59 -47 -44 86 74 68 -75 -68 60 99 -60
Sample Output
4 2 1 5 3
题目大意:
给定一个数m,表示有m个白点和m个黑点,然后给出2*m个点(先黑点,后白点)的坐标,要求
将白点和黑点连线,但是不能交叉,输出连线的标号(只输出白点的)
解题思路:
借鉴冒泡的思想,判断是否有交叉,如果有交叉的话就交换match[i]和match[j],
只需要写一个判断的 函数就OK了,
上代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <algorithm> #include <set> using namespace std; #define MM(a) memset(a,0,sizeof(a)) typedef long long LL; typedef unsigned long long ULL; const int maxn = 100000+5; const int mod = 1000000007; const double eps = 1e-7; struct Point { double x, y; }white[105], black[105]; ///叉积 double xmul(Point p0, Point p1, Point p2) { return (p1.x-p0.x)*(p2.y-p0.y) - (p1.y-p0.y)*(p2.x-p0.x); } ///判断条件 bool Judge(Point a, Point b, Point aa, Point bb) { if(max(a.x,b.x)>=min(aa.x,bb.x) && max(aa.x,bb.x)>=min(a.x,b.x) &&max(a.y,b.y)>=min(aa.y,bb.y) && max(aa.y,bb.y)>=min(a.y,b.y)) if(xmul(a,b,aa)*xmul(a,b,bb) < -eps) if(xmul(aa,bb,a)*xmul(aa,bb,b) < -eps) return true; return false; } int n, match[105]; void Init() { for(int i=1; i<=n; i++) match[i]=i; } int main() { while(~scanf("%d",&n)) { Init(); for(int i=1; i<=n; i++) scanf("%lf%lf",&white[i].x,&white[i].y); for(int i=1; i<=n; i++) scanf("%lf%lf",&black[i].x,&black[i].y); while(1) { bool ok=true; ///冒泡 for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) { if(i==j) continue; if(Judge(white[i],black[match[i]],white[j],black[match[j]])) { swap(match[i],match[j]); ok=false; } } if(ok) break; } for(int i=1; i<=n; i++) printf("%d\n",match[i]); } return 0; }
相关文章推荐
- 我想一个更好的科技博客和网站集合,非常喜欢的风格
- 归并排序的实现
- 数据库 Navicat_Premium_11.0.10 破解版下载安装
- 写让别人能读懂的代码
- 随笔823
- 【POJ2114】Boatherds 树分而治之
- 点击eclipse.exe显示找不到jdk、jre,所以不能运行
- [NOIP2005]陶陶摘苹果 T1
- 爱就是一种屁!
- struts 2的常量配置
- ViewGroup的onMeasure和onLayout分析
- 一个正则表达式测试(只可输入中文、字母和数字)
- 记一次解决Visual Paradigm打开始终是在Initializing Environment的问题
- Solr在结果返回函数值
- ubuntu 14.10 共享文件夹
- 软件测试面试汇总
- Java核心技术第4章(3)
- 编码的规范
- java多维数组遍历
- java中super,this的用法举例