zoukankan      html  css  js  c++  java
  • Pick-up sticks(判断两直线相交)

    Pick-up sticks
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 11335   Accepted: 4250

    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.
    题解:这个题是让找出与其他直线不交或者在其他直线最上面的直线;
    判断相交,两个直线的一个端点都要判断;
    代码:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #define mem(x,y) memset(x,y,sizeof(x))
     7 using namespace std;
     8 typedef long long LL;
     9 const int INF=0x3f3f3f3f;
    10 const int MAXN=100010<<1;
    11 struct Point{
    12     double x,y;
    13     Point(double x=0,double y=0):x(x),y(y){}
    14 };
    15 typedef Point Vector;
    16 Point dt[MAXN];
    17 Vector operator - (Point a,Point b){return Vector(a.x-b.x,a.y-b.y);}
    18 double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}
    19 int main(){
    20     int n,k,j;
    21     while(scanf("%d",&n),n){
    22         k=0;
    23         for(int i=1;i<=n;i++)
    24             scanf("%lf%lf%lf%lf",&dt[i].x,&dt[i].y,&dt[i+n].x,&dt[i+n].y);
    25             printf("Top sticks: ");
    26         for(int i=1;i<n;i++){
    27             for(j=i+1;j<=n;j++){
    28                 if(Cross(dt[i]-dt[j],dt[i]-dt[j+n])*Cross(dt[i+n]-dt[j],dt[i+n]-dt[j+n])<=0)
    29                     if(Cross(dt[j]-dt[i],dt[j]-dt[i+n])*Cross(dt[j+n]-dt[i],dt[j+n]-dt[i+n])<=0)
    30                         break;
    31             }
    32             if(j==n+1)printf("%d, ",i);
    33         }
    34         printf("%d.
    ",n); 
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    NYOJ 625 笨蛋的难题(二)
    NYOJ 102 次方求模
    ZJU Least Common Multiple
    ZJUOJ 1073 Round and Round We Go
    NYOJ 709 异形卵
    HDU 1279 验证角谷猜想
    BNUOJ 1015 信息战(一)——加密程序
    HDU 1202 The calculation of GPA
    "蓝桥杯“基础练习:字母图形
    "蓝桥杯“基础练习:数列特征
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4946074.html
Copyright © 2011-2022 走看看