题目链接:http://poj.org/problem?id=2398
这个题目和上一篇博客题目意思几乎一样,不过这个是要求格子内装玩具个数为i的格子数
具体解法基本一致!
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
using namespace std;
#define maxn 6000
struct point{
double x,y;
point(double _x=0,double _y=0):x(_x),y(_y){}
}begin,end,temp;
struct line{
point a,b;
}po[maxn];
int n,m;
double cross(point &a,point &b,point &c){
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
bool cmp(const line &a,const line &b){
if(a.b.x != b.b.x) return a.b.x < b.b.x;
return a.a.x < a.a.x;
}
int ans[maxn],re_ans[maxn];
int find_ans(point temp){
if(temp.x>end.x || temp.x <begin.x || temp.y>begin.y || temp.y<end.y) return 0;
int l=0,r=n-1,mid;
while(l<r){
mid=(l+r)/2;
if(l+1==r){
ans[l]++;
return 0;
}
if(cross(temp,po[mid].a,po[mid].b)>0)
r=mid;
else l=mid;
}
return 0;
}
int main(){
int i,j,k;
while(scanf("%d",&n),n){
scanf("%d%lf%lf%lf%lf",&m,&begin.x,&begin.y,&end.x,&end.y);
po[0].a=point(begin.x,end.y);
po[0].b=begin;
for(i=1;i<=n;i++){
scanf("%lf%lf",&po[i].b.x,&po[i].a.x);
po[i].a.y=end.y,po[i].b.y=begin.y;
}
n++;
po[n].a=end;
po[n].b=point(end.x,begin.y);
n++;
sort(po,po+n,cmp);
memset(ans,0,sizeof(ans));
for(i=0;i<m;i++){
scanf("%lf%lf",&temp.x,&temp.y);
find_ans(temp);
}
memset(re_ans,0,sizeof(re_ans));
for(i=0;i<n-1;i++)
re_ans[ans[i]]++;
printf("Box\n");
for(i=1;i<=n;i++){
if(re_ans[i]!=0){
printf("%d: %d\n",i,re_ans[i]);
}
}
}
return 0;
}