HDU1147:Pick-up sticks(线段相交)

本文介绍了一个关于筷子放置的问题,即如何找出放置在平面上的所有筷子中处于最顶部的筷子。通过线段相交判断的方法,文章提供了一种有效的算法解决方案,并附带了完整的源代码实现。

Pick-up sticks

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


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.
 

Source
题意:给n跟筷子,按顺序放到地上,输出最后在最上面的筷子编号。

思路:每根筷子做一次线段相交判断即可。

# include <iostream>
# include <cmath>
# include <cstdio>
# include <cstring>
# include <algorithm>
using namespace std;
const int maxn = 1e5;
struct node
{
    double x, y;
}a[maxn+3], b[maxn+3];
int n, m, c[maxn+3];

double cha(node a, node b, node c)//向量叉乘。
{
    return a.x*b.y + b.x*c.y + c.x*a.y - a.x*c.y - c.x*b.y - b.x*a.y;
}

bool fun(node a, node b, node c, node d)
{
    if((max(a.x, b.x)>=min(c.x, d.x) || min(a.x, b.x)<=max(c.x, d.x)) &&
       (max(a.y, b.y)>=min(c.y, d.y) || min(a.y, b.y)<=max(c.y, d.y)))//快速排斥。
    {
        if(cha(a, b, c)*cha(a, b, d) <= 0 && cha(c, d, a)*cha(c, d, b)<=0)
            return true;
        else
            return false;
    }
    else
        return false;
}

int main()
{
    while(~scanf("%d",&n),n)
    {
        m = n;
        memset(c, 0, sizeof(c));
        for(int i=1; i<=n; ++i)
            scanf("%lf%lf%lf%lf",&a[i].x,&a[i].y,&b[i].x,&b[i].y);
        for(int i=1; i<=n; ++i)
            for(int j=i+1; j<=n; ++j)
                if(fun(a[i], b[i], a[j], b[j]))
                {
                    c[i] = 1;
                    --m;
                    break;
                }
        printf("Top sticks: ");
        for(int i=1; i<=n; ++i)
            if(!c[i]&&m)
            {
                --m;
                printf("%d%c%c",i,!m?'.':',', !m?'\n':' ');
            }
    }
    return 0;
}



源码地址: https://pan.quark.cn/s/3916362e5d0a 在C#编程平台下,构建一个曲线编辑器是一项融合了图形用户界面(GUI)构建、数据管理及数学运算的应用开发任务。 接下来将系统性地介绍这个曲线编辑器开发过程中的核心知识点:1. **定制曲线面板展示数据曲线**: - 控件选用:在C#的Windows Forms或WPF框架中,有多种控件可用于曲线呈现,例如PictureBox或用户自定义的UserControl。 通过处理重绘事件,借助Graphics对象执行绘图动作,如运用DrawCurve方法。 - 数据图形化:通过线性或贝塞尔曲线连接数据点,以呈现数据演变态势。 这要求掌握直线与曲线的数学描述,例如两点间的直线公式、三次贝塞尔曲线等。 - 坐标系统与缩放比例:构建X轴和Y轴,设定坐标标记,并开发缩放功能,使用户可察看不同区间内的数据。 2. **在时间轴上配置多个关键帧数据**: - 时间轴构建:开发一个时间轴组件,显示时间单位刻度,并允许用户在特定时间点设置关键帧。 时间可表现为连续形式或离散形式,关键帧对应于时间轴上的标识。 - 关键帧维护:利用数据结构(例如List或Dictionary)保存关键帧,涵盖时间戳和关联值。 需考虑关键帧的添加、移除及调整位置功能。 3. **调整关键帧数据,通过插值方法获得曲线**: - 插值方法:依据关键帧信息,选用插值方法(如线性插值、样条插值,特别是Catmull-Rom样条)生成平滑曲线。 这涉及数学运算,确保曲线在关键帧之间无缝衔接。 - 即时反馈:在编辑关键帧时,即时刷新曲线显示,优化用户体验。 4. **曲线数据的输出**: - 文件类型:挑选适宜的文件格式存储数据,例如XML、JSON或...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值