POJ 2653 - Pick-up sticks - [枚举+判断线段相交]
题目链接:http://poj.org/problem?id=2653
Time Limit: 3000MS Memory Limit: 65536K
Description
Input
Output
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.
Hint
题意:
给出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");
}
}
POJ 2653 - Pick-up sticks - [枚举+判断线段相交]的更多相关文章
- POJ 2653 Pick-up sticks(判断线段相交)
Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7699 Accepted: 2843 De ...
- POJ 1066 - Treasure Hunt - [枚举+判断线段相交]
题目链接:http://poj.org/problem?id=1066 Time Limit: 1000MS Memory Limit: 10000K Description Archeologist ...
- POJ 3304 Segments (叉乘判断线段相交)
<题目链接> 题目大意: 给出一些线段,判断是存在直线,使得该直线能够经过所有的线段.. 解题思路: 如果有存在这样的直线,过投影相交区域作直线的垂线,该垂线必定与每条线段相交,问题转化为 ...
- 【POJ 2653】Pick-up sticks 判断线段相交
一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...
- POJ 1066--Treasure Hunt(判断线段相交)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7857 Accepted: 3247 Des ...
- POJ2653 Pick-up sticks 判断线段相交
POJ2653 判断线段相交的方法 先判断直线是否相交 再判断点是否在线段上 复杂度是常数的 题目保证最后答案小于1000 故从后往前尝试用后面的线段 "压"前面的线段 排除不可能 ...
- POJ 2826 An Easy Problem? 判断线段相交
POJ 2826 An Easy Problem?! -- 思路来自kuangbin博客 下面三种情况比较特殊,特别是第三种 G++怎么交都是WA,同样的代码C++A了 #include <io ...
- POJ_2653_Pick-up sticks_判断线段相交
POJ_2653_Pick-up sticks_判断线段相交 Description Stan has n sticks of various length. He throws them one a ...
- POJ_1066_Treasure Hunt_判断线段相交
POJ_1066_Treasure Hunt_判断线段相交 Description Archeologists from the Antiquities and Curios Museum (ACM) ...
随机推荐
- instsrv.exe srvany.exe启动服务
1.通过注册表注册服务 private static readonly string regpath = @"SYSTEM\CurrentControlSet\Services\Consul ...
- 谈谈入门iOS的经验吧
前言 近期忙完项目比較闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助.博主非科班出身,一些计算机术语上的不专业欢迎大家指正. 我是学微电子的.大四的时候找了一家深圳的专业对口的 ...
- VMware Playerでの仮想マシン起動エラー
Windows Updateすると.翌日VMware Playerの仮想マシン起動時に 「この仮想マシンを構成済み設定でパワーオンするのに十分な物理メモリがありません.」 のエラーとなることが時々あり ...
- [AX2012]代码更改默认财务维度
在前文(http://www.cnblogs.com/duanshuiliu/p/3243048.html)最后演示了如何使用代码更改默认财务维度,那段代码模拟了创建各数据表记录的过程,实际上AX提供 ...
- LHC大神问的矩阵转置问题
数学中线性代数中提到的矩阵转置,其实在我们的业务场景中也有需要的地方,比如LHC大神问到的这个问题 那么如何进行行列转换呢? 代码如下: <?php $array=array( '部门1'=&g ...
- My Apple Developer Library Catalog
Objective-C & Memory Management:Programming with Objective-CConcepts in Objective-C ProgrammingM ...
- 【安全开发】Android安全编码规范
申明:本文非笔者原创,原文转载自:https://github.com/SecurityPaper/SecurityPaper-web/blob/master/_posts/2.SDL%E8%A7%8 ...
- Synchronizing Threads and GUI in Delphi application
Synchronizing Threads and GUI See More About delphi multithreading tthread class user interface de ...
- DexArchiveBuilderException
出现这个问题大概是因为版本资源问题 比如把TextView 改为CompatTextView 解决方法一: 在项目的build.gradle文件中查看自己导入的依赖,看看是否有重复的,如果有的话删除 ...
- N76E003系统时钟
系统时钟源N76E003共有3种系统时钟源,包括: 内部高速/低速振荡器.外部输入时钟.它们每一个都可以作为N76E003的系统时钟源.开启不同的时钟源可能会影响到多功能引脚P3.0/XIN .内部振 ...