poj 2398 Toy Storage

来源:互联网 发布:mac outlook windows 编辑:程序博客网 时间:2024/06/07 10:18


题目链接:http://poj.org/problem?id=2398

分析:2318的变形题,需要排序,不多解释了。

代码如下:

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>using namespace std;struct Node {int x,y;}edge[5005],node[5005];int cmp(Node a,Node b){return a.x<b.x;}int ans[5005],num[5005];int calc(int xA,int yA,int xB,int yB,int xC,int yC){return (xA-xC)*(yB-yC)-(yA-yC)*(xB-xC);}int judge(int a,int b,int y1,int y2){if(calc(edge[b].x,y1,edge[b].y,y2,node[a].x,node[a].y)>0 && calc(edge[b+1].x,y1,edge[b+1].y,y2,node[a].x,node[a].y)<0)return 1;return 0;}int main(){int n,m,x1,y1,x2,y2;int inde=0;while(scanf("%d",&n),n){memset(ans,0,sizeof(ans));memset(num,0,sizeof(num));scanf("%d%d%d%d%d",&m,&x1,&y1,&x2,&y2);edge[0].x=edge[0].y=x1;edge[n+1].x=edge[n+1].y=x2;for(int i=1;i<=n;i++)scanf("%d%d",&edge[i].x,&edge[i].y);sort(edge,edge+n+2,cmp);for(int i=0;i<m;i++)scanf("%d%d",&node[i].x,&node[i].y);for(int i=0;i<m;i++){for(int j=0;j<n+1;j++){if(judge(i,j,y1,y2))ans[j]++;}}for(int i=0;i<=n;i++)if(ans[i]>0)num[ans[i]]++;printf("Box\n");        for(int i = 1;i <= n;i++)            if(num[i]>0)                printf("%d: %d\n",i,num[i]);}return 0;}


原创粉丝点击