• 【洛谷 P3194】 [HNOI2008]水平可见直线 (单调栈)


    题目链接
    把线段以斜率为第一关键字,截距为第二关键字升序排序。
    然后维护一个单调栈,保证栈中两两线段的交点的(x)坐标单调上升就行了。栈中的线段即为所求。

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    const int MAXN = 50010;
    struct Seg{
    	double k, b;
    	int id;
    	int operator < (const Seg A) const{
    		return k == A.k ? b < A.b : k < A.k;
    	}
    }s[MAXN];
    int n;
    int st[MAXN], top;
    inline int cmp(int a, int b){
    	return s[a].id < s[b].id;
    }
    double calc(int a, int b){
    	return (s[b].b - s[a].b) / (s[a].k - s[b].k);
    }
    int main(){
    	scanf("%d", &n);
    	for(int i = 1; i <= n; ++i){
    	   scanf("%lf%lf", &s[i].k, &s[i].b);
    	   s[i].id = i;
        }
    	sort(s + 1, s + n + 1);
    	st[++top] = 1;
    	for(int i = 2; i <= n; ++i){
    		while(top && s[i].k == s[st[top]].k || top > 1 && calc(i, st[top - 1]) <= calc(st[top], st[top - 1])) --top;
    		st[++top] = i;
    	}
    	sort(st + 1, st + top + 1, cmp);
    	for(int i = 1; i <= top; ++i)
    	   printf("%d ", s[st[i]].id);
    	return 0;
    }
    
  • 相关阅读:
    Ghost博客安装
    PHP变量作用域
    ssh文件传输命令:sz与rz命令
    excel怎么固定第一行
    memcache和redis区别
    Memcache分布式部署方案
    Memcache服务器端参数说明
    Memcache基础教程
    在Windows下安装Memcached
    MySQL体系结构和存储引擎概述
  • 原文地址:https://www.cnblogs.com/Qihoo360/p/10322557.html
Copyright © 2020-2023  润新知