首页 技术 正文
技术 2022年11月12日
0 收藏 704 点赞 2,789 浏览 2564 个字

题目链接:http://poj.org/problem?id=2653

Time Limit: 3000MS Memory Limit: 65536K

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.

POJ 2653 – Pick-up sticks – [枚举+判断线段相交]

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.

Hint

Huge input,scanf is recommended.

题意:

给出n根细木棍的坐标,每次按顺序放到指定坐标位置,这样会导致一些后放的木棍压倒前面的木棍;

求最后那些木棍没有被压住。

题解:

刚开始我是每输入第i跟木棍,就遍历1 ~ i-1的木棍,看看他们有没有被压住,但这样最后TLE了;

看Dis里说,先全部输入,然后枚举,对第i根木棍,遍历i+1 ~ n的木棍,一旦出现压住i的,就标记并且跳出;

明明感觉这样不T很不科学,但就是AC了……奇怪……

AC代码:

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;const double eps = 1e-;struct Point{
double x,y;
Point(double tx=,double ty=):x(tx),y(ty){}
};
typedef Point Vctor;//向量的加减乘除
Vctor operator + (Vctor A,Vctor B){return Vctor(A.x+B.x,A.y+B.y);}
Vctor operator - (Point A,Point B){return Vctor(A.x-B.x,A.y-B.y);}
Vctor operator * (Vctor A,double p){return Vctor(A.x*p,A.y*p);}
Vctor operator / (Vctor A,double p){return Vctor(A.x/p,A.y/p);}int dcmp(double x)
{
if(fabs(x)<eps) return ;
else return (x<)?(-):();
}
bool operator == (Point A,Point B){return dcmp(A.x-B.x)== && dcmp(A.y-B.y)==;}double Cross(Vctor A,Vctor B){return A.x*B.y-A.y*B.x;}//判断线段是否规范相交
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
double c1 = Cross(a2 - a1,b1 - a1), c2 = Cross(a2 - a1,b2 - a1),
c3 = Cross(b2 - b1,a1 - b1), c4 = Cross(b2 - b1,a2 - b1);
return dcmp(c1)*dcmp(c2)< && dcmp(c3)*dcmp(c4)<;
}int n;
struct Seg{
Point a,b;
bool pressed;
}seg[];
int main()
{
while(scanf("%d",&n) && n!=)
{
for(int i=;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&seg[i].a.x,&seg[i].a.y,&seg[i].b.x,&seg[i].b.y);
seg[i].pressed=;
} for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
if(SegmentProperIntersection(seg[i].a,seg[i].b,seg[j].a,seg[j].b))
{
seg[i].pressed=;
break;
}
}
} printf("Top sticks: ");
for(int i=,cnt=;i<=n;i++)
{
if(seg[i].pressed) continue;
if(cnt!=) printf(", ");
printf("%d",i);
cnt++;
}
printf(".\n");
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,051
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,533
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,384
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,160
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,795
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,875