TOYS-POJ2318 简单几何,叉乘

TOYS-POJ2318 简单几何,叉乘

题意

把一个盒子用m个隔板隔开,给定n个点的坐标,问每一个区域中各有多少个点

思路

利用向量叉乘判断点在线的哪一边,当叉乘小于等于0时,点在线的左边,否则在右边

题目中

You may assume that the cardboard partitions do not intersect each other and that they are specified in sorted order from left to right.

表示了输入的隔板是排好序的,我们可以利用二分查找来优化时间复杂度

要记得盒子的最左边和最右边也是隔板

代码

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<string>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<iomanip>
using namespace std;

#define getT long long t;scanf("%lld",&t)
#define getN long long n;scanf("%lld",&n)
#define for0n(n) getN; for(long long i=0;i<n;i++)
#define for1n(n) getN; for(long long i=1;i<=n;i++)
#define for0(n) for(long long i=0;i<n;i++)
#define for1(n) for(long long i=1;i<=n;i++)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)

typedef long long ll;
ll mod;
ll gcd(ll a, ll b) {
   
	return a % b == 0 ? b : gcd(b, a%b);
}

ll pow(ll a, ll b) {
   
	ll an = 1;
	ll base = a % mod;
	while (b > 0) {
   
		if (b & 1 != 0) {
   
			an = (an*base) % mod;
		}
		base = (base*base) % mod;
		b >>= 1;
	}
	return an % mod;
}

ll inv(ll a) {
   
	return pow(a, mod - 2);
}
ll lcm(ll a, ll b) {
   
	return a * b / gcd(a, b);
}
ll Arithmetic(ll a1, ll n, ll d) {
   
	ll sum = 0;
	sum += n * a1;
	sum %= mod;
	ll temp = n * (n - 1);
	temp %= mod;
	temp *= d;
	temp %= mod;
	temp /= 2;
	temp %= mod;
	return (sum + temp) % mod;
}
ll euler(ll n)
{
   
	ll ans = n;
	for (ll i = 2; i*i <= n; i++)
		if (n%i == 0)
		{
   
			ans = ans - ans / i;
			while (n%i == 0)
				n /= i;
		}
	if (n > 1)
		ans = ans - ans / n;
	return ans;
}
struct Point
{
   
	double x, y;
	Point(double a = 0, double b = 0) {
    x = a; y = b; }
	Point operator-(Point a)
	{
   
		return Point(x - a.x, y - a.y);
	}
	double operator*(Point a)
	{
   
		return x * a.y - y * a.x;
	}
};
Point pots[5005][2];
bool check(Point p, ll mid) {
   
	//true for right
	if ((p-pots[mid][1])*(pots[mid][0]-pots[mid][1])< 0) {
   
		return false;
	}
	return true;
}
ll ans[5005];
int main(void) {
   
	ll n, m;
	double lx, ly, rx, ry, thx, tlx;
	while (scanf("%lld%lld%lf%lf%lf%lf", &n, &m, &lx, &ly, &rx, &ry) == 6) {
   
		memset(ans, 0, sizeof(ans));
		pots[0][0] = Point(lx, ly);
		pots[0][1] = Point(lx, ry);
		pots[n][0] = Point(rx, ly);
		pots[n][1] = Point(rx, ry);
		for1(n) {
   
			scanf("%lf%lf", &thx, &tlx);
			pots[i][0] = Point(thx, ly);
			pots[i][1] = Point(tlx, ry);
		}
		for0(m) {
   
			scanf("%lf%lf", &thx, &tlx);
			ll left = 0, right = n;
			ll mid;
			Point pot = Point(thx, tlx);
			while (left<=right)
			{
   
				mid = (left + right) >> 1;
				if (check(pot, mid)) {
   
					left = mid+1;
				}
				else {
   
					right = mid-1;
				}
			}
			ans[left]++;
		}
		for1(n+1) {
   
			printf("%lld: %lld\n", i-1, ans[i]);
		}
		printf("\n");
	}
	return 0;
}
全部评论

相关推荐

10-15 15:00
潍坊学院 golang
跨考小白:这又不是官方
投递拼多多集团-PDD等公司10个岗位
点赞 评论 收藏
分享
11-29 11:21
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务