poj2398

/***************************************************************\

 *Author:Hu Wenbiao

 *Created Time: Tue 27 Jul 2010 07:10:34 PM CST

 *File Name: main.cpp

 *Description:几何题目。对poj2318的扩展

\***************************************************************/

//*========================*Head File*========================*\\



#include<iostream>

#include<stdio.h>

#include<stdlib.h>

#include<algorithm>

#include<string.h>

/*----------------------*Global Variable*----------------------*/

struct Cardboard{//隔板

 int u,l;

}card[1010];

int toynum[1010],ans[1010],n,m,x1,y1,x2,y2,X,Y,_max;//toynum各分区的数目

//*=======================*Main Program*=======================*//

using namespace std;



bool cmp(Cardboard a,Cardboard b){//返回a是否在b的左边,以此排序

 int s=b.u-b.l,t=y1-y2,p=a.u-b.l,q=y1-y2;

 return s*q-t*p>0;

}

bool left(int m){//判断toy在第m个隔板左边

 int a=card[m].u-card[m].l,b=y1-y2,c=X-card[m].l,d=Y-y2;

 return a*d-b*c>0;

}



void location(){//二分法,将toy所在的区间数目加1

 int s=0,t=n;

 int m=(s+t)/2;

 while(s<t){

  if(left(m)){

   t=m;

   m=(s+t)/2;

  }

  else{

   s=m+1;

   m=(s+t)/2;

  }

 }

 toynum[t]++;

}

int main(){

 //freopen("input","r",stdin);

 while(scanf("%d%d%d%d%d%d",&n,&m,&x1,&y1,&x2,&y2)!=EOF&&n){

  for(int i=0;i<n;i++)

    scanf("%d%d",&card[i].u,&card[i].l);

  sort(card,card+n,cmp);//隔板从左到右的顺序排序

  card[n].u=card[n].l=x2;//用box右边界作第n个区间的‘隔板’

  memset(toynum,0,sizeof(toynum));

  while(m--){

   scanf("%d%d",&X,&Y);

   location();

  }

  memset(ans,0,sizeof(ans));

  for(int i=0;i<=n;i++){

   _max=_max>toynum[i]?_max:toynum[i];

   ans[toynum[i]]++;

  }

  printf("Box\n");

  for(int i=1;i<=_max;i++)

    if(ans[i])

   printf("%d: %d\n",i,ans[i]);

 }

}

你可能感兴趣的:(poj)