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;
}