POJ 2398(同POJ2318)

来源:互联网 发布:banner制作软件 编辑:程序博客网 时间:2024/05/17 23:30

题目:题目链接

题目意思同2318,只不过加了线段排序。再者输出每一种数量有几个格子就OK:

代码:

#include <iostream>#include <cstdio>#include <string>#include <string.h>#include <map>#include <vector>#include <cstdlib>#include <cmath>#include <algorithm>#include <queue>#include <set>#include <stack>using namespace std;const int M=1030;const double eps=1e-8;struct point{    __int64 x,y;} p[M],q;struct rec{    point p[4];} r[M];__int64 cnt[M],cnt1[M];int n,m;void init(){    for(int i=0; i<=n+1; i++)        cnt[i]=cnt1[i]=0;}double cross(double x1,double y1,double x2,double y2){    return x1*y2-x2*y1;}void search(){    int low=0,high=n;    bool flag=false;    while(low<=high)    {        int mid=(low+high)>>1;        double temp1=cross(r[mid].p[0].x-q.x,r[mid].p[0].y-q.y,r[mid].p[1].x-q.x,r[mid].p[1].y-q.y);        double temp2=cross(r[mid].p[3].x-q.x,r[mid].p[3].y-q.y,r[mid].p[2].x-q.x,r[mid].p[2].y-q.y);        if(temp1>=eps && temp2<=-eps)        {            cnt[mid]++;            return ;        }        if(temp2>eps)            low=mid+1;        else            high=mid-1;    }}bool cmp(point p,point q){    return p.x<q.x;}int main(){    while(scanf("%d",&n)==1)    {        if(!n) break;        init();        scanf("%d",&m);        scanf("%I64d%I64d%I64d%I64d",&r[0].p[0].x,&r[0].p[0].y,&r[n].p[2].x,&r[n].p[2].y);        r[0].p[1].x=r[0].p[0].x;        r[0].p[1].y=r[n].p[2].y;        r[n].p[3].x=r[n].p[2].x;        r[n].p[3].y=r[0].p[0].y;        for(int i=1; i<=n; i++)        {            scanf("%I64d%I64d",&p[i].x,&p[i].y);        }        sort(p+1,p+n+1,cmp);        for(int i=1; i<=n; i++)        {            r[i-1].p[3].x=p[i].x;            r[i-1].p[3].y=r[i-1].p[0].y;            r[i-1].p[2].x=p[i].y;            r[i-1].p[2].y=r[i-1].p[1].y;            r[i].p[0].x=p[i].x;            r[i].p[0].y=r[i-1].p[3].y;            r[i].p[1].x=p[i].y;            r[i].p[1].y=r[i-1].p[2].y;        }        for(int i=0; i<m; i++)        {            scanf("%I64d%I64d",&q.x,&q.y);            search();        }        printf("Box\n");        for(int i=0; i<=n; i++)        {            if(cnt[i])                cnt1[cnt[i]]++;        }        for(int i=1; i<=m; i++)        {            if(cnt1[i])                printf("%d: %I64d\n",i,cnt1[i]);        }    }    return 0;}

努力努力...

原创粉丝点击