TOYS-POJ2318

本题主要是确定给定的点在那块区域。原题给出n条直线,将长方形分为n+1快区域。我们可以对每个给定的点来判断它在那块区域,判段方法可以根据点与直线的位置关系,具体如下,对于点(x0,y0)和直线ax+by+c=0(a>0):

1)若a*x0+b*y0+c=0,则点在直线上。

2)若a*x0+b*y0+c>0,则点在直线右侧。

3)若a*x0+b*y0+c<0,则点在直线左侧。

顺序查找点的位置必然会耗费大量的时间。所以采用二分方法,找到点所在的区间,然后累计起来。下面代码将ax+by+c=0化简为x+(b/a)y+(c/a)=0,默认x的系数为1.

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<string>
#include<queue>
#include<cmath>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN = 5001;
struct Line{
	double b, c;   //x+b*y+c=0
};
Line L[MAXN];
int Count[MAXN];
int BinFind(int x,int y,int n);
int main(){
	int x1, y1, x2, y2,s,t,n,m,i,x,y,id,c=1;
	while (scanf("%d", &n)&&n){
		if (c != 1)  //控制下格式
			cout << endl;
		c++;
		scanf("%d%d%d%d%d", &m, &x1, &y1, &x2, &y2);
		memset(Count, 0, sizeof(Count));
		L[0].b = 0, L[0].c = 0 - x1;   //第一条直线应该是矩形左边的一条边
		for (i = 0; i < n; i++){
			scanf("%d%d", &s, &t);
			L[i+1].b = (t - s)*1.0/(y1 - y2);
			L[i+1].c = (y2*s - t*y1)*1.0 / (y1 - y2);
		}
		L[n + 1].b = 0, L[n + 1].c = 0 - x2;      //最后一条直线应该是矩形右边那条边
		for (i = 0; i < m; i++){
			scanf("%d%d", &x, &y);
			id = BinFind(x, y, n);
			Count[id]++;
		}
		for (i = 0; i <= n; i++)
			cout << i << ": " << Count[i] << endl;
	}
	return 0;
}
int BinFind(int x, int y,int n){
	int left = 0, right = n+1;
	while (left < right-1){    //找到的点在区间(L[left],L[rigth])之间
		int mid = (left + right) >> 1;
		if (x + L[mid].b*y + L[mid].c>0)  //点在直线右边
			left = mid;
		else
			right = mid;
	}
	return left;
}

  

时间: 2025-01-02 14:01:07

TOYS-POJ2318的相关文章

POJ2318 TOYS[叉积 二分]

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14433   Accepted: 6998 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away w

[POJ2318]TOYS

Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys in

POJ-2318 TOYS 计算几何 判断点在线段的位置

题目链接:https://cn.vjudge.net/problem/POJ-2318 题意 在一个矩形内,给出n-1条线段,把矩形分成n快四边形 问某些点在那个四边形内 思路 二分+判断点与位置关系 提交过程 WA*n x1和x2,y1和y2在复制的时候没分清(哭 WA 可能存在二分问题? AC 代码 #define PI 3.1415926 #include <cmath> #include <cstdio> #include <vector> #include &

poj2318 TOYS 【计算几何】【点和线的关系】

题目链接:http://poj.org/problem?id=2318 题目大意:给你n,m,x1,y1,x2,y2表示的分别是n个线,m个点,(x1,y1)表示的是矩形左上角那个点,(x2,y2)表示的是矩形右下角那个点. 然后给出n个线(L)的x坐标L.s.x,L.e.x,就相当于是给出了线的位置,下面给出m个点的坐标. 最后问n条线分成的n+1个区域内各有多少个点. 题目不是很难,不过与二分结合起来还是有点意思的. 注意叉乘的性质,在什么时候叉乘的结果会是小于0什么时候会是大于0. 如果是

[poj2318]TOYS(直线与点的位置关系)

解题关键:计算几何入门题,通过叉积判断. 两个向量的关系: P*Q>0,Q在P的逆时针方向: P*Q<0,Q在P的顺时针方向: P*Q==0,Q与P共线. #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<cmath> #include<iostream> using namespace std; typede

POJ2318 TOYS (计算几何)

### 题目链接 ### 题目大意: 给定一个矩形,这个矩形被 \(n\) 条线段(端点分别在矩形上下边界上)切割成 \(n+1\) 个块.再给你 \(m\) 个物品的坐标,问每个块中会有多少个物品,保证物品放置位置合法. 分析: 一个物品在一个块中时,他一定夹在两个分界线中间,且在左分界线的右边,右分界线的左边.故按图中那样,将物品连接某个边界下端,构成一个向量,再判断 蓝色向量 与 紫色向量 的位置关系,进行叉积即可.故二分找到第一个 物品向量 \(×\) 边界向量 为负的位置,即为该物品所

poj2318(计算几何)

1.题目(theme) TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10488   Accepted: 5031 Description Calculate the number of toys that land in each bin of a partitioned toy box.  Mom and dad have a problem - their child John never puts hi

POJ 2318 TOYS 叉积

题意: 给出一个矩形范围,给出n条线段,这n条线段一定与矩形上下边界相交且互不相交,将矩形分成n+1个划分.给出m个玩具的坐标.求每个划分放的玩具数,玩具保证不会在线段和左右边界上. 分析: 判断点是否在两条直线中间,利用叉积,如果在两条直线间,必定会有两个叉积一个小于0,一个大于0(不能把相乘小于0作为判断条件) #include <iostream> #include <cstdio> #include <cstring> using namespace std;

【POJ】2318 TOYS ——计算几何+二分

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10281   Accepted: 4924 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away w

TOYS

Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys in, but John i