一定要注意位运算的优先级!!!我被这个卡了好久

判断线段相交模板题。

叉积,点积,规范相交,非规范相交的简单模板

用了“链表”优化之后还是$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 判断线段相交的更多相关文章

  1. POJ2653 Pick-up sticks 判断线段相交

    POJ2653 判断线段相交的方法 先判断直线是否相交 再判断点是否在线段上 复杂度是常数的 题目保证最后答案小于1000 故从后往前尝试用后面的线段 "压"前面的线段 排除不可能 ...

  2. POJ 2826 An Easy Problem? 判断线段相交

    POJ 2826 An Easy Problem?! -- 思路来自kuangbin博客 下面三种情况比较特殊,特别是第三种 G++怎么交都是WA,同样的代码C++A了 #include <io ...

  3. POJ 2653 Pick-up sticks (判断线段相交)

    Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10330   Accepted: 3833 D ...

  4. POJ 1066 - Treasure Hunt - [枚举+判断线段相交]

    题目链接:http://poj.org/problem?id=1066 Time Limit: 1000MS Memory Limit: 10000K Description Archeologist ...

  5. 【POJ 1556】The Doors 判断线段相交+SPFA

    黑书上的一道例题:如果走最短路则会碰到点,除非中间没有障碍. 这样把能一步走到的点两两连边,然后跑SPFA即可. #include<cmath> #include<cstdio> ...

  6. POJ 2653 Pick-up sticks(判断线段相交)

    Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 7699   Accepted: 2843 De ...

  7. POJ 1066--Treasure Hunt(判断线段相交)

    Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7857   Accepted: 3247 Des ...

  8. POJ_2653_Pick-up sticks_判断线段相交

    POJ_2653_Pick-up sticks_判断线段相交 Description Stan has n sticks of various length. He throws them one a ...

  9. 还记得高中的向量吗?leetcode 335. Self Crossing(判断线段相交)

    传统解法 题目来自 leetcode 335. Self Crossing. 题意非常简单,有一个点,一开始位于 (0, 0) 位置,然后有规律地往上,左,下,右方向移动一定的距离,判断是否会相交(s ...

随机推荐

  1. 在Docker中运行web应用

    启动一个简单的web 应用 使用社区提供的模板,启动一个简单的web应用,熟悉下各种Docker命令的使用: # docker run -d -P training/webapp python app ...

  2. Thread 同步线程(打印机同步)

    1.首先创建一个打印机对象 package cn.b.happy; public class Printer { Object o =new Object(); public void print() ...

  3. Entityframework core 动态添加模型实体

    重新DBContext中OnModelCreating protected override void OnModelCreating(ModelBuilder modelBuilder)  { // ...

  4. 树莓派及其他硬件平台国内外Linux镜像站全汇总

    转载至segmentfault. 本文收集整理国内对于Raspberry Pi.CubieBoard.pcDuino等各种Linux开源硬件平台,所使用的Raspbian.Arch Linux ARM ...

  5. text

    链接: 初识 TextKit 如何实现自己没实现过的需求之文本动画

  6. 安装ESXi5.5遇到Relocating modules and starting up the kernel的处理

    在一些Dell较旧的服务器上安装ESXi 5.x时, 会遇到卡在Relocating modules and starting up the kernel过不去的问题. 比如我装的这台CS24VSS. ...

  7. 【MVC】AJAX+PartialView实现商城首页的楼层加载

    使用AJAX实现楼层加载的例子已经非常多,但是html代码大都是手动拼接的,编写不便,而且难以维护. 下面就使用AJAX+PartialView来实现 1.html代码 <!--楼层1开始--& ...

  8. 解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题

    下面两种现象,用同一种方法解决 1.解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题 2.突然有一天首页访问图片很慢,至少隔20多秒所有图片才会出来.(解析:app使 ...

  9. ASP.NET整理:Cookie,Application,Session,页面生命周期

    一.设置Cookie的2种方式 1.    Repsonse.Cookie[“名”] = 值; 2.    HttpCookie hcCookie = new HttpCookie(“名”,值); h ...

  10. ssh生成key不交互

    ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ""   首次执行不交互 第二次再次执行会让输入y