POJ 2318 TOYS + POJ 2398 Toy Storage (点与直线的位置关系)

http://poj.org/problem?id=2318


利用向量外积+lower_bound搞定,见代码。

/*172ms,484KB*/

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mx = 5005;

struct point
{
	int x, y;
	point() {}
	point(int x, int y): x(x), y(y) {}
};

struct vec
{
	point p1, p2;
	vec() {}
	vec(point p1, point p2): p1(p1), p2(p2) {} ///冒号初始化:分配内存空间并赋值
	int cross_product(const vec& v)
	{
		return (p2.x - p1.x) * (v.p2.y - v.p1.y) - (p2.y - p1.y) * (v.p2.x - v.p1.x);
	}
	bool operator < (const point& p) const
	{
		return vec(p1, p2).cross_product(vec(p1, p)) < 0;
	}
} v[mx];

int sum[mx];

int main()
{
	int n, m, x1, y1, x2, y2, i, ux, lx, x, y;
	while (scanf("%d%d%d%d%d%d", &n, &m, &x1, &y1, &x2, &y2), n)
	{
		for (i = 0; i < n; ++i)
		{
			scanf("%d%d", &ux, &lx);
			v[i] = vec(point(lx, y2), point(ux, y1));
		}
		memset(sum, 0, sizeof(sum));
		while (m--)
		{
			scanf("%d%d", &x, &y);
			++sum[lower_bound(v, v + n, point(x, y)) - v];
		}
		for (i = 0; i <= n; ++i) printf("%d: %d\n", i, sum[i]);
		putchar(10);
	}
	return 0;
}

http://poj.org/problem?id=2398


和前面那题一样,不过要先给这些纸板的位置排序。


/*0ms,412KB*/

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mx = 1005;

struct point
{
	int x, y;
	point() {}
	point(int x, int y): x(x), y(y) {}
};

struct vec
{
	point p1, p2;
	vec() {}
	vec(point p1, point p2): p1(p1), p2(p2) {} ///冒号初始化:分配内存空间并赋值
	int cross_product(const vec& v)
	{
		return (p2.x - p1.x) * (v.p2.y - v.p1.y) - (p2.y - p1.y) * (v.p2.x - v.p1.x);
	}
	bool operator < (const vec& v) const
	{
		return p1.x < v.p1.x;
	}
	bool operator < (const point& p) const
	{
		return vec(p1, p2).cross_product(vec(p1, p)) < 0;
	}
} v[mx];

int sum[mx], cntsum[mx];

int main()
{
	int n, m, x1, y1, x2, y2, i, ux, lx, x, y;
	while (scanf("%d%d%d%d%d%d", &n, &m, &x1, &y1, &x2, &y2), n)
	{
		puts("Box");
		for (i = 0; i < n; ++i)
		{
			scanf("%d%d", &ux, &lx);
			v[i] = vec(point(lx, y2), point(ux, y1));
		}
		sort(v, v + n);
		memset(sum, 0, sizeof(sum));
		for (i = 0; i < m; ++i)
		{
			scanf("%d%d", &x, &y);
			++sum[lower_bound(v, v + n, point(x, y)) - v];
		}
		memset(cntsum, 0, sizeof(cntsum));
		for (i = 0; i <= n; ++i) ++cntsum[sum[i]];
		for (i = 1; i <= m; ++i)
			if (cntsum[i]) printf("%d: %d\n", i, cntsum[i]);
	}
	return 0;
}

你可能感兴趣的:(ACM,poj,计算几何)