【叉积性质】POJ 2318 TOYS && POJ 2398 Toy Storage

来源:互联网 发布:apache ab工具 编辑:程序博客网 时间:2024/05/16 12:54

http://acm.pku.edu.cn/JudgeOnline/problem?id=2318

//#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;typedef long long LL;#define mem(a, b) memset(a, b, sizeof(a))/*poj 2318给出一个矩形抽屉,然后有n个板子放在其中,连接上下底。给出m个玩具的坐标(视作点),问n+1个区间内,每个区间中有多少玩具。二分,叉乘判点在线段左边或右边*/const int N = 5010;struct Point{ double x,y; };    //点坐标struct V{ Point start,endd; };  //两点式V line[N];int ans[N];double xmult(Point a,Point b,Point c)   //(ca)×(cb){   return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);}bool pan(Point p,V ve){    if(xmult(p,ve.endd,ve.start) > 0)        return true;    return false;}int n,m;void solve(Point p){    if(!pan(p,line[n])){        ans[n]++;        return ;    }    int L = 1, R = n;    while(L < R){        int mid = L + (R-L>>1);        if(pan(p,line[mid]))            R = mid;        else            L = mid+1;    }    ans[L-1]++;    return ;}int main(){    while(~scanf("%d",&n) && n)    {        memset(ans,0,sizeof(ans));        double x1,y1,x2,y2;        scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2);        for(int i = 1; i <= n; i++)        {            Point tmp1,tmp2;            tmp1.y = y1;            tmp2.y = y2;            scanf("%lf%lf",&tmp1.x,&tmp2.x);            line[i].start = tmp1;            line[i].endd = tmp2;        }        Point p;        for(int i = 1; i <= m; i++){            scanf("%lf%lf",&p.x,&p.y);            solve(p);        }        for(int i = 0; i <= n; i++)            printf("%d: %d\n",i,ans[i]);        puts("");    }    return 0;}


http://acm.pku.edu.cn/JudgeOnline/problem?id=2398

//#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;typedef long long LL;#define mem(a, b) memset(a, b, sizeof(a))/*poj 2398给一个矩形,用N条线段将矩形分区域,撒一堆点,找每个区域的点个数有多少。这次给的线段没有按照顺序,所以要对线段排序。输出也有变化,输出的是  点的个数为x的区域个数有多少个Box点的个数x:拥有x个点的区域个数当然x不能等于0二分,叉乘判点在线段左边或右边*/const int N = 5010;struct Point{ double x,y; };    //点坐标struct V{ Point start,endd; };  //两点式V line[N];int ans[N],res[N];double xmult(Point a,Point b,Point c)   //(ca)×(cb){   return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);}bool pan(Point p,V ve){    if(xmult(p,ve.endd,ve.start) > 0)        return true;    return false;}bool cmp(V x,V y){    return pan(x.start,y);}int n,m;void solve(Point p){    if(!pan(p,line[n])){        ans[n]++;        return ;    }    int L = 1, R = n;    while(L < R){        int mid = L + (R-L>>1);        if(pan(p,line[mid]))            R = mid;        else            L = mid+1;    }    ans[L-1]++;    return ;}int main(){    while(~scanf("%d",&n) && n)    {        memset(ans,0,sizeof(ans));        memset(res,0,sizeof(res));        double x1,y1,x2,y2;        scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2);        for(int i = 1; i <= n; i++)        {            Point tmp1,tmp2;            tmp1.y = y1;            tmp2.y = y2;            scanf("%lf%lf",&tmp1.x,&tmp2.x);            line[i].start = tmp1;            line[i].endd = tmp2;        }        sort(line+1,line+n+1,cmp);        Point p;        for(int i = 1; i <= m; i++){            scanf("%lf%lf",&p.x,&p.y);            solve(p);        }        for(int i = 0; i <= n; i++)        {//            printf("%d: %d\n",i,ans[i]);            res[ans[i]]++;        }        puts("Box");        for(int i = 1; i <= m; i++)            if(res[i]) printf("%d: %d\n",i,res[i]);    }    return 0;}