【BZOJ 1007】【HNOI 2008】水平可见直线 解析几何

时间:2024-04-08 12:33:56

之前机房没网就做的这道题,用的解析几何判断交点横坐标

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 500003
#define eps 1E-8
using namespace std;
struct node {
double k, b;
int id;
} line[N];
int n, stack[N], top = 0;
bool ans[N];
inline bool cmp(node X, node Y) {
if (fabs(X.k - Y.k) < eps)
return X.b < Y.b;
else
return X.k < Y.k;
}
inline double crossx(int X, int Y) {
return (line[Y].b - line[X].b) / (line[X].k - line[Y].k);
}
inline void insert(int x) {
while (top) {
if (fabs(line[x].k - line[stack[top]].k) < eps)
--top;
else
if (crossx(x, stack[top]) <= crossx(stack[top], stack[top - 1]) && top > 1)
--top;
else
break;
}
stack[++top] = x;
}
int main() {
scanf("%d\n", &n);
for(int i = 1; i <= n; ++i) {
scanf("%lf%lf\n", &line[i].k, &line[i].b);
line[i].id = i;
}
sort(line + 1, line + n + 1, cmp);
for(int i = 1; i <= n; ++i)
insert(i);
for(int i = 1; i <= top; ++i)
ans[line[stack[i]].id] = 1;
for(int i = 1; i <= n; ++i)
if (ans[i])
printf("%d ",i);
return 0;
}

hhh