一、传送门

http://poj.org/problem?id=2653
https://zoj.pintia.cn/problem-sets/91827364500/problems/91827366050

二、算法分析说明与代码编写指导

overlapped 函数:
返回两个区间是否重叠。若不交,返回-1;只重叠与一点(左端区间的右端点与右端区间的左端点重合),返回0;有重叠,返回1。
cross_product:返回两个二维向量的向量积。二维向量的向量积一般定义成标量:

intersected 函数用快速排斥实验和跨立实验判断两条线段是否相交:
题意:依次往地上扔棍子,问哪些棍子在棍子堆的最顶上。
考察任意两根木棍是否有交点即可。
本题中,无论是否将其中一条线段的交点为端点的情况包括在相交的范畴,都不影响 AC。
但是要注意,外层循环变量是递减的,也就是说先考察后面扔到地上的木棍再考察前面的。原因是:每根木棍只会被后面扔在它上面的木棍压住。如果两层循环的循环变量都是递增的,那么之前判定没有与其它木棍相交的木棍一旦被后来的木棍压住,就无法修正判定结果,导致 WA。当然,由于本题数据量较大,这种出错情况会导致输出的在顶上的木棍数量非常多,从而 TLE。

三、AC 代码

POJ 2653:

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#pragma warning(disable:4996)
using namespace std;
template<class _Ty> struct point { _Ty x, y; };
struct segment { point<double> s, t; };
const unsigned nmax = 100001;
unsigned n; segment s[nmax]; bool top; vector<unsigned> v;
//[a1, b1] ∩ [a2, b2]
template<class _Ty> inline int overlapped(_Ty a1, _Ty b1, _Ty a2, _Ty b2) {if (a1 > b1)swap(a1, b1); if (a2 > b2)swap(a2, b2);_Ty m1 = (a1 + b1) / 2, m2 = (a2 + b2) / 2;if (m1 == m2)return b1 - a1 > 0 && b2 - a2 > 0;if (m1 < m2) {if (b1 < a2)return -1; if (b1 == a2)return 0;return 1;}if (b2 < a1)return -1; if (b2 == a1)return 0;return 1;
}
template<class _Ty> inline _Ty cross_product(const _Ty& x1, const _Ty& y1, const _Ty& x2, const _Ty& y2) {return x1 * y2 - x2 * y1;
}
//if segment ab is intersected with segment cd
template<class _Ty> inline bool intersected(const point<_Ty>& a, const point<_Ty>& b, const point<_Ty>& c, const point<_Ty>& d) {if (overlapped(a.x, b.x, c.x, d.x) != 1)return false;if (overlapped(a.y, b.y, c.y, d.y) != 1)return false;_Ty u1 = b.x - a.x, u2 = b.y - a.y, v1 = d.x - c.x, v2 = d.y - c.y;if (cross_product(u1, u2, c.x - a.x, c.y - a.y) * cross_product(u1, u2, d.x - a.x, d.y - a.y) >= 0)return false;if (cross_product(v1, v2, a.x - c.x, a.y - c.y) * cross_product(v1, v2, b.x - c.x, b.y - c.y) >= 0)return false;return true;
}
int main() {for (;;) {scanf("%u", &n); if (n == 0)return 0;for (unsigned i = 1; i <= n; ++i)scanf("%lf%lf%lf%lf", &s[i].s.x, &s[i].s.y, &s[i].t.x, &s[i].t.y);v.clear(); v.push_back(n);for (unsigned i = n - 1; i; --i) {top = true;for (unsigned j = i + 1; j <= n; ++j) {if (intersected(s[i].s, s[i].t, s[j].s, s[j].t)) { top = false; break; }}if (top)v.push_back(i);}printf("Top sticks: %u", *v.rbegin());for (vector<unsigned>::reverse_iterator i = v.rbegin() + 1; i != v.rend(); ++i)printf(", %u", *i);puts(".");}
}

ZOJ 2551:

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#pragma warning(disable:4996)
using namespace std;
template<class _Ty> struct point { _Ty x, y; };
struct segment { point<double> s, t; };
const unsigned nmax = 100001;
unsigned n; segment s[nmax]; bool top; vector<unsigned> v;
//[a1, b1] ∩ [a2, b2]
template<class _Ty> inline int overlapped(_Ty a1, _Ty b1, _Ty a2, _Ty b2) {if (a1 > b1)swap(a1, b1); if (a2 > b2)swap(a2, b2);_Ty m1 = (a1 + b1) / 2, m2 = (a2 + b2) / 2;if (m1 == m2)return b1 - a1 > 0 && b2 - a2 > 0;if (m1 < m2) {if (b1 < a2)return -1; if (b1 == a2)return 0;return 1;}if (b2 < a1)return -1; if (b2 == a1)return 0;return 1;
}
template<class _Ty> inline _Ty cross_product(const _Ty& x1, const _Ty& y1, const _Ty& x2, const _Ty& y2) {return x1 * y2 - x2 * y1;
}
//if segment ab is intersected with segment cd
template<class _Ty> inline bool intersected(const point<_Ty>& a, const point<_Ty>& b, const point<_Ty>& c, const point<_Ty>& d) {if (overlapped(a.x, b.x, c.x, d.x) != 1)return false;if (overlapped(a.y, b.y, c.y, d.y) != 1)return false;_Ty u1 = b.x - a.x, u2 = b.y - a.y, v1 = d.x - c.x, v2 = d.y - c.y;if (cross_product(u1, u2, c.x - a.x, c.y - a.y) * cross_product(u1, u2, d.x - a.x, d.y - a.y) >= 0)return false;if (cross_product(v1, v2, a.x - c.x, a.y - c.y) * cross_product(v1, v2, b.x - c.x, b.y - c.y) >= 0)return false;return true;
}
int main() {for (;;) {scanf("%u", &n); if (n == 0)return 0;for (unsigned i = 1; i <= n; ++i)scanf("%lf%lf%lf%lf", &s[i].s.x, &s[i].s.y, &s[i].t.x, &s[i].t.y);v.clear(); v.emplace_back(n);for (unsigned i = n - 1; i; --i) {top = true;for (unsigned j = i + 1; j <= n; ++j) {if (intersected(s[i].s, s[i].t, s[j].s, s[j].t)) { top = false; break; }}if (top)v.emplace_back(i);}printf("Top sticks: %u", *v.crbegin());for (auto i = v.crbegin() + 1; i != v.crend(); ++i)printf(", %u", *i);puts(".");}
}

