hdu 1147

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值