您的位置:首页 > 其它

poj 2398 Toy Storage(计算几何:叉积)

2014-12-01 22:05 501 查看
基本上和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
= 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: