POJ 2318 TOYS(计算几何)

利用叉积推断点在线段左边还是右边,然后进行二分就可以

代码:

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

const int N = 5005;

int n, m, x1, y1, x2, y2;

struct Point {
    int x, y;
    Point() {}
    Point(int x, int y) {
        this->x = x;
        this->y = y;
    }
};

typedef Point Vector;

Vector operator - (Vector A, Vector B) {
    return Vector(A.x - B.x, A.y - B.y);
}

struct Seg {
    Point a, b;
    Seg() {}
    Seg(Point a, Point b) {
        this->a = a;
        this->b = b;
    }
} seg[N];

int ans[N];

int Cross(Vector A, Vector B) {return A.x * B.y - A.y * B.x;}

void gao(Point p) {
    int l = 0, r = n;
    while (l < r) {
        int mid = (l + r) / 2;
        if (Cross(seg[mid].a - p, seg[mid].b - p) < 0) r = mid;
        else l = mid + 1;
    }
    ans[l]++;
}

int main() {
    while (~scanf("%d", &n) && n) {
        memset(ans, 0, sizeof(ans));
        scanf("%d%d%d%d%d", &m, &x1, &y1, &x2, &y2);
        int x, y;
        for (int i = 0; i < n; i++) {
            scanf("%d%d", &x, &y);
            seg[i] = Seg(Point(x, y1), Point(y, y2));
        }
        for (int i = 0; i < m; i++) {
            scanf("%d%d", &x, &y);
            gao(Point(x, y));
        }
        for (int i = 0; i <= n; i++)
            printf("%d: %d\n", i, ans[i]);
        printf("\n");
    }
    return 0;
}


转载于:https://www.cnblogs.com/llguanli/p/6913689.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值