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.html
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.ios
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.c++
The picture to the right below illustrates the first case from input.
Sample Inputweb
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.
Hint
Huge input,scanf is recommended.app
题意:
给定一个指定数目的棍子(数量是n),每次扔出去一根(从1号扔到n号),直到最后一根扔完。求在扔出去的这些棍子中有哪些棍子是在这堆棍子的顶端(即该号棍子上方没有棍子覆盖了)。dom
c++ AC 代码ide
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #define EPS 1e-10 const int MAX = 100010; bool ans[MAX]; int n; double add(double a,double b) { if(fabs(a+b) < EPS * (fabs(a) + fabs(b)) return 0; return a + b; } struct P { double x,y; P() {} P(double x,double y) : x{x],y(y) {} P operator + (P p) { return P(add(x,p.x),add(y,p.y)); } P operator - (P p) { return P(add(x,-p,x),add(y,-p.y)); } P operator * (double d) { return P(x*d,y*d); } double dot (P p) { return add(x*p.x,y*p.y); } double det (P p) { return add(x*p.y,-y*p.x); } }; P pset[MAX][2]; double ccw(P s,P e,P p) // s表明起点 e表明终点 p是须要判断的点 { P a = e - s,b = p - s; if(a.det(b) > 0) return 1; // a、b叉积为正说明p在线段的逆时针方向 else if(a.det(b) < 0) return -1; // 叉积为负 p 在线段的逆时针方向 // 如下三种状况点在线段所在的直线上 else if(a.dot(b) < 0) return 2; // 点积小于0 两向量反向 else if(a.dot(a) < b.dot(b)) return -2; // a的长度小于b的长度 a在b上 return 0; // 点在线上(b的长度小于a的长度) } bool interesect(P p0,P p1,P p2,P p3,P p4) { return (ccw(p0,p1,p2)*ccw(p0,p1,p3)<=0 && ccw(p2,p3,p0)*ccw(p2,p3,p1)<=0); } int main() { while(~scanf("%d",&n) && n) { scanf("%lf%lf%lf%lf",&pset[0][0].x,&pset[0][0].y,&pset[0][1].x,&pset[0][1],y); for(int i=1;i<n;i++) { scanf("%lf%lf%lf%lf",&pset[i][0].x,&pset[i][0].y,&pset[i][1].x,&pset[i][1],y); ans[i] = interesect(pset[i-1][0],pset[i-1][1],pset[i][0],pset[i][1]); // 先进行一轮相邻的放的棍子的判断,减轻后面判断的压力(由于算法不够优秀,只能这样) } printf("Top sticks: "); for(int i=0;i<n;i++) { if(!ans[i+1]) // 判断不是相邻的放的棍子是否与当前棍子相交,一有相交就说明这根棍子不是最顶上的,当即退出 fo(int j=i+1;j<n;j++) { ans[i+1] = interesect(pset[i][0],pset[i][1],pset[j][0],pset[j][1]); if(ans[i+1) break; // 这里至关于一个“剪枝”的步骤 不加这个会超时 } if(!ans[i+1] && i!=n-1) printf("%d, ",i+1); } printf("%d.\n",n); } return 0; }
if(a.det(b) > 0) return 1; // a、b叉积为正说明p在线段的逆时针方向 else if(a.det(b) < 0) return -1; // 叉积为负 p 在线段的逆时针方向
当咱们分别给ccw
函数传递ccw(P2,P3,P0)
和ccw(P2,P3,P1)
时咱们获得的返回结果是一正一负,经过这两个结果相乘咱们勉强能够认为这两个线段是相交的,为何是勉强?由于还有下面的状况:
这种状况也是会产生一正一负的状况,可是两线段并不相交。因此咱们有了interesect(P0,P1,P2,P3)
函数来进行下一步的判断。我svg
(ccw(p0,p1,p2)*ccw(p0,p1,p3)<=0 && ccw(p2,p3,p0)*ccw(p2,p3,p1)<=0);
们只有用时保证P2、P3也是别落在 ,的两侧(顺时针和逆时针方向)才能判定两直线是相交的。函数