• 题面描述
    • \(xOy\)直角坐标平面上有\(n\)条直线\(L_1,L_2,...,L_n\),若在\(y\)值为正无穷大处往下看,能见到\(L_i\)的某个子线段,则称\(L_i\)为可见的,否则\(L_i\)为被覆盖的.例如,对于直线:\(L_1:y=x; L_2:y=-x; L_3:y=0\)\(L_1\)\(L_2\)是可见的,\(L_3\)是被覆盖的.给出\(n\)条直线,表示成\(y=Ax+B\)的形式\((|A|,|B|\leq 5*10^5)\),且\(n\)条直线两两不重合.求出所有可见的直线.
  • 输入格式
    • 第一行为\(N(0 < N < 50000)\),接下来的\(N\)行输入\(A_i,B_i\)
  • 输出格式
    • 从小到大输出可见直线的编号,两两中间用空格隔开,最后一个数字后面也必须有个空格
  • 题解
    • 不难看出该题核心就是对于给定\(n\)条直线,求他们的下凸包
    • 注意有多个\(A_i\)相同的,在压入栈的时候按\(B_i\)从小到大压入,把前面没有被弹出的\(A_x==A_i\)全部弹出
    • 另:前两条直线不能默认直接压进去,因为他们\(A\)可能相等
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=5e4+5;
const double eps=1e-8;
struct rec{
    double a,b;
    int id;
} a[MAXN];
int n;
bool cmp(rec a,rec b){ return fabs(a.a-b.a)<eps?a.b<b.b:a.a<b.a; }
int st[MAXN],top;
struct dt{
    double x,y;
};
dt dot(int i,int j){
    double x=(a[j].b-a[i].b)/(a[i].a-a[j].a);
    double y=a[i].a*x+a[i].b;
    return (dt){x,y};
}
int ans[MAXN],cnt;
int main(){
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%lf%lf",&a[i].a,&a[i].b),a[i].id=i;
    sort(a+1,a+n+1,cmp);
    for (int i=1;i<=n;i++){
        while (top>=1&&fabs(a[st[top]].a-a[i].a)<eps) top--;
        while (top>=2&&dot(st[top-1],st[top]).x>=dot(st[top-1],i).x) top--;
        st[++top]=i;
    }
    for (int i=1;i<=top;i++) ans[cnt++]=a[st[i]].id;
    sort(ans,ans+cnt);
    for (int i=0;i<cnt;i++) printf("%d ",ans[i]);
    printf("\n");
    return 0;
}
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