hdu 1147 Pick-up sticks

本文介绍了一个经典的几何问题——拾棍问题。通过分析线段之间的覆盖关系,找出最终位于顶部未被其它线段覆盖的所有线段。文章详细描述了算法思路与优化方案,并提供了一段完整的C++实现代码。

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

题目:

Pick-up sticks

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

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, t

hat 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

Sample Output

Top sticks: 2, 4, 5.

Top sticks: 1, 2, 3.

题目大意:

给n给线段,后面给的线段会压到前面放的线段,求最后放完所有线段在最上面未被压到的线段

题目思路:

1、每次去两个线段判断是否被相交,相交将前面的线段标记删除

2、用向量的

题目优化

1、如果简单的枚举可能会超时,所以需采用优化

2、被删除标记的就可以不再被查询

3、减少判断的次数,所以应从前往后判断 ,判断成立就退出

For(i=0...n-1)                      For(i=0...n-1)

for(j=i+1...n) for(j=i-1...0)

成立break   if(v[j])

  如果采用后面的 那么我们必然的多次的判断它是否被删除而空运行

如 号 被 号 压住 那么第一种只用3,:1 2, 1 3 , 1 4

而第二种   :1 2 , 1 3 , 1 4 , 1 5 , ......都要判断

程序:

 

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const double range=0.001;//误差
bool v[100000];
struct node//点
{
    double x,y;
};
struct wire//线
{
    node p;
    node q;
    friend bool operator < (wire wx,wire wy)
    {
        return wx.p.x>wy.p.x;
    }
} a[100010];
int solve(int m,int n);
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        memset(v,0,sizeof(v));
        for(int i=0; i<n; i++)
            scanf("%lf %lf %lf %lf",&a[i].p.x,&a[i].p.y,&a[i].q.x,&a[i].q.y);
        for(int i=0; i<n; i++)
            for(int j=i+1; j<n; j++)//注意顺序
                if(!v[j])
                    if(solve(i,j))
                    {
                    v[i]=1;
                    break;
                    }

        printf("Top sticks:");
        for(int i=0,j=0; i<n; i++)
            if(!v[i])
                printf("%s %d",(j++?",":""),i+1);
        printf(".\n");
    }
    return 0;
}
int solve(int m,int n)
{
    node *p1=&a[m].p,*p2=&a[m].q,*q1=&a[n].p,*q2=&a[n].q;
    if( min(p1->x,p2->x) <= max(q1->x,q2->x)+range &&
            min(q1->x,q2->x) <= max(p1->x,p2->x)+range &&
            min(p1->y,p2->y) <= max(q1->y,q2->y)+range &&
            min(q1->y,q2->y) <= max(p1->y,p2->y)+range
      )//排斥试验
        if(
            ((q1->x-p1->x)*(q1->y-q2->y)-(q1->y-p1->y)*( q1->x-q2->x)) * ((q1->x-p2->x)*(q1->y-q2->y)-(q1->y-p2->y)*(q1->x-q2->x)) <= range &&
            ((p1->x-q1->x)*(p1->y-p2->y)-(p1->y-q1->y)*(p1->x-p2->x)) * ((p1->x-q2->x)*(p1->y-p2->y)-(p1->y-q2->y)*( p1->x-p2->x)) <= range
        )//跨立实验
            return true;
    return false;
}

 

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值