POJ2398(计算几何)

POJ2318的升级版,就是增加了边界的排序。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
#define maxn 5111
#define x1 x_1
#define x2 x_2
#define y1 y_1
#define y2 y_2
#define find Find

struct node {
    double x1, y1, x2, y2;
    double x, y;
    bool operator < (const node &a) const {
        return x < a.x;
    }
}a[maxn], b[maxn];
int n, m, ans[maxn], cnt[maxn];
double x1, y1, x2, y2;

bool cmp (const node &a, const node &b) {
    return a.x1 < b.x1;
}

void find (int pos) {
    double h = b[pos].y;
    for (int i = 0; i < n; i++) {
        double rate = (h-a[i].y1)/(a[i].y2-a[i].y1);
        a[i].x = a[i].x1 + rate*(a[i].x2-a[i].x1);
    }
    int p = upper_bound (a, a+n, b[pos]) - a-1;
    ans[p]++;
}

bool legal (double x, double y) {
    if (x < x1 || x > x2 || y < y2 || y > y1)
        return 0;
    return 1;
}

int main () {
    //freopen ("in", "r", stdin);
    while (scanf ("%d", &n) == 1 && n) {
        scanf ("%d%lf%lf%lf%lf", &m, &x1, &y1, &x2, &y2);
        a[0].x1 = x1, a[0].y1 = y2, a[0].x2 = x1, a[0].y2 = y1;
        double u, v;
        for (int i = 1; i <= n; i++) {
            scanf ("%lf%lf", &u, &v);
            a[i].x1 = v, a[i].x2 = u;
            a[i].y1 = y2, a[i].y2 = y1;
        }
        n++;
        a[n].x1 = x2, a[n].y1 = y2;
        a[n].x2 = x2, a[n].y2 = y1;
        n++;
        sort (a, a+n, cmp);
        memset (ans, 0 ,sizeof ans);
        for (int i = 1; i <= m; i++) {
            scanf ("%lf%lf", &u, &v);
            b[i].x = u, b[i].y = v;
            if (!legal (u, v))
                continue;
            find (i);
        }
        printf ("Box\n");
        memset (cnt, 0, sizeof cnt);
        for (int i = 0; i < n-1; i++) {
            cnt[ans[i]]++;
        }
        for (int i = 1; i <= n; i++) {
            if (cnt[i])
                printf ("%d: %d\n", i, cnt[i]);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值