题目链接https://vjudge.net/problem/UVA-13024

题意:先给出\(L\)个点构造一个凸包,再给出\(S\)个点,询问有几个点在凸包内。

题解:判断点是否在凸包内的模板题。最暴力的想法是\(o(n^2)\)枚举每个点,但实际上我们可以使用二分优化。具体操作就是以凸包最左侧点为起点,对每个点按斜率排序,然后把凸包分割成数个三角形,在这些三角形中二分查找斜率。复杂度便优化为\(o(nlogn)\)如下图。

  • 在凸包\(ABCDEFG\)中可二分查找\(H\),\(I\),\(J\),\(K\)点

完成二分后,通过叉积判断点是否在凸包内,如下图。

  • \(\vec{FH}\times{\vec {HE}} < 0\),\(\vec{FK}\times{\vec {KE}} > 0\)

即叉积<0则在凸包内,>0在凸包外,同时还有叉积=0的情况(点在凸包的边上)。但是叉积=0时还应考虑一种情况,即在凸包一条边的延长线上,需要特判。如下图。

  • 如图所示的\(L\)点和\(M\)点叉积均为0。

AC代码

#include <bits/stdc++.h>
#define SIZE 100007
#define rep(i, a, b) for(int i = a; i <= b; ++i)
using namespace std;
typedef long long ll;
void io() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
ll n, m, t, num;
struct Point {
ll x, y;
double k;
friend bool operator<(const Point& a, const Point& b) { //为使用lower_bound进行运算符重载
return a.k <= b.k;
}
};
Point p[SIZE], ch[SIZE], tp[SIZE];
bool cmp(Point a, Point b) { //andrew算法排序预处理函数
if (a.x == b.x) return a.y < b.y;
else return a.x < b.x;
}
ll cross(Point a, Point b, Point c) { return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); }
ll crossx(Point a, Point b, Point c) { return (a.x - b.x) * (c.y - a.y) - (a.y - b.y) * (c.x - a.x); }
ll andrew() { //采用安德鲁算法求凸包,返回顶点数
sort(p + 1, p + n + 1, cmp);
ll top = 0;
for (int i = 1; i <= n; ++i) {
while ((top > 1) && (cross(ch[top - 1], ch[top], p[i]) <= 0)) --top;
ch[++top] = p[i];
}
ll tmp = top;
for (int i = n - 1; i; --i) {
while ((top > tmp) && (cross(ch[top - 1], ch[top], p[i]) <= 0)) --top;
ch[++top] = p[i];
}
if (n > 1) top--;
return top;
}
bool InConvexHull(ll top, Point x) { //判断点x是否在凸包内
ll pos = lower_bound(ch + 1, ch + top + 1, x) - ch - 1; //二分查找
if (pos == 1) return false;
ll j = crossx(x, ch[pos], ch[pos + 1]);
if (j < 0) return true;
else if (j == 0) { //叉积为0时特判
ll minx = min(ch[pos].x, ch[pos + 1].x), maxx = max(ch[pos].x, ch[pos + 1].x);
ll miny = min(ch[pos].y, ch[pos + 1].y), maxy = max(ch[pos].y, ch[pos + 1].y);
if ((minx <= x.x) && (maxx >= x.x) && (miny <= x.y) && (maxy >= x.y)) return true;
else return false;
}
else return false;
}
int main() {
io();
while (cin >> n && n) { //多组输入
num = 0;
rep(i, 1, n) cin >> p[i].x >> p[i].y;
cin >> m;
ll top = andrew();
ll x = ch[1].x, y = ch[1].y;
rep(i, 2, top) { //计算斜率
double tx = ch[i].x - x, ty = ch[i].y - y;
if (!tx) { //若x坐标相同,则斜率设为1e18
if (ty < 0) ch[i].k = -1e18;
else ch[i].k = 1e18;
}
else ch[i].k = 1.0 * ty / tx;
}
ch[1].k = -1e18;
rep(i, 1, m) {
cin >> tp[i].x >> tp[i].y;
if (ch[1].x > tp[i].x) continue;
ll tx = tp[i].x - x, ty = tp[i].y - y;
Point x;
if (!tx) {
if (ty < 0) x.k = -1e18;
else x.k = 1e18;
}
else x.k = 1.0 * ty / tx;
x.x = tp[i].x, x.y = tp[i].y;
if (InConvexHull(top, x)) ++num; //计数
}
cout << num << endl;
}
}

最新文章

  1. AC日记——密码翻译 openjudge 1.7 09
  2. easyUI框架之学习1--框架
  3. 21个常用的PHP代码汇总
  4. POJ 2185 Milking Grid KMP(矩阵循环节)
  5. 无责任Windows Azure SDK .NET开发入门篇一[Windows Azure开发前准备工作]
  6. C#下多进程共同读写同一文件
  7. javascript 之 this 用法
  8. CSS 设计彻底研究(五)文字与图像
  9. android加载更多的图片
  10. Mysql开启远程
  11. javascript this指针指向?
  12. 转载 Deep learning:五(regularized线性回归练习)
  13. 微信小程序入门之构建一个简单TODOS应用
  14. Swift基础之UITableView(之前写的知识点都是最新的2.2版本样式,欢迎大家参考,可以相互交流)
  15. PAT1070:Mooncake
  16. 工作VUE布局记录
  17. 【笔记】.NET开发环境下使用PostgreSQL+Oracle_fdw 实现两个数据库之间数据交互操作(二)
  18. 十二省联考 - JLOI2019 游记
  19. http与rfc
  20. leetcode题解 1.TwoSum

热门文章

  1. csrf跨站点请求伪造
  2. SQLServer使用链接服务器远程查询
  3. S3C2440之存储控制器学习记录
  4. JQuery/JS插件 jsTree加载树,普通加载,点一级加载一级
  5. 数位dp(Balanced Numbers )
  6. 创建集群corosync
  7. Go_select
  8. try catch 语句中有return 的各类情况
  9. 三分钟快速上手TensorFlow 2.0 (上)——前置基础、模型建立与可视化
  10. 如何获取object数据的描述符