【POJ 2653】Pick-up sticks 判断线段相交
一定要注意位运算的优先级!!!我被这个卡了好久
判断线段相交模板题。
叉积,点积,规范相交,非规范相交的简单模板
用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据?
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100005
using namespace std;
struct Point {
double x, y;
Point(double _x = 0, double _y = 0) : x(_x), y(_y) {}
};
inline int dcmp(double x) {
return fabs(x) < 1e-6 ? 0 : (x < 0 ? -1 : 1);
}
Point operator - (Point a, Point b) {
return Point(a.x - b.x, a.y - b.y);
}
bool operator == (Point a, Point b) {
return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
double Cross(Point a, Point b) {
return a.x * b.y - a.y * b.x;
}
double Dot(Point a, Point b) {
return a.x * b.x + a.y * b.y;
}
bool jiao(Point d1, Point d2, Point d3, Point d4) {
return (dcmp(Cross(d4 - d3, d1 - d3)) ^ dcmp(Cross(d4 - d3, d2 - d3))) == -2 &&
(dcmp(Cross(d2 - d1, d3 - d1)) ^ dcmp(Cross(d2 - d1, d4 - d1))) == -2;
}
int bjiao(Point d1, Point d2, Point d3) {
if (d1 == d2 || d1 == d3)
return 1;
if (dcmp(Cross(d2 - d1, d3 - d1)) == 0 && dcmp(Dot(d2 - d1, d3 - d1)) == -1)
return 1;
return 0;
}
Point d[N][2];
int n, next[N], ans[N], cnt;
inline bool pd(int now, int up) {
if (bjiao(d[now][0], d[up][0], d[up][1]) ||
bjiao(d[now][1], d[up][0], d[up][1]) ||
bjiao(d[up][0], d[now][0], d[now][1]) ||
bjiao(d[up][1], d[now][0], d[now][1]))
return 1;
return jiao(d[now][0], d[now][1], d[up][0], d[up][1]);
}
inline void mktb(int up) {
for(int now = next[0], pre = 0; now != up; now = next[now]) {
if (pd(now, up))
next[pre] = next[now];
else
pre = now;
}
}
int main() {
scanf("%d", &n);
while (n) {
for(int i = 0; i <= n; ++i)
next[i] = i + 1;
for(int i = 1; i <= n; ++i) {
scanf("%lf%lf%lf%lf", &d[i][0].x, &d[i][0].y, &d[i][1].x, &d[i][1].y);
mktb(i);
}
cnt = 0;
for(int now = next[0]; now != n + 1; now = next[now])
ans[++cnt] = now;
printf("Top sticks:");
for(int i = 1; i < cnt; ++i)
printf("% d,", ans[i]);
printf(" %d.\n", ans[cnt]);
scanf("%d", &n);
}
return 0;
}
【POJ 2653】Pick-up sticks 判断线段相交的更多相关文章
- 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 (判断线段相交)
Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10330 Accepted: 3833 D ...
- POJ 1066 - Treasure Hunt - [枚举+判断线段相交]
题目链接:http://poj.org/problem?id=1066 Time Limit: 1000MS Memory Limit: 10000K Description Archeologist ...
- 【POJ 1556】The Doors 判断线段相交+SPFA
黑书上的一道例题:如果走最短路则会碰到点,除非中间没有障碍. 这样把能一步走到的点两两连边,然后跑SPFA即可. #include<cmath> #include<cstdio> ...
- POJ 2653 Pick-up sticks(判断线段相交)
Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7699 Accepted: 2843 De ...
- POJ 1066--Treasure Hunt(判断线段相交)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7857 Accepted: 3247 Des ...
- POJ_2653_Pick-up sticks_判断线段相交
POJ_2653_Pick-up sticks_判断线段相交 Description Stan has n sticks of various length. He throws them one a ...
- 还记得高中的向量吗?leetcode 335. Self Crossing(判断线段相交)
传统解法 题目来自 leetcode 335. Self Crossing. 题意非常简单,有一个点,一开始位于 (0, 0) 位置,然后有规律地往上,左,下,右方向移动一定的距离,判断是否会相交(s ...
随机推荐
- 2014 UESTC暑前集训动态规划专题解题报告
A.爱管闲事 http://www.cnblogs.com/whatbeg/p/3762733.html B.轻音乐同好会 C.温泉旅馆 http://www.cnblogs.com/whatbeg/ ...
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
- NOIP模拟赛 行走
题目描述 “我有个愿望,我希望走到你身边.” 这是个奇异的世界,世界上的n-1条路联结起来形成一棵树,每条路有一个对应的权值ci. 现在我会给出q组询问或操作. 每次询问我会从一个x点走到y点,初始在 ...
- TestLink测试软件安装条件检查不通过的解决方案
在第一次安装的时候出现这个错误信息 解决办法: 修改config.inc.php文件里的两个属性值为: $tlCfg->log_path = TL_ABS_PATH . 'logs' . DIR ...
- TFS命令tf:undo(强制签入签出文件)
由于修改计算机名称或不同电脑上操作忘记签入,则需要强制签入文件 具体步骤如下: 1.在命令行中输入"cd C:\Program Files\Microsoft Visual Studio ...
- nginx反向代理tomcat访问时浏览器加载失败,出现 ERR_CONTENT_LENGTH_MISMATCH 问题
问题说明:测试机上部署了一套业务环境,nginx反向代理tomcat,在访问时长时间处于加载中,十分缓慢! 通过浏览器调试(F12键->Console),发现有错误ERR_CONTENT_LEN ...
- 【点滴积累,厚积薄发】修改hosts,并刷新dns缓存
Windows系统下hosts位置 C:\Windows\System32\drivers\etc 修改hosts后,要想马上生效,需要运行命令来刷新DNS缓存:ipconfig /flushdns
- c#描述异常处理语句try、catch、finally执行时的相互关系
try里面是执行代码,其中的代码"可能"产生异常. catch是对产生异常后的处理代码,可以抛出异常,也可以显示异常,也可以弹出某中提示,总之catch里是任何代码都行,如果你知道 ...
- python数字图像处理(19):骨架提取与分水岭算法
骨架提取与分水岭算法也属于形态学处理范畴,都放在morphology子模块内. 1.骨架提取 骨架提取,也叫二值图像细化.这种算法能将一个连通区域细化成一个像素的宽度,用于特征提取和目标拓扑表示. m ...
- 通过js动态生成页面表格
var redlineTemplateP = $(".redlineDataList"); for (var index in detailArraryLists.rows){ v ...