POJ 2318 计算几何大水题

来源:互联网 发布:135 139端口关闭 编辑:程序博客网 时间:2024/04/30 00:50

暴力一下 O(MN)

#include<cstdio>#include<cstring>#include<cmath>const int maxn = 5100;const double eps = 1e-12;int dcmp(double x){    return fabs(x) < eps ? 0 : (x < 0 ? -1 : 1);}struct Point{    double x, y;    Point(){}    Point(double _x, double _y):x(_x),y(_y){}    void input(){scanf("%lf%lf", &x, &y);}    void set(double a, double b){x = a; y = b;}};typedef Point Vector;Vector operator - (const Vector &a, const Vector &b){    return Vector(a.x - b.x, a.y - b.y);}double operator ^ (const Vector &a, const Vector &b){    return a.x * b.y - a.y * b.x;}Point u[maxn], d[maxn];int cnt[maxn];int main(){//  freopen("in.txt", "r", stdin);    int n, m;    Point A, B, C;    while(scanf("%d%d", &n, &m) == 2 && n){        memset(cnt, 0, sizeof(cnt));        A.input(); B.input();        u[0].set(A.x, A.y);        d[0].set(A.x, B.y);        for(int i = 1; i <= n; i++){            double t;            u[i].set((scanf("%lf", &t), t), A.y);            d[i].set((scanf("%lf", &t), t), B.y);        }        u[n+1].set(B.x, A.y);        d[n+1].set(B.x, B.y);        for(int i = 0; i < m; i++){            C.input();            for(int i = 0; i <= n; i++)                if(dcmp((d[i]-u[i])^(C-u[i])) == 1 && dcmp((d[i+1]-u[i+1])^(C-d[i+1])) == -1){                    cnt[i]++;                    break;                }        }        for(int i = 0; i <= n; i++)            printf("%d: %d\n", i, cnt[i]);        printf("\n");    }    return 0;}
0 0