计算几何—Toy 叉积判断点与线段的相对位置
来源:互联网 发布:高圆圆的淘宝店 编辑:程序博客网 时间:2024/06/06 18:41
A - TOYS
<span class="crawlInfo" id="crawling" style="display: none;"><strong><span style="color:green;">Crawling in process...</span></strong> </span> <span class="crawlInfo" id="crawlFailed" style="display: none;"><strong><span style="color:red;">Crawling failed</span></strong> </span> <span class="crawlInfo" id="crawlSuccess"><strong>Time Limit:</strong><span id="timeLimit">2000</span>MS <strong>Memory Limit:</strong><span id="memoryLimit">65536</span>KB <strong>64bit IO Format:</strong><span id="_64IOFormat">%I64d & %I64u</span> </span>
<div style="margin: auto; width: 960px;"><div class="hiddable" id="vj_description" style="display: block;"><p class="pst">Description</p><div class="textBG"><div class="ptx" lang="en-US">Calculate the number of toys that land in each bin of a partitioned toy box.Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys in, but John is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for John to find his favorite toys.John's parents came up with the following idea. They put cardboard partitions into the box. Even if John keeps throwing his toys into the box, at least toys that get thrown into different bins stay separated. The following diagram shows a top view of an example toy box.<img src="http://poj.org/images/2318_1.jpg" alt="" />For this problem, you are asked to determine how many toys fall into each partition as John throws them into the toy box.</div></div></div><div class="hiddable" id="vj_input" style="display: block;"><p class="pst">Input</p><div class="textBG"><div class="ptx" lang="en-US">The input file contains one or more problems. The first line of a problem consists of six integers, n m <span data-scayt_word="x1" data-scaytid="1">x1</span> <span data-scayt_word="y1" data-scaytid="3">y1</span> <span data-scayt_word="x2" data-scaytid="6">x2</span> <span data-scayt_word="y2" data-scaytid="8">y2</span>. The number of cardboard partitions is n (0 < n <= 5000) and the number of toys is m (0 < m <= 5000). The coordinates of the upper-left corner and the lower-right corner of the box are (<span data-scayt_word="x1" data-scaytid="2">x1</span>,<span data-scayt_word="y1" data-scaytid="4">y1</span>) and (<span data-scayt_word="x2" data-scaytid="7">x2</span>,<span data-scayt_word="y2" data-scaytid="9">y2</span>), respectively. The following n lines contain two integers per line, <span data-scayt_word="Ui" data-scaytid="11">Ui</span> Li, indicating that the ends of the <span data-scayt_word="i-th" data-scaytid="13">i-th</span> cardboard partition is at the coordinates (<span data-scayt_word="Ui" data-scaytid="12">Ui</span>,<span data-scayt_word="y1" data-scaytid="5">y1</span>) and (Li,<span data-scayt_word="y2" data-scaytid="10">y2</span>). You may assume that the cardboard partitions do not intersect each other and that they are specified in sorted order from left to right. The next m lines contain two integers per line, <span data-scayt_word="Xj" data-scaytid="14">Xj</span> <span data-scayt_word="Yj" data-scaytid="15">Yj</span> specifying where the <span data-scayt_word="j-th" data-scaytid="16">j-th</span> toy has landed in the box. The order of the toy locations is random. You may assume that no toy will land exactly on a cardboard partition or outside the boundary of the box. The input is terminated by a line consisting of a single 0.</div></div></div><div class="hiddable" id="vj_output" style="display: block;"><p class="pst">Output</p><div class="textBG"><div class="ptx" lang="en-US">The output for each problem will be one line for each separate bin in the toy box. For each bin, print its bin number, followed by a colon and one space, followed by the number of toys thrown into that bin. Bins are numbered from 0 (the leftmost bin) to n (the rightmost bin). Separate the output of different problems by a single blank line.</div></div></div><div class="hiddable" id="vj_sampleInput" style="display: block;"><p class="pst">Sample Input</p><div class="textBG"><pre class="sio">5 6 0 10 60 03 14 36 810 1015 301 52 12 85 540 107 94 10 0 10 100 020 2040 4060 6080 80 5 1015 1025 1035 1045 1055 1065 1075 1085 1095 100
Sample Output
0: 21: 12: 13: 14: 05: 10: 21: 22: 23: 24: 2
Hint
As the example illustrates, toys that fall on the boundary of the box are "in" the box.
//上述是问题描述//##include <iostream>#include <cstdio>#include <string>#include <cstdlib>using namespace std;const int max_n = 100000;int Up[max_n] = { 0 };int Down[max_n] = { 0 };int ans[max_n + 1] = { 0 };struct Vector{int x, y;Vector(){};Vector(int _x, int _y)//以原点为起点的向量坐标{x = _x;y = _y;}Vector(int _x1, int _y1, int _x2, int _y2)//以其他的任意两点的向量{x = _x2 - _x1;y = _y2 - _y1;}Vector operator +(const Vector &b){return (Vector(x + b.x, y + b.y));}Vector operator -(const Vector &b){return (Vector(x - b.x, y - b.y));}int operator *(const Vector &b){return (x*b.x + y*b.y);}int operator ^(const Vector &b){return (x*b.y - y*b.x);}};int main(){int n, m;int x1, y1, x2, y2;void BinarySearch(int x, int y, int card_n, int y1, int y2);while (cin >> n){if (n != 0){cin >> m >> x1 >> y1 >> x2 >> y2;for (int i = 1; i < n + 1; i++){cin >> Up[i] >> Down[i];}Up[0] = x1;Down[0] = x1;Up[n + 1] = x2;Up[n + 1] = x2;for (int i = 0; i < m; i++){int x, y;cin >> x >> y;BinarySearch(x, y, n, y1, y2);}for (int i = 0; i < n + 1; i++){cout << i << ":" << ans[i] << endl;}}else{break;}memset(ans,0,sizeof(ans));}system("pause");return 0;}void BinarySearch(int x, int y, int card_n,int y1,int y2){int left, right;left = 0;right = card_n+1;int mid;int temp;while (right >left+1){mid = (right + left) / 2;struct Vector card = Vector(Down[mid], y2, Up[mid], y1);struct Vector toy = Vector(Down[mid], y2, x, y);int flag = toy^card;if (flag < 0){//temp = mid;right = mid;}else{left = mid;}}ans[left]++;}include <iostream>#include <cstdio>#include <string>#include <cstdlib>using namespace std;const int max_n = 100000;int Up[max_n] = { 0 };int Down[max_n] = { 0 };int ans[max_n + 1] = { 0 };struct Vector{ int x, y; Vector(){}; Vector(int _x, int _y)//以原点为起点的向量坐标 { x = _x; y = _y; } Vector(int _x1, int _y1, int _x2, int _y2)//以其他的任意两点的向量 { x = _x2 - _x1; y = _y2 - _y1; } Vector operator +(const Vector &b) { return (Vector(x + b.x, y + b.y)); } Vector operator -(const Vector &b) { return (Vector(x - b.x, y - b.y)); } int operator *(const Vector &b) { return (x*b.x + y*b.y); } int operator ^(const Vector &b) { return (x*b.y - y*b.x); }};int main(){ int n, m; int x1, y1, x2, y2; void BinarySearch(int x, int y, int card_n, int y1, int y2); while (cin >> n) { if (n != 0) { cin >> m >> x1 >> y1 >> x2 >> y2; for (int i = 1; i < n + 1; i++) { cin >> Up[i] >> Down[i]; } Up[0] = x1; Down[0] = x1; Up[n + 1] = x2; Up[n + 1] = x2; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; BinarySearch(x, y, n, y1, y2); } for (int i = 0; i < n + 1; i++) { cout << i << ":" << ans[i] << endl; } } else { break; } memset(ans,0,sizeof(ans)); } system("pause"); return 0;}void BinarySearch(int x, int y, int card_n,int y1,int y2){ int left, right; left = 0; right = card_n+1; int mid; int temp; while (right >left+1) { mid = (right + left) / 2; struct Vector card = Vector(Down[mid], y2, Up[mid], y1); struct Vector toy = Vector(Down[mid], y2, x, y); int flag = toy^card; if (flag < 0) { //temp = mid; right = mid; } else { left = mid; } } ans[left]++;}
总结:该题用到了计算几何中通过向量的叉积来判断两个向量之间顺逆时针的方法,即通过顺逆时针的判断来确定点与直线(线段)的相对位置关系
该题是计算几何中一个入门的题,需要自己独立认真完成。
0 0
- 计算几何—Toy 叉积判断点与线段的相对位置
- POJ 2398 Toy Storage(计算几何,叉积判断点和线段的关系)
- poj2318——TOYS(计算几何+点与线段的位置)
- 计算几何-点与多边形的位置
- POJ 1410Intersection 计算几何 判断线段与矩形位置
- poj 2398 Toy Storage(计算几何,点跟直线的位置关系)
- 几何+点与线段的位置关系+二分(POJ2318)
- POJ 2398 Toy Storage 计算几何(点与直线的关系)
- HDU1756--判断点与多边形的相对位置
- 判断一个点与直线的相对位置
- <模板><计算几何>点与多边形的位置关系
- 计算几何问题汇总--点与线的位置关系
- 计算几何问题汇总--点与线的位置关系
- POJ 2318 TOYS(叉积判断点与线段的位置关系)
- POJ 2398 Toy Storage (判断点与线段关系)
- POJ 2398 Toy Storage [叉积判断+二分查找]【计算几何】
- poj 2398 poj 2318 二分+叉乘 判断 点与线段的位置
- 【计算几何】判断线段相交
- 搭建hbase-0.94.26集群环境
- 【Nutch2.3基础教程】集成Nutch/Hadoop/Hbase/Solr构建搜索引擎:安装及运行【集群环境】
- Nutch关于robot.txt的处理
- /root/.bashrc与/etc/profile的异同
- 在Eclipse中运行Nutch2.3
- 计算几何—Toy 叉积判断点与线段的相对位置
- SSD 之ECC校验和纠错
- Gora官方范例
- 软件项目版本号的命名规则及格式
- Gora快速入门
- ORACLE10g数据库补丁激活
- Linux 系统挂载数据盘
- Gora官方文档之二:Gora对Map-Reduce的支持
- Hbase常见异常