【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 ...
随机推荐
- VS Code
VS Code VS Code(Visual Studio Code)是由微软研发的一款免费.开源的跨平台文本(代码)编辑器.几乎完美的编辑器. 官网:https://code.visualstudi ...
- AppStore ipa (苹果内购)笔记
内购示意图 准备条件 苹果的开发者证书,已经为应用启用App内购,并在Xcode更新配置文件 itunes store设置 itunes中创建App及其它设置 参考:iOS应用程序内购/内付费(一) ...
- QuickFIX/N 动态存储配置信息
Acceptor或者Initiator能够为您维护尽可能多的FIX会话,因而FIX会话标识的唯一性非常重要.在QuickFIX/N中,一个FIX会话的唯一标识是由:BeginString(FIX版本号 ...
- 一个App需要的东西
1.短信申请平台 (发送验证码需要的短信) http://www.yuntongxun.com/api/sms?nl=sy_cp 容联云通讯
- nginx访问白名单设置以及根据$remote_addr分发
在日常运维工作中,会碰到这样的需求:设置nginx的某个域名访问只对某些ip开放,其他ip的客户端都不能访问.达到这样的目的一般有下面两种设置方法:(1)针对nginx域名配置所启用的端口(一般是80 ...
- zkw费用流+当前弧优化
zkw费用流+当前弧优化 var o,v:..] of boolean; f,s,d,dis:..] of longint; next,p,c,w:..] of longint; i,j,k,l,y, ...
- MYSQL临时表创建索引
DROP TEMPORARY TABLE IF EXISTS tmp_record_t2;CREATE TEMPORARY TABLE tmp_record_t2(consumption_id INT ...
- PHP PSR规范
PHP PSR-1 基本代码规范(中文版) http://segmentfault.com/a/1190000002521577PHP PSR-2 代码风格规范 (中文版) http://segm ...
- WPF:自动执行"机器人"程序若干注意事项
企业应用中,经常会遇到一些需要定时自动执行的程序来完成某些功能,比如:自动定时从第三方web service取回数据.定时对历史数据进行清理.定时向ftp上传业务数据... 这类程序,我习惯称为“机器 ...
- 什么是API
我们从API的功能.分类.设计.实现.用户来看什么是API. API是应用程序组件之间通信的接口 --wiki:Application Programming Interface In compute ...