poj2398 Toy Storage computational geometry, the cross product binary

poj2398 Toy Storage

link

capacity

Subject to the effect

This question is probably meant to input six numbers: n, m, x1, y1, x2, y2. N represents the number of the card, the card vertically (or obliquely) is placed in the box, the box can be divided into block regions n + 1, respectively, and n represents 0, m represents the number of toys, (x1, y1) on behalf of the coordinates of the top left vertex of the box, (x2, y2) on behalf of the lower right vertex coordinates box, next n lines, each line has two numbers a, b, (a, y1), (b, y2) represent the card two vertices positions, each row next two numbers m rows from c, d, (c, d), representative of the coordinates of the toy is placed, you let the output region when the toy number (incremented, excluding 0) , and the same number of toys there are several areas, when the input end of the first number is zero.

Thinking

First partition ordering.
Then each toy-half position. Determined by the cross product.

Code

#include <algorithm>
#include <cstdio>
using namespace std;
const int N=1005;
const double eps=1e-10;
struct point {
    double x,y;
}a[N],b[N];
point operator - (point a,point b) {
    return (point){a.x-b.x,a.y-b.y};
}
double operator * (point a,point b) {
    return a.x*b.y-b.x*a.y;
}
struct line {
    double u,l;
}l[N];
int n,m;
bool cmp(line a,line b) {
    return a.u<b.u;
}
int cnt[N],ans[N];

int main() {
    while(scanf("%d",&n)!=EOF&&n) {
        scanf("%d",&m);
        scanf("%lf%lf%lf%lf",&b[0].x,&b[0].y,&a[n+1].x,&a[n+1].y);
        a[0].x=b[0].x,a[0].y=b[n+1].y;
        b[n+1].x=a[n+1].x,b[n+1].y=b[0].y;
        for(int i=1;i<=n;++i)
            scanf("%lf%lf",&l[i].u,&l[i].l);
        sort(l+1,l+1+n,cmp);
        for(int i=1;i<=n;++i) {
            a[i].x=l[i].l,a[i].y=a[n+1].y;
            b[i].x=l[i].u,b[i].y=b[0].y;
        }
        memset(cnt,0,sizeof(cnt));
        memset(ans,0,sizeof(ans));
        for(int i=1;i<=m;++i) {
            point c;
            scanf("%lf%lf",&c.x,&c.y);
            if(!(a[n+1].y<=c.y&&c.y<=b[0].y&&b[0].x<=c.x&&c.x<=a[n+1].x)) continue;
            int l=0,r=n+1;
            while(l+1!=r) {
                int mid=(l+r)>>1;
                if((b[mid]-a[mid])*(c-a[mid])>0) r=mid;
                else l=mid;
            }
            cnt[l+1]++;
        }
        puts("Box");
        for(int i=1;i<=n+1;++i)
            if(cnt[i]) ans[cnt[i]]++;
        for(int i=1;i<=m;++i)
            if(ans[i]) printf("%d: %d\n",i,ans[i]);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/dsrdsr/p/11013972.html