poj2653 Pick-up sticks

本文介绍了一种通过倒序检查线段是否被其他线段覆盖的方法,并提供了完整的C++实现代码。利用叉乘来判断线段相交,最终确定哪些线段未被覆盖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意:

按顺序放上一些线段,为最后未被其他线段覆盖的线段有哪些?

解题思路:

数据很水,n2也能过,主要考察线段相交的判断。
倒着加入,看每条线段有没有与先加入的相交,没有则不被覆盖。
若两条线段相交,则其两端点分别在另一条线段两侧,用叉乘判断即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<ctime>
#include<vector>
#include<queue>
#define ll long long
using namespace std;

int getint()
{
    int i=0,f=1;char c;
    for(c=getchar();(c!='-')&&(c<'0'||c>'9');c=getchar());
    if(c=='-')c=getchar(),f=-1;
    for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
    return i*f;
}

const int N=100005;
int n,m,ans[N];
struct point
{
    double x,y;
    point(){}
    point(double _x,double _y):
        x(_x),y(_y){}
    friend inline point operator -(const point &a,const point &b)
    {return point(a.x-b.x,a.y-b.y);}
    friend inline double operator *(const point &a,const point &b)
    {return a.x*b.y-a.y*b.x;}
};
struct node
{
    point s,t;
}a[N];

bool Inter(node u,node v)
{
    return ((u.t-u.s)*(v.t-u.s))*((u.t-u.s)*(v.s-u.s))<=0&&((v.t-v.s)*(u.s-v.s))*((v.t-v.s)*(u.t-v.s))<=0;
}

int main()
{
    //freopen("lx.in","r",stdin);
    //freopen("lx.out","w",stdout);
    while(n=getint())
    {
        if(!n)break;
        m=0;
        for(int i=1;i<=n;i++)
            scanf("%lf%lf%lf%lf",&a[i].s.x,&a[i].s.y,&a[i].t.x,&a[i].t.y);
        for(int i=n;i;i--)
        {
            int bz=1;
            for(int j=i+1;j<=n;j++)
                if(Inter(a[i],a[j]))
                {
                    bz=0;
                    break;
                }
            if(bz)ans[++m]=i;
        }
        printf("Top sticks: ");
        for(int i=m;i>1;i--)printf("%d, ",ans[i]);
        printf("%d.\n",ans[1]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值