zoukankan      html  css  js  c++  java
  • bzoj1007-水平可见直线

    题目

    在平面直角坐标系上以(y=kx+b)的形式给出(n (nle 50000))条直线,求从无限高的地方能看到多少条直线。

    分析

    举几个例子发现我们要求的直线组成一个下凸的形状。所以我们只要找出直线围成的下凸包即可。
    对直线排序,(k)从小到大,(b)从大到小,用一个栈维护一下。如果当前元素与栈顶元素的交点在栈顶元素与栈中第二个元素的交点的左边,那么弹出栈顶(模拟一下就知道了)。

    代码

    计算几何尽量避免除法,因为会有精度问题,一般移项转化成乘法计算。

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxn=5e4+10;
    struct line {
    	double k,b;
    	int id;
    	bool operator < (const line a) const {
    		return k==a.k?b>a.b:k<a.k;	
    	}
    } a[maxn],sta[maxn];
    int top=0;
    bool bid(line a,line b) {
    	return a.id<b.id;
    }
    bool ans[maxn];
    int main() {
    	#ifndef ONLINE_JUDGE
    		freopen("test.in","r",stdin);
    	#endif
    	int n;
    	scanf("%d",&n);
    	for (int i=1;i<=n;++i) scanf("%lf%lf",&a[i].k,&a[i].b),a[i].id=i;
    	sort(a+1,a+n+1);
    	for (int i=1;i<=n;++i) {
    		while (top>1) if ((a[i].b-sta[top].b)*(sta[top-1].k-sta[top].k)<=(sta[top].b-sta[top-1].b)*(sta[top].k-a[i].k)) --top; else break;
    		sta[++top]=a[i];
    	}
    	for (int i=1;i<=top;++i) ans[sta[i].id]=true;
    	for (int i=1;i<maxn;++i) if (ans[i]) printf("%d ",i);
    	puts("");
    }
    
  • 相关阅读:
    4.PHP正则表达式与数组
    3.PHP条件语句及其字符串相关函数
    3.PHP条件语句及其字符串相关函数
    2.PHP语言基础
    2.PHP语言基础
    1.简单认识PHP和环境搭建
    1.简单认识PHP和环境搭建
    Windows PE 第十章 加载配置信息
    #Leetcode# 20.Valid Parentheses
    #Leetcode# 14. Longest Common Prefix
  • 原文地址:https://www.cnblogs.com/owenyu/p/6724551.html
Copyright © 2011-2022 走看看