poj 2398 Toy Storage(計算幾何:叉積)

基本上和poj 2318一模一樣。。。

改下輸出就可以了

代碼如下:

/* ***********************************************
Author        :yinhua
Created Time  :2014年12月01日 星期一 19時25分15秒
File Name     :poj2398.cpp
************************************************ */

#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 100010
#define LL long long
using namespace std;

struct Point {  
    int x, y;  
    Point() { }  
  
    Point(int _x, int _y) {  
        x = _x;y = _y;  
    }  
  
    Point operator -(const Point &b) const {  
        return Point(x-b.x, y-b.y);  
    }  
    int operator *(const Point &b) const {  
        return x*b.x+y*b.y;  
    }  
    int operator ^(const Point &b) const {  
        return x*b.y-y*b.x;  
    }  
};  
  
struct Line {  
    Point s, e;  
    Line() { }  
    Line(Point _s, Point _e) {  
        s = _s; e = _e;  
    }  
};  

bool cmp(Line a, Line b) {
    return (a.s).x < (b.s).x;
}

  
int xmult(Point p0, Point p1, Point p2) {//p0點與線段p1 p2的叉積  
    return (p1-p0)^(p2-p0);  
}  
  
Line line[MAXN];  
int res[MAXN];
int ans[MAXN];  
  
int main(void) {  
    int m, n, x1, y1, x2, y2;  
    bool first = true;  
    while(~scanf("%d", &n) && n) {  
  
        scanf("%d%d%d%d%d", &m, &x1, &y1, &x2, &y2);  
        int ui, li;  
        for(int i=0; i<n; ++i) {  
            scanf("%d%d", &ui, &li);  
            line[i] = Line(Point(ui, y1), Point(li, y2));  
        }  
        sort(line, line+n, cmp);
        line[n] = Line(Point(x2, y1), Point(x2, y2));  
        int x, y;  
        Point p;  
        memset(ans, 0, sizeof(ans));  
        for(int i=1; i<=m; ++i) {
            scanf("%d%d", &x, &y);  
            p = Point(x, y);  
            int l = 0, r = n;  
            int tmp;  
            while(l <= r) {  
                int mid = (l+r)>>1;  
                if(xmult(p, line[mid].s, line[mid].e) < 0) {//點在當前線的左側  
                    tmp = mid;  
                    r = mid-1;  
                } else l = mid+1;//點在當前線的右側  
            }  
            ans[tmp]++;  
        }  
        memset(res, 0, sizeof(res));
        for(int i=0; i<=n; ++i) {
            if(ans[i]) ++res[ans[i]];
        }
        bool ok = true;
        for(int i=1; i<=m; ++i) {
            if(res[i]) {
                if(ok) {
                    ok = false;
                    puts("Box");
                }
                printf("%d: %d\n", i, res[i]);
            }
        }
    }  
    return 0;  
}  


發佈了481 篇原創文章 · 獲贊 8 · 訪問量 31萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章