【POJ 2653】Pick-up sticks 判线段与线段交

博客围绕扔木棍问题展开,Stan 按顺序扔 n 根木棍,要找出最上面的木棍。输入给出木棍数量及端点坐标,输出按扔的顺序列出最上面的木棍。题解思路是若为最上面木棍,后面无木棍与之相交,即判断后面线段与当前线段是否相交。

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

Pick-up sticks

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 15600 Accepted: 5893

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.

题意:有顺序的扔一些木棍,让你求最上面的木棍。

题解:如果是最上面的木棍,那么后面就没有木棍和它相交。相当于判断后面的线段是否和当前线段相交。

#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn = 100000+7;
const double eps = 1e-8;
double min(double x, double y)
{
  return x < y ? x : y;
}
double max(double x, double y)
{
  return x > y ? x : y;
}
int sgn(double x)
{
  if(fabs(x) < eps) return 0;
  else if(x < 0) return -1;
  else return 1;
}
struct point{
  double x,y;
  point(){}
  point(double _x, double _y) {
    x = _x;
    y = _y;
  }
  point operator - (const point &b) const{
    return point(x-b.x, y-b.y);
  }
  double operator * (const point &b) const{  //点积
    return x*b.x + y*b.y;
  }
  double operator ^ (const point &b) const{  //叉积
    return x*b.y - y*b.x;
  }
};
struct line{
  point s,e;
  line(){}
  line(point _s, point _e) {
    s = _s;
    e = _e;
  }
};
line l[maxn];
int n;
bool flag[maxn];
bool inter(line l1,line l2)  // 判线段交
{
  return
        max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x)&&//快
        max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y)&&//速
        max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x)&&//排
        max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y)&&//斥
        sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s))<=0&&//跨
        sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s))<=0;//立
}
int main()
{
  while(scanf("%d",&n)==1&&n){
    double x1,y1,x2,y2;
    for(int i = 1; i <= n; i++){
      scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
      l[i] = line(point(x1,y1),point(x2,y2));
      flag[i] = true;
    }
    for(int i = 1; i <= n; i++){
      for(int j = i+1; j <= n; j++){
        if(inter(l[i],l[j])){
          flag[i] = false;
          break;
        }
      }
    }
    bool first = true;
    printf("Top sticks: ");
    for(int i = 1; i <= n; i++){
      if(flag[i]){
        if(first){
          printf("%d",i);
          first = false;
        }
        else printf(", %d",i);
      }
    }
    printf(".\n");
  }
  return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值