初入计算几何——[poj_2318] TOYS && [poj_2398]Toy Storage

TOYS
Toy Storage

1

题目意思就是给你一个盒子,然后用几条线把盒子分块,然后告诉你玩具的坐标,问你每一块中有多少个玩具。

主要算法就是判断点在直线的两侧,可以用叉积来看。
在直线上取两点 A A A B B B,(这题可以直接取端点),要判断的点为 C C C点,
在这里插入图片描述
根据右手准则就可以判断出,所以点在线段左侧时,叉积小于0;在右侧时,叉积大于0。

这题只要找到第一个叉积小于0的就行。

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
#define endl '\n'
#define pb(a) push_back(a)
#define al(x) x.begin(), x.end()
#define sz(x) int(x.size())
#define IOS ios::sync_with_stdio(0);

int n, m;
// 点
struct Point {
	int x, y;
	Point(int x = 0, int y = 0): x(x), y(y) {}
} s, e;

Point operator - (const Point &A, const Point&B) {
	return Point(A.x - B.x, A.y - B.y);
}

struct Line { //直线定义
	Point v, p;
	Line() {}
	Line(Point v, Point p): v(v), p(p) {}
} line[5010];

int ans[5010]; // 记录每一块的个数

// 计算叉积
int Cross(Point A, Point B) {
	return A.x * B.y - A.y * B.x;
}

void solve(int x, int y) {
	Point C = Point(x, y);
	bool ok = 0;
	for (int i = 1; i <= n; ++i) {
		if (Cross(C - line[i].p, line[i].v - line[i].p) < 0) {
			ans[i - 1]++;
			ok = 1;
			break;
		}
	}
	if (!ok ) ans[n]++;
}

int main() {
	while (~scanf("%d", &n) && n) {
		memset(ans, 0, sizeof(ans));
		scanf("%d%d%d%d%d", &m, &s.x, &s.y, &e.x, &e.y);
		for (int i = 1; i <= n; ++i) {
			int u, v;
			scanf("%d%d", &u, &v);
			line[i] = Line(Point(u, s.y), Point(v, e.y));
		}
		while (m--) {
			int x, y;
			scanf("%d%d", &x, &y);
			solve(x, y);
		}
		for (int i = 0; i <= n; ++i) {
			printf("%d: %d\n", i, ans[i]);
		}
		printf("\n");
	}
	return 0;
}

网上也有二分的做法,但这题直接找也可以过。

2

这两题都是一样的题目,就是有一些地方不一样,第二题给出的数据并没有按 x x x进行升序,所以首先需要排序,而且输出的地方也得改变。总的来说还是一样的。

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
#define endl '\n'
#define pb(a) push_back(a)
#define al(x) x.begin(), x.end()
#define sz(x) int(x.size())
#define IOS ios::sync_with_stdio(0);

int n, m;
struct Point {
	int x, y;
	Point(int x = 0, int y = 0): x(x), y(y) {}
} s, e;

Point operator - (const Point &A, const Point&B) {
	return Point(A.x - B.x, A.y - B.y);
}

struct Line { //直线定义
	Point v, p;
	Line() {}
	Line(Point v, Point p): v(v), p(p) {}
} line[5010];
int ans[5010];
bool operator == (const Point&A, const Point&B) {
	return A.x == B.x && A.y == B.y;
}
bool operator < (const Point&A, const Point&B) {
	if (A.x == B.x) return A.y < B.y;
	return A.x < B.x;
}
bool operator < (const Line &A, const Line&B) {
	if (A.v == B.v) return A.p < B.p;
	return A.v < B.v;
}


int Cross(Point A, Point B) {
	return A.x * B.y - A.y * B.x;
}

void solve(int x, int y) {
	Point C = Point(x, y);
	bool ok = 0;
	for (int i = 1; i <= n; ++i) {
		if (Cross(C - line[i].p, line[i].v - line[i].p) < 0) {
			ans[i - 1]++;
			ok = 1;
			break;
		}
	}
	if (!ok )ans[n]++;
}


int main() {
	while (~scanf("%d", &n) && n) {
		memset(ans, 0, sizeof(ans));
		scanf("%d%d%d%d%d", &m, &s.x, &s.y, &e.x, &e.y);
		for (int i = 1; i <= n; ++i) {
			int u, v;
			scanf("%d%d", &u, &v);
			line[i] = Line(Point(u, s.y), Point(v, e.y));
		}
		sort(line + 1, line + 1 + n);
		while (m--) {
			int x, y;
			scanf("%d%d", &x, &y);
			solve(x, y);
		}
		map<int, int>mp;
		for (int i = 0; i <= n; ++i) {
			mp[ans[i]]++;
		}
		map<int, int>::iterator it;
		cout << "Box" << endl;
		for (it = mp.begin(); it != mp.end(); ++it) {
			if (it ->first > 0)
				printf("%d: %d\n", it->first, it->second);
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值