【代码超详解】ZOJ 2551 / POJ 2653 Pick-up Sticks(快速排斥实验 + 跨立实验判断线段是否相交 · 模板)相关推荐

  1. 【代码超详解】LightOJ 1197 Help Hanzo(区间质数筛法)

    一.题目描述 二.算法分析说明与代码编写指导 对于求指定区间 [a, b] 的质数的题目,通常 a 和 b 都比较大,而 b - a 不太大. 采用埃氏筛或者欧拉筛的代码,一般都会同时给出前若干个质数 ...

  2. 【代码超详解】洛谷 P2922 [USACO08DEC]秘密消息Secret Message

    一.题目描述 题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending sec ...

  3. 语言模型_Pytorch_代码超详解

    1. 环境配置 Python 3.7.4 torch 1.5.1 torchtext 0.6.0 torchvision 0.6.1 numpy 1.16.5 2. 学习目标_语言模型 学习目标 学习 ...

  4. 线性规划之单纯形法【超详解+图解】-转载

    线性规划之单纯形法[超详解+图解] 目录 1.作用 2.线性规划的一般形式 5.1几何意义 5.2如何判断最优 5.3如何选择新的基变量 5.4如何选择被替换的基变量 5.5终止条件 标准型: 转化为 ...

  5. Android vector标签 PathData 画图超详解

    此文章来源于https://www.cnblogs.com/yuhanghzsd/p/5466846.html点击打开链接 Android vector标签 PathData 画图超详解 SVG是一种 ...

  6. python控制手机模拟器_Appium+python自动化之连接模拟器并启动淘宝APP(超详解)...

    简介 上一篇讲解完模拟器的安装.配置好以后,就好比我们手机已经买好,并且系统已经做好了,就差我们用数据线和电脑连接开始实战了,这篇宏哥就带着小伙伴们和童鞋们趁热打铁,讲解和分享一下如何连接模拟器(电脑 ...

  7. x264 代码重点详解 详细分析

    eg mplayer x264 代码重点详解 详细分析 分类: ffmpeg 2012-02-06 09:19 4229人阅读 评论(1) 收藏 举报 h.264codecflv优化initializ ...

  8. 强化学习教程(四):从PDG到DDPG的原理及tf代码实现详解

    强化学习教程(四):从PDG到DDPG的原理及tf代码实现详解 原创 lrhao 公众号:ChallengeHub 收录于话题 #强化学习教程 前言 在前面强化学习教程(三)中介绍了基于策略「PG」算 ...

  9. C/C++实现蛇形矩阵(超详解)【沈七】

    C/C++实现蛇形矩阵(超详解) 题目链接 题目描述 输入样例 题解部分 完整代码 完结散花 悄悄告诉你: 参考文章 萌新报道! 唤我沈七就行嘿嘿. 大一软件工程在读. 菜鸡蒟蒻想在博客中记录一些算法 ...

最新文章

  1. linux中快速查找文件
  2. HihoCoder 1671 DFS
  3. MySQL的timestamp类型存储时间范围为什么是2038年之内
  4. php7 imagick安装,php扩展imagick安装for windows7
  5. mysql group by自定义_mysql – GROUP BY和自定义顺序
  6. react入门jsx
  7. hdu 3123(GCC)数论
  8. Sentinel热点Key降级上_分布式系统集群限流_线程数隔离_削峰填谷_流量控制_速率控制_服务熔断_服务降级---微服务升级_SpringCloud Alibaba工作笔记0042
  9. tpadmin隐藏index.php,百度云服务器tp5框架布署,隐藏路径中的index.php
  10. halcon 1维测量
  11. springboot证书管理系统的设计与实现毕业设计源码162317
  12. FileInputStream.read()返回int类型原因
  13. React从零到一Demo演练(上)
  14. 云呐|固定资产调拨流程(固定资产调拨需要哪些流程)
  15. 文本框的左视图不见了?
  16. 2018.6清北学堂day6考试
  17. PhpSpreadsheet常用操作
  18. 如何使用mysql数据库做网站_php小型数据库(不用mysql做网站)
  19. 数据库oracle--PL/SQL的使用-如何按f8只执行一行
  20. limit和offset用法

热门文章

  1. JS Notifications消息通知
  2. There are no devices registered in your account on the developer website. Plug in and select a devic
  3. 我对生活的理解,人生的感悟
  4. 现在流的泪,都是当年犯滴错
  5. linux bugfree安装教程,Linux系统下安装bugfree操作方法
  6. 【随记】Matlab画四象限图形
  7. Linux学习整理-用户与组相关的命令-who,whoami,w,id,logname
  8. [dp][lis] Jzoj P5920 风筝
  9. java的ElementById的意思_是getelementbyid意思
  10. enumerate函数