BZOJ1007 凸包

https://www.lydsy.com/JudgeOnline/problem.php?id=1007

比较简单的一道凸包问题:

拿单调栈来维护:

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e4;
inline void read(int &x){char ch;bool ok;
for(ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if(ch=='-') ok=1;
for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());if(ok) x=-x;}
struct Segment{
    double A,B;
    int id;
}Seg[N+10];
bool operator <(Segment a,Segment b){
    if(a.A==b.A)
        return a.B>b.B;
    return a.A<b.A;
}
struct nod{
    double x,y;
};
stack<int> Sts;
stack<nod> Stn;
nod cross(Segment a,Segment b){
    double x=(b.B-a.B)/(a.A-b.A);
    double y=a.A*x+a.B;
    nod no;
    no.x=x,no.y=y;
    return no;
}
bool check(nod a,Segment b){
    double ans=a.x*b.A+b.B;
    if(ans>=a.y) return 1;
    return 0;
}
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;++i){
        int a,b;
        read(a);read(b);
        Seg[i].A=double(a);
        Seg[i].B=double(b);
        Seg[i].id=i;
    }
    sort(Seg+1,Seg+n+1);
    Sts.push(1),Sts.push(2);
    Stn.push(cross(Seg[1],Seg[2]));
    for(int i=3;i<=n;++i){
        if(Seg[i].A==Seg[i-1].A) continue;
        while(!Stn.empty()){
            if(check(Stn.top(),Seg[i]))
                Stn.pop(),Sts.pop();
            else
                break;
        }
        Stn.push(cross(Seg[i],Seg[Sts.top()]));
        Sts.push(i);
    }
    vector<int> V;
    while(Sts.size()){
        V.push_back(Seg[Sts.top()].id);
        Sts.pop();
    }
    sort(V.begin(),V.end());
    for(int i=0;i<V.size();++i)
        printf("%d ",V[i]);
    cout<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值