Pick-up sticks
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2903 Accepted Submission(s): 1072
Problem Description
Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.
Input
Input consists of a number of cases. The data for each case start with 1 ≤ n ≤ 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.
Output
For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown.
The picture to the right below illustrates the first case from input.
Sample Input
5
1 1 4 2
2 3 3 1
1 -2.0 8 4
1 4 8 2
3 3 6 -2.0
3
0 0 1 1
1 0 2 1
2 0 3 1
0
Sample Output
Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.
首先引出计算几何学中一个最基本的问题:如何判断向量在的顺时针方向还是逆时针方向?
把p0定为原点,p1的坐标是(x1,y1),p2的坐标是(x2,y2)。向量的叉积(cross product)实际上就是矩阵的行列式:
当叉积为正时,说明在的顺时针方向上;叉积为0说明两向量共线(同向或反向)。
当同时满足:
(1)和在的两侧(即一个顺时针方向上,一个在逆时针方向上)
(2)和在的两侧
时可肯定和相交。
图1
图1是线段相交的一般情形。
图2只满足第(1)条,不满足第(2)条所以不能证明和相交。
图2
图3和图4是一种特殊情况,它不满足第(2)条,因为和重合,即和的叉积为0。
可见当叉积为0时要分情况讨论,当p3在线段p1p2上时两线段相交;当p3在线段p1p2的延长线上时两线段不相交。
很明显的计算几何题目但是在操作的过程中如果用叉积处理相交问题的话需要注意的是,在叉积小于0时还存在两条向量平行但是不共线的情况,这时就需要加条件来排除这种情况,,在遍历查找时,需要注意的时从前向后查找是否被覆盖,如果从后向前遍历的话,会由于不必要的额外操作次数而出现超时的情况,在从前向后遍历时,如果出现被覆盖的情况,就直接跳出循环,代码如下
注意时间复杂度的技巧,,,,补习一下叉乘的概念
注意输入数据是double 中间用变量替换时不要手残改成了int............
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
using namespace std;
const int N = 1000100;
struct node
{
double x1, y1, x2, y2;
}p[N];
int visit[N];
int judge(node a,node b);
int main()
{
int n;
while(scanf("%d", &n)!=EOF&&n)
{
for(int i=0;i<n;i++)
{
scanf("%lf %lf %lf %lf",&p[i].x1, &p[i].y1, &p[i].x2, &p[i].y2);
}
memset(visit,0,sizeof(visit));
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(judge(p[i],p[j]))
{
visit[i]=1;
break;
}
}
}
printf("Top sticks:");
int flag=0;
for(int i=0;i<n;i++)
{
if(visit[i]==0)
{
if(flag==0)
{
printf(" %d",i+1);
flag=1;
}
else
{
printf(", %d",i+1);
}
}
}
printf(".\n");
}
return 0;
}
int judge(node a,node b)
{
double m1=(a.x2-a.x1)*(b.y1-a.y1), m2=(a.y2-a.y1)*(b.x1-a.x1);
double m3=(a.x2-a.x1)*(b.y2-a.y1), m4=(a.y2-a.y1)*(b.x2-a.x1);
double x5=(b.x2-b.x1)*(a.y1-b.y1), x6=(b.y2-b.y1)*(a.x1-b.x1);
double x7=(b.x2-b.x1)*(a.y2-b.y1), x8=(b.y2-b.y1)*(a.x2-b.x1);
if(((m1-m2)*(m3-m4)<=0)&&((x5-x6)*(x7-x8)<=0))
{
if((a.x1-a.x2)*(b.y1-b.y2)-(a.y1-a.y2)*(b.x1-b.x2)==0&&(a.x1!=b.x1||a.y1!=b.y1)&&(a.x2!=b.x1||b.y1!=a.y2))
return 0;
else
return 1;
}
return 0;
}