PS:网上的代码大多数都是错的,结果还能AC,这道题的数据很水哟。
【代码】
<span style="font-size:18px;">#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <cmath> using namespace std; const int N=50000; const double eps=1e-5; struct Line { double k,b; int id; }line[N],_line[N]; struct Point { double x,y; }now,last; int n,_n,stk[N]; inline int dc(double i,double j) { if (fabs(i-j)<eps) return 0; return i<j?-1:1; } int cmp(Line La,Line Lb) { int r=dc(La.k,Lb.k); return r?r<0:dc(La.b,Lb.b)>0; } inline Point get_point(int i,int j) { double k1=_line[i].k,b1=_line[i].b,k2=_line[j].k,b2=_line[j].b; Point P; P.x=(b2-b1)/(k1-k2); P.y=(b1*k2-b2*k1)/(k1-k2); return P; } inline int cmp1(int i,int j) { return _line[i].id<_line[j].id; } int main(void) { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%lf%lf",&line[i].k,&line[i].b),line[i].id=i; sort(line+1,line+n+1,cmp); for (int i=1;i<=n;i++) { if (_n&&!dc(line[i].k,_line[_n].k)) continue; _line[++_n]=line[i]; } stk[++stk[0]]=1,stk[++stk[0]]=2; for (int i=3;i<=_n;i++) { if (stk[0]&&!dc(_line[stk[stk[0]]].k,_line[i].k)) continue; for (;stk[0]>=2;) { last=get_point(stk[stk[0]-1],stk[stk[0]]); now=get_point(stk[stk[0]],i); if (last.x>=now.x) stk[stk[0]--]=0; else break; } stk[++stk[0]]=i; } sort(stk+1,stk+stk[0]+1,cmp1); for (int i=1;i<=stk[0];i++) { if (!line[i].id) continue; printf("%d ",_line[stk[i]].id); } printf("\n"); return 0; }</span>
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u013598409/article/details/46763567