半平面交模板(BZOJ1007)

#include<cstdio>
#include<algorithm>
#define LDB long double
using namespace std;

  int ans[600000];
  struct lin{
      LDB k,b;
      int num;
  }a[600000];
  
  struct rec{
      LDB inte;
      int num;
  }sta[600000];
 
  int mycomp(const lin &a,const lin &b){
      if (a.k<b.k) return(1);
      if (a.k>b.k) return(0);
      if (a.b>b.b) return(1);
      return(0);
  }
  
  LDB inter(lin a,lin b){
      return((b.b-a.b)/(a.k-b.k));
  }

  int main(){
      int n;
    scanf("%d",&n);
    
    for (int i=1;i<=n;i++){
        int t1,t2;
        scanf("%d%d",&t1,&t2);
        a[i].k=(LDB)t1;a[i].b=(LDB)t2;a[i].num=i;
    }
    
    sort(a+1,a+n+1,mycomp);
    int top;
    sta[top=1].num=1;sta[top].inte=-1000000000;
    for (int i=2;i<=n;i++)
      if (a[i].k!=a[i-1].k){
          while ((top>0)&&(inter(a[i],a[sta[top].num])<=sta[top].inte)) top--;
          sta[++top].num=i;
        sta[top].inte=inter(a[i],a[sta[top-1].num]);
      }
      
    for (int i=1;i<=top;i++) ans[i]=a[sta[i].num].num;
    sort(ans+0,ans+top+1);
    for (int i=1;i<=top;i++) printf("%d ",ans[i]);  
  }

 

posted @ 2016-06-17 16:43  z1j1n1  阅读(342)  评论(0编辑  收藏  举报