欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

bzoj 1007 : [HNOI2008]水平可见直线 计算几何

发布时间:2025/4/14 编程问答 64 豆豆
生活随笔 收集整理的这篇文章主要介绍了 bzoj 1007 : [HNOI2008]水平可见直线 计算几何 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题目链接

给出n条直线, 问从y轴上方向下看, 能看到哪些直线, 输出这些直线的编号。

 

首先我们按斜率排序, 然后依次加入一个栈里面, 如果刚加入的直线, 和之前的那条直线斜率相等, 那么显然之前的会被覆盖。

假设栈顶直线为st[top], 新加入的直线为tmp, 那么如果tmp和st[top-1]这条直线的交点在st[top]和st[top-1]交点的左边, 那么显然st[top]这条直线会被覆盖。

 

#include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set> #include <string> #include <queue> #include <stack> #include <bitset> using namespace std; #define pb(x) push_back(x) #define ll long long #define mk(x, y) make_pair(x, y) #define lson l, m, rt<<1 #define mem(a) memset(a, 0, sizeof(a)) #define rson m+1, r, rt<<1|1 #define mem1(a) memset(a, -1, sizeof(a)) #define mem2(a) memset(a, 0x3f, sizeof(a)) #define rep(i, n, a) for(int i = a; i<n; i++) #define fi first #define se second typedef pair<int, int> pll; const double PI = acos(-1.0); const double eps = 1e-8; const int mod = 1e9+7; const int inf = 1061109567; const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; struct node {double x, y;int id;bool operator < (node a) const{if(fabs(x-a.x)<eps)return y<a.y;return x<a.x;} }a[50005], st[50005]; int top, ans[50005]; double cross(node a, node b) {return (b.y-a.y)/(a.x-b.x); } void add(node tmp) {while(top) {if(fabs(tmp.x-st[top].x)<eps)top--;else if(top>1 && cross(tmp, st[top-1])<=cross(st[top], st[top-1]))top--;elsebreak;}st[++top] = tmp; } int main() {int n;cin>>n;for(int i = 0; i<n; i++) {scanf("%lf%lf", &a[i].x, &a[i].y);a[i].id = i;}sort(a, a+n);for(int i = 0; i<n; i++) {add(a[i]);}for(int i = 1; i<=top; i++) {ans[st[i].id] = 1;}for(int i = 0; i<n; i++)if(ans[i])printf("%d ", i+1);cout<<endl;return 0; }

 

转载于:https://www.cnblogs.com/yohaha/p/5221079.html

《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读

总结

以上是生活随笔为你收集整理的bzoj 1007 : [HNOI2008]水平可见直线 计算几何的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。