hdu-1147 Pick-up sticks(判断线段相交)

题目链接:点击打开链接

Pick-up sticks

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2680    Accepted Submission(s): 978


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.
题意:依次投出n根木棍,求最上面的木棍的条数。

思路:枚举木棍,求出没有被覆盖的木棍条数。

代码:第一个655ms,第二个超时了。我觉得两个应该差不多,都是O(n²),可能是数据的原因。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 100010
struct Line
{
    double x1,y1,x2,y2;
} line[N];
struct Node
{
    double x,y;
};
int anss[N];
double ans(Node a,Node b)
{
    return a.x*b.y-a.y*b.x;
}
int check(Line a,Line b)
{
    if(!(min(a.x1,a.x2)<=max(b.x1,b.x2)&&min(a.y1,a.y2)<=max(b.y1,b.y2)&&
            min(b.x1,b.x2)<=max(a.x1,a.x2)&&min(b.y1,b.y2)<=max(a.y1,a.y2)))
        return 0;
    Node p1q1,p2q1,q1p1,q2p1,q2q1,p2p1;
    p1q1.x=a.x1-b.x1;
    p1q1.y=a.y1-b.y1;
    p2q1.x=a.x2-b.x1;
    p2q1.y=a.y2-b.y1;
    q1p1.x=b.x1-a.x1;
    q1p1.y=b.y1-a.y1;
    q2p1.x=b.x2-a.x1;
    q2p1.y=b.y2-a.y1;
    q2q1.x=b.x2-b.x1;
    q2q1.y=b.y2-b.y1;
    p2p1.x=a.x2-a.x1;
    p2p1.y=a.y2-a.y1;
    if(ans(p1q1,q2q1)*ans(p2q1,q2q1)>0) return 0;
    if(ans(q1p1,p2p1)*ans(q2p1,p2p1)>0) return 0;
    return 1;
}
int main()
{
    int n,cnt;
    while(~scanf("%d",&n)&&n)
    {
        cnt=0;
        for(int i=0; i<n; i++)
            scanf("%lf %lf %lf %lf",&line[i].x1,&line[i].y1,&line[i].x2,&line[i].y2);
        for(int i=0; i<n; i++)
        {
            int flag=1;
            for(int j=i+1; j<n; j++)
                if(check(line[i],line[j]))
                {
                    flag=0;
                    break;
                }
            if(flag)
                anss[cnt++]=i+1;
        }
        printf("Top sticks: ");
        printf("%d",anss[0]);
        for(int i=1; i<cnt; i++)
                printf(", %d",anss[i]);
        printf(".\n");
    }
    return 0;
}
</pre><pre style="font-family: 'Courier New'; background-color: rgb(244, 251, 255);">
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 100010
struct Line
{
    double x1,y1,x2,y2;
}line[N];
struct Node
{
    double x,y;
};
int flag[N];
double ans(Node a,Node b)
{
    return a.x*b.y-a.y*b.x;
}
int check(Line a,Line b)
{
    if(!(min(a.x1,a.x2)<=max(b.x1,b.x2)&&min(a.y1,a.y2)<=max(b.y1,b.y2)&&
         min(b.x1,b.x2)<=max(a.x1,a.x2)&&min(b.y1,b.y2)<=max(a.y1,a.y2)))
            return 0;
    Node p1q1,p2q1,q1p1,q2p1,q2q1,p2p1;
    p1q1.x=a.x1-b.x1;
    p1q1.y=a.y1-b.y1;
    p2q1.x=a.x2-b.x1;
    p2q1.y=a.y2-b.y1;
    q1p1.x=b.x1-a.x1;
    q1p1.y=b.y1-a.y1;
    q2p1.x=b.x2-a.x1;
    q2p1.y=b.y2-a.y1;
    q2q1.x=b.x2-b.x1;
    q2q1.y=b.y2-b.y1;
    p2p1.x=a.x2-a.x1;
    p2p1.y=a.y2-a.y1;
    if(ans(p1q1,q2q1)*ans(p2q1,q2q1)>0) return 0;
    if(ans(q1p1,p2p1)*ans(q2p1,p2p1)>0) return 0;
    return 1;
}
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        memset(flag,0,sizeof(flag));
        for(int i=0;i<n;i++)
        {
            scanf("%lf %lf %lf %lf",&line[i].x1,&line[i].y1,&line[i].x2,&line[i].y2);
            for(int j=0;j<i;j++)
            {
                if(!flag[j]&&check(line[j],line[i]))
                    flag[j]=1;
            }
        }
        int num=0;
        printf("Top sticks: ");
        for(int i=0;i<n;i++)
            {
                if(!flag[i])
                {
                    if(num)
                    printf(", ");
                    printf("%d",i+1);
                    num=1;
                }
            }
        printf(".\n");
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值