BZOJ 1007 [HNOI2008]水平可见直线 (栈)

1007: [HNOI2008]水平可见直线

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 7940  Solved: 3030
[Submit][Status][Discuss]

Description

  在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为
可见的,否则Li为被覆盖的.
例如,对于直线:
L1:y=x; L2:y=-x; L3:y=0
则L1和L2是可见的,L3是被覆盖的.
给出n条直线,表示成y=Ax+B的形式(|A|,|B|<=500000),且n条直线两两不重合.求出所有可见的直线.

Input

  第一行为N(0 < N < 50000),接下来的N行输入Ai,Bi

Output

  从小到大输出可见直线的编号,两两中间用空格隔开,最后一个数字后面也必须有个空格

Sample Input

3
-1 0
1 0
0 0

Sample Output

1 2

HINT

析:先把所有的直线按斜率从小到大,按截距从大到小排序,然后维护一个栈,如果一个栈里只有一个元素,并且下一条线斜率不和本条相同,直接进栈,否则就要进行判断,设栈顶的直线与要判的直线 x 轴交点为x1,栈最上面两条直线交点为 x2,如果 x1<=x2,那么就删除栈顶的直线,它不可能被看到,依次重复直接条件不成立。栈里的直线就是答案。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-3;
const int maxn = 1e5 + 10;
const int maxm = 3e5 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}

struct Line{
  int id, k, b;
  bool operator < (const Line &l) const{
    return k < l.k || k == l.k && b > l.b;
  }
};
Line a[maxn];
stack<Line> st;

double crossX(const Line &lhs, const Line &rhs){
  return (rhs.b - lhs.b) * 1. / (lhs.k - rhs.k);
}

int main(){
  scanf("%d", &n);
  for(int i = 0; i < n; ++i){
    a[i].id = i;
    scanf("%d %d", &a[i].k, &a[i].b);
  }
  sort(a, a + n);
  st.push(a[0]);
  for(int i = 1; i < n; ++i){
    if(a[i].k == st.top().k)  continue;
    if(st.sz == 1){  st.push(a[i]);  continue; }
    while(st.sz != 1){
      Line l = st.top();  st.pop();
      double x1 = crossX(l, a[i]);
      double x2 = crossX(l, st.top());
      if(x1 > x2){ st.push(l);  break; }
    }
    st.push(a[i]);
  }
  vector<int> ans;
  while(!st.empty()){
    ans.push_back(st.top().id);  st.pop();
  }
  sort(ans.begin(), ans.end());
  for(int i = 0; i < ans.sz; ++i)  printf("%d ", ans[i]+1);
  printf("
");
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/7808909.html