POJ2318:TOYS(叉积判断点和线段的关系+二分)&&POJ2398Toy Storage

题目:http://poj.org/problem?id=2318 

题意:

给定一个如上的长方形箱子,中间有n条线段,将其分为n+1个区域,给定m个玩具的坐标,统计每个区域中的玩具个数。(其中这些线段有序且不相交)

解答:

因为线段是有序给出,所以不用排序,判断某个点在哪个区域,采用二分法,将某个点和线段的叉积来判断这个点是在线的左边或者右边,根据这个来二分找出区域。

这是第一道计算几何的题目,怎么说呢,对于二分的边界问题还有点搞不清楚,这次是对线段二分,感觉二分真的很有用处。

开阔思维!!!具体请看代码。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <queue>
#define inf 0x3f3f3f3f
#define eps 1e-9
typedef long long ll;
using namespace std;
int n,m,x,y,x2,y2,temp;
int a[5010][2],point[5010][2],sum[5010];

void er(int l,int r,int keyx,int keyy)
{
    ll X1,Y1,X2,Y2;
    int mid;
    while(l<r)
    {
        mid=l+(r-l)/2;
        X1=a[mid][0]-keyx;
        Y1=y-keyy;
        X2=a[mid][1]-keyx;
        Y2=y2-keyy;
        if(X1*Y2-X2*Y1<=0)
            r=mid;
        else l=mid+1;
    }
    sum[l]++;
}
int main()
{
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        scanf("%d%d%d%d%d",&m,&x,&y,&x2,&y2);
        for(int i=0; i<n; i++)
            scanf("%d%d",&a[i][0],&a[i][1]);
        a[n][0]=a[n][1]=x2;//保证所有点都能找到叉积<=0的线段
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&point[i][0],&point[i][1]);
        }
        memset(sum,0,sizeof(sum));
        for(int i=0; i<m; i++)
        {
            er(0,n,point[i][0],point[i][1]);
        }
        for(int i=0; i<=n; i++)
        {
            printf("%d: %d
",i,sum[i]);
        }
        cout<<endl;
    }
    return 0;
}

 大神写的规范代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

struct Point {
    int x, y;
};

struct Line {
   Point a, b;
} line[5005];

int cnt[5005];

int Multi(Point p1, Point p2, Point p0) {
    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}

void BSearch(Point a, int n) {
    int l, r, mid;

    l = 0; r = n-1;
    while (l < r) {
        mid = (l + r) >> 1;
        if (Multi(a, line[mid].a, line[mid].b) > 0) l = mid + 1;
        else r = mid;
    }
    if (Multi(a, line[l].a, line[l].b) < 0) cnt[l]++;
    else cnt[l+1]++;
}

int main()
{
    int n, m, x1, y1, x2, y2;
    int i, t1, t2;
    Point a;

    while (scanf ("%d", &n) && n) {
        scanf ("%d%d%d%d%d", &m, &x1, &y1, &x2, &y2);
        for (i = 0; i < n; i++) {
            scanf ("%d%d", &t1, &t2);
            line[i].a.x = t1;
            line[i].a.y = y1;
            line[i].b.x = t2;
            line[i].b.y = y2;
        }
        memset(cnt, 0, sizeof (cnt));
        for (i = 0; i < m; i++) {
            scanf ("%d%d", &a.x, &a.y);
            BSearch(a, n);
        }
        for (i = 0; i <= n; i++)
            printf ("%d: %d
", i, cnt[i]);
        printf("
");
    }
    return 0;
}

 POJ2398:

本题和poj2318 TOYS大致一样,但有一些不同。

1:输入是无序的,需要自己排序。

2:输出是输出拥有相同玩具数的区块有几个。     (区块里的玩具数:相同玩具数的区块个数)

知道题意就很好做了。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <queue>
#define inf 0x3f3f3f3f
#define eps 1e-9
typedef long long ll;
using namespace std;
int n,m,x,y,x2,y2,temp;
int point[1010][2],sum[1010];
struct node
{
    int a1,a2;
} a[1010];
int cmp(const void *a,const void *b)
{
    struct node *aa=(struct node *)a;
    struct node *bb=(struct node *)b;
    return aa->a1-bb->a1;
}
void er(int l,int r,int keyx,int keyy)
{
    ll X1,Y1,X2,Y2;
    int mid;
    while(l<r)
    {
        mid=l+(r-l)/2;
        X1=a[mid].a1-keyx;
        Y1=y-keyy;
        X2=a[mid].a2-keyx;
        Y2=y2-keyy;
        if(X1*Y2-X2*Y1<=0)
            r=mid;
        else l=mid+1;
    }
    sum[l]++;
}
int main()
{
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        scanf("%d%d%d%d%d",&m,&x,&y,&x2,&y2);
        for(int i=0; i<n; i++)
            scanf("%d%d",&a[i].a1,&a[i].a2);
        a[n].a1=a[n].a2=x2;
        qsort(a,n+1,sizeof(a[0]),cmp);
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&point[i][0],&point[i][1]);
        }
        memset(sum,0,sizeof(sum));
        for(int i=0; i<m; i++)
        {
            er(0,n,point[i][0],point[i][1]);
        }
        printf("Box
");
        int ha[1010];
        memset(ha,0,sizeof(ha));
        for(int i=0; i<=m; i++)
        {
            if(sum[i])
                ha[sum[i]]++;
        }
        for(int i=0; i<=1001; i++)
        {
            if(ha[i]) printf("%d: %d
",i,ha[i]);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zhangmingcheng/p/4274999.html