TOYS(计算几何--点与线的关系(叉积+二分(二分之前必须排序!!!)))

WOFOST模型参数调优实战:从‘跑不通’到‘预测准’的避坑指南 本文详细介绍了WOFOST模型参数调优的实战技巧,帮助用户从模型报错诊断到参数敏感性分析,再到土壤-作物参数联调策略,逐步解决‘跑不通’到‘预测准’的问题。通过Python和PCSE工具,提供参数扰动测试模板和关键参数优先级矩阵,助力作物模型的高效校准与预测。 阅读详情


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


TOYS
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 12022 Accepted: 5799

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, but John is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for John to find his favorite toys. 

John's parents came up with the following idea. They put cardboard partitions into the box. Even if John keeps throwing his toys into the box, at least toys that get thrown into different bins stay separated. The following diagram shows a top view of an example toy box. 
 
For this problem, you are asked to determine how many toys fall into each partition as John throws them into the toy box.

Input

The input file contains one or more problems. The first line of a problem consists of six integers, n m x1 y1 x2 y2. The number of cardboard partitions is n (0 < n <= 5000) and the number of toys is m (0 < m <= 5000). The coordinates of the upper-left corner and the lower-right corner of the box are (x1,y1) and (x2,y2), respectively. The following n lines contain two integers per line, Ui Li, indicating that the ends of the i-th cardboard partition is at the coordinates (Ui,y1) and (Li,y2). You may assume that the cardboard partitions do not intersect each other and that they are specified in sorted order from left to right. The next m lines contain two integers per line, Xj Yj specifying where the j-th toy has landed in the box. The order of the toy locations is random. You may assume that no toy will land exactly on a cardboard partition or outside the boundary of the box. The input is terminated by a line consisting of a single 0.

Output

The output for each problem will be one line for each separate bin in the toy box. For each bin, print its bin number, followed by a colon and one space, followed by the number of toys thrown into that bin. Bins are numbered from 0 (the leftmost bin) to n (the rightmost bin). Separate the output of different problems by a single blank line.

Sample Input

5 6 0 10 60 0
3 1
4 3
6 8
10 10
15 30
1 5
2 1
2 8
5 5
40 10
7 9
4 10 0 10 100 0
20 20
40 40
60 60
80 80
 5 10
15 10
25 10
35 10
45 10
55 10
65 10
75 10
85 10
95 10
0

Sample Output

0: 2
1: 1
2: 1
3: 1
4: 0
5: 1

0: 2
1: 2
2: 2
3: 2
4: 2

Hint

As the example illustrates, toys that fall on the boundary of the box are "in" the box.

Source


题意:一个矩形,有被若干直线分成N个格子,给出一个点的坐标,问你该点位于哪个点中。
编程思想:其实就是点在凸四边形内的判断,若利用叉积的性质,可以二分求解。

AC code1:

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#define LL long long
#define MAXN 1000010 
using namespace std;
struct point{
	double x;
	double y;
};
struct v{
	point s;
	point e;
}line[MAXN];
double multi(point p1,point p2,point p0)
{
	return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int cnt[MAXN];
int n,m;
point toy[MAXN];
void Bsearch(point p)
{
	int l=1;
	int r=n;
	while(l<r)
	{
		int mid=(l+r)/2;
		if(multi(p,line[mid].s,line[mid].e)<0)//on left
			r=mid-1;
		else
			l=mid+1;
	}
	if(multi(p,line[l].s,line[l].e)<0)//on left
	{
		cnt[l-1]++;
	}
	else
	{
		cnt[l]++;
	}
}
int main()
{
	//freopen("D:\in.txt","r",stdin);
	double x1,y1,x2,y2,Ui,Li;
	int i,j,cas;
	cas=0;
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0)
			break;
		cas++;
		scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2);
		for(i=1;i<=n;i++)
		{
			scanf("%lf%lf",&Ui,&Li);
			line[i].s.x=Ui;
			line[i].s.y=y1;
			line[i].e.x=Li;
			line[i].e.y=y2;
		}
		memset(cnt,0,sizeof(cnt));
		for(j=1;j<=m;j++)
		{
			scanf("%lf%lf",&toy[j].x,&toy[j].y);
			Bsearch(toy[j]);
		}
		if(cas>1)
			printf("\n");
		for(i=0;i<=n;i++)
		{
			printf("%d: %d\n",i,cnt[i]);
		}
	}
	return 0;
 } 


AC code2:

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#define LL long long
#define MAXN 1000010 
using namespace std;
struct point{
	double x;
	double y;
};
struct v{
	point s;
	point e;
}line[MAXN];
double multi(point p1,point p2,point p0)
{
	return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int cnt[MAXN];
int n,m;
point toy[MAXN];
void Bsearch(point p)
{
	int l=0;
	int r=n+1;
	while(l<r)
	{
		int mid=(l+r)/2;
		if(multi(p,line[mid].s,line[mid].e)<0)//on left
			r=mid-1;
		else
			l=mid+1;
	}
	if(multi(p,line[l].s,line[l].e)<0)//on left
	{
		cnt[l-1]++;
	}
	else
	{
		cnt[l]++;
	}
}
int main()
{
	//freopen("D:\in.txt","r",stdin);
	double x1,y1,x2,y2,Ui,Li;
	int i,j,cas;
	cas=0;
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0)
			break;
		cas++;
		scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2);
		line[0].s.x=x1;
		line[0].s.y=y1;
		line[0].e.x=x1;
		line[0].e.y=y2;
		line[n+1].s.x=x2;
		line[n+1].s.y=y1;
		line[n+1].e.x=x2;
		line[n+1].e.y=y2;
		for(i=1;i<=n;i++)
		{
			scanf("%lf%lf",&Ui,&Li);
			line[i].s.x=Ui;
			line[i].s.y=y1;
			line[i].e.x=Li;
			line[i].e.y=y2;
		}
		memset(cnt,0,sizeof(cnt));
		for(j=1;j<=m;j++)
		{
			scanf("%lf%lf",&toy[j].x,&toy[j].y);
			Bsearch(toy[j]);
		}
		if(cas>1)
			printf("\n");
		for(i=0;i<=n;i++)
		{
			printf("%d: %d\n",i,cnt[i]);
		}
	}
	return 0;
 } 


下面是该题目的升级版本:


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


Toy Storage
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4597 Accepted: 2723

Description

Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunately, Reza is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for Reza to find his favorite toys anymore. 
Reza's parents came up with the following idea. They put cardboard partitions into the box. Even if Reza keeps throwing his toys into the box, at least toys that get thrown into different partitions stay separate. The box looks like this from the top: 

We want for each positive integer t, such that there exists a partition with t toys, determine how many partitions have t, toys.

Input

The input consists of a number of cases. The first line consists of six integers n, m, x1, y1, x2, y2. The number of cardboards to form the partitions is n (0 < n <= 1000) and the number of toys is given in m (0 < m <= 1000). The coordinates of the upper-left corner and the lower-right corner of the box are (x1, y1) and (x2, y2), respectively. The following n lines each consists of two integers Ui Li, indicating that the ends of the ith cardboard is at the coordinates (Ui, y1) and (Li, y2). You may assume that the cardboards do not intersect with each other. The next m lines each consists of two integers Xi Yi specifying where the ith toy has landed in the box. You may assume that no toy will land on a cardboard. 

A line consisting of a single 0 terminates the input.

Output

For each box, first provide a header stating "Box" on a line of its own. After that, there will be one line of output per count (t > 0) of toys in a partition. The value t will be followed by a colon and a space, followed the number of partitions containing t toys. Output will be sorted in ascending order of t for each box.

Sample Input

4 10 0 10 100 0
20 20
80 80
60 60
40 40
5 10
15 10
95 10
25 10
65 10
75 10
35 10
45 10
55 10
85 10
5 6 0 10 60 0
4 3
15 30
3 1
6 8
10 10
2 1
2 8
1 5
5 5
40 10
7 9
0

Sample Output

Box
2: 5
Box
1: 4
2: 1

Source



编程思想:现在给的线段不是按从左到右来的,是无序的,需要自己排序,输出也比较难,这里用到了离散化的思想来存储输出数据。


AC code:

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#define LL long long
#define MAXN 1000010 
using namespace std;
struct point{
	double x;
	double y;
};
struct v{
	point s;
	point e;
}line[MAXN];
bool cmp(v line1,v line2)
{
	return line1.s.x<line2.s.x;
}
double multi(point p1,point p2,point p0)
{
	return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

int cnt[MAXN];
vector<int>vec[MAXN];
int n,m;
point toy[MAXN];
void Bsearch(point p)
{
	int l=0;
	int r=n+1;
	while(l<r)
	{
		int mid=(l+r)/2;
		if(multi(p,line[mid].s,line[mid].e)<0)//on left
			r=mid-1;
		else
			l=mid+1;
	}
	if(multi(p,line[l].s,line[l].e)<0)//on left
	{
		cnt[l-1]++;
	}
	else
	{
		cnt[l]++;
	}
}
int main()
{
	//freopen("D:\in.txt","r",stdin);
	double x1,y1,x2,y2,Ui,Li;
	int i,j,cas,t;
	cas=0;
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0)
			break;
		cas++;
		scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2);
		line[0].s.x=x1;
		line[0].s.y=y1;
		line[0].e.x=x1;
		line[0].e.y=y2;
		line[n+1].s.x=x2;
		line[n+1].s.y=y1;
		line[n+1].e.x=x2;
		line[n+1].e.y=y2;
		for(i=1;i<=n;i++)
		{
			scanf("%lf%lf",&Ui,&Li);
			line[i].s.x=Ui;
			line[i].s.y=y1;
			line[i].e.x=Li;
			line[i].e.y=y2;
		}
		memset(cnt,0,sizeof(cnt));
		sort(line,line+n+2,cmp);
		for(j=1;j<=m;j++)
		{
			scanf("%lf%lf",&toy[j].x,&toy[j].y);
			Bsearch(toy[j]);
		}
		/*if(cas>1)
			printf("\n");*/
		for(i=0;i<=n;i++)
			vec[i].clear();
		for(i=0;i<=n;i++)
		{
			vec[cnt[i]].push_back(i);
		}
		printf("Box\n");
		for(t=1;t<=m;t++)
		{
			int size=vec[t].size();
			if(size>0)
			{
				printf("%d: %d\n",t,size);
			}
		}
	}
	return 0;
 } 


【OpenClaw 故障排查完全指南】 OpenClaw 故障排查指南摘要 本指南针对OpenClaw 2026.2.26版本常见问题提供解决方案,适用于Windows/Linux/macOS平台。 核心问题及修复方案: 飞书插件连接失败:删除冲突插件或重装OpenClaw Ollama认证错误:设置环境变量OLLAMA_API_KEY="ollama-local" 配置警告:可安全忽略的安全扫描提示 端口占用:查找并终止占用18789端口的进程 插件重复加载:清理重复插件目录 快速修复: 提供一键修复PowerShell脚本 阅读详情

相关推荐

IBM V3700更换控制器2(503错误)

正常的node1 ,机器代码版本:7.5.0.3, 机器代码构建:115.51.1509171734000。耐心等待后,刷新,看到node2,状态变成 节点状态:服务,错误:656,版本和node1一样了。新换的node2 ,机器代码版本:7.2.0.5, 机器代码构建:87.8.1404221000。耐心等待,刷新,node2,状态看到了,节点状态:活动,错误 724 1 1 0。拔出故障的控制,把内存和HBA卡,电池换到新的控制器。新的控制还是报错无法使用,节点状态:服务 错误:503。

klyshao的博客 4446

放玩具,用几何方法求解+二分

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,

Conqueror_demons的博客 509

django安装mysqlclient (Windows下正确安装mysqlclient)

由于本人也是初学Python和django。本人使用的Python版本是3.7所以连接MySQL数据库需要安装数据驱动,对应Python3的数据驱动是mysqlclient。因此本人习惯性的使用命令pip install mysqlclient,结果安装失败 (venv) C:\Users\songlihui\PycharmProjects\autotest&gt;pip install my...

songlh1234的博客 2万+

A - TOYS(POJ - 2318) 计算几何的一道基础题

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

叶梨子 865

abaqus wcm插件下载_Abaqus随机短纤维增强复合材料建模插件

文末附Abaqus随机短纤维增强复合材料建模插件下载链接前言短纤维增强复合材料相对于长纤维增强复合材料来说更容易制备,且短纤维的填充性能略有改善,可制造复杂产品,广泛应用于车辆、航空航天、建筑等行业,对短纤维增强复合材料进行细观力学分析,可对其宏观力学性能进行预测、分析破坏机理等。接下来对随机短纤维增强复合材料建模插件进行介绍。NO.1 【算法简介】难点一:此插件核心在于判断生成的纤维是...

weixin_39974882的博客 2833

手机如何高效辅助林业调查

手机如何高效辅助林业调查 很多林业工作者将栅格类型的地形图处理后导入平板电脑,在林地使用平板电脑加载小班矢量图,辅助林业调查。但是,如果没有地形图底图,这种方法可能就有难度,而且最重要的是,大部分林地工作者没有能力配置平板电脑。大凡林业基层工作者在林地调查时,除调查记录本等部分纸质材料外,都必然携带手机和水杯,此外其他设备已经成为累赘,尤其是炎热的夏季在杂灌丛生的林地进行调查作业。因此,使用手机...

xu780820的博客 3077

POJ 2318 - TOYS (计算几何)

POJ 2318- 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 ...

weixin_45691686的博客 340

即插即用篇 | 手把手教你 YOLOv11 添加注意力机制 | 适用于【检测任务】【分类任务】【分割任务】【关键点任务】| 20+ 种全打通!

即插即用篇 | 手把手教你 YOLOv11 添加注意力机制 | 适用于【检测任务】【分类任务】【分割任务】【关键点任务】| 20+ 种全打通!

Coding... 2830

poj 2318 TOYS(叉积的应用)

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7307   Accepted: 3453 Description Calculate the number of toys that land in each bin of a partitioned

志当存高远 849

撸毛笔记-朴朴超市 v1.01

朴朴超市 v1.01脚本地址:自动签到和组队赚朴分, 朴分可以抵扣50%的金额组队瓜分需要4人成团, 每个号每天只能开团和入队各一次每天跑一两次即可使用扫码或自己想办法捉refreshToken扫码获取token: (先微信进入朴朴超市小程序并登录注册过账号再扫码)把获取的refreshToken和备注(非必须)填到文件pupuCookie.txt里(第一次运行会自动创建), 多账号换行或&或@隔开。

xiaoshun007的专栏 1804

OpenStack ussuri 私有云平台搭建企业级实战

openstack是一个云操作系统,这个操作系统控制着数据中心中的计算,存储和网络资源。所有这些资源的管理都是通过API来来实现的,并且管理资源都有相应的认证机制。在openstack中有一个叫做dashboard的仪表盘,这个仪表盘通过web界面可以管理这些在DC中的资源。除了能提供IaaS功能外,你还可以使用orchestration,错误管理和服务管理等服务来确保应用的高可用性。

悦分享 2337

计算几何-点、线、面、形

1.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...

weixin_30765319的博客 194

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

poj 2318 Toys(叉积判点在线段的哪一侧)

【题目大意】:给出一个矩形的左上角的点(x1,y1)以及右下角的点(x2,y2),并给出n条线段,线段表示为(l,y1),(r,y2),用于将矩形切割成n+1块,分别标记为0到n。再给出m个点,求举行的每一块分别包含几个点。 【解题思路】:已知给出三个点a,b,c,通过叉积可以判断c在线段a,b的哪一侧,若叉积小于0,则在线段左侧,等于0,三点共线,大于0,在线段右侧。所以对于每一个点只需要找到

773

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

POJ 2318 TOYS 叉积+二分

http://poj.org/problem?id=2318 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 p...

xiji333的博客 220

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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 Joh...

xxxle的博客 455

计算几何——TOYS(叉积判点与线的位置)

题目链接: poj.org/problem?id=2318 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 finishe...

mumei的博客 333

POJ—2318 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 ga...

slark_的博客 495

计算几何—Toy 叉积判断点与线段的相对位置

 A - TOYS Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Calculate t

Dream_going的博客 691

POJ 2318 TOYS

POJ 2318 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 playin...

旺崽的博客 2904

POJ - 2318 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 Joh...

slass-mot-odet 571

【算法杂谈】计算几何题目推荐

//第一期 计算几何题的特点与做题要领: 1.大部分不会很难,少部分题目思路很巧妙 2.做计算几何题目,模板很重要,模板必须高度可靠。 3.要注意代码的组织,因为计算几何的题目很容易上两百行代码,里面大部分是模板。如果代码一片混乱,那么会严重影响做题正确率。 4.注意精度控制。 5.能用整数的地方尽量用整数,要想到扩大数据的方法(扩大一倍,或扩大sqrt2)。因为整

1385

POJ - 2398 -- Toy Storage【叉积+二分】

Toy Storage Description Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunately, Reza is rebellious and obeys his parents by simply th

qq_45249273的博客 248

POJ 计算几何入门题目推荐(转)

POJ 计算几何入门题目推荐(转)       其实也谈不上推荐,只是自己做过的题目而已,甚至有的题目尚未AC,让在挣扎中。之所以推荐计算几何题,是因为,本人感觉ACM各种算法中计算几何算是比较实际的算法,在很多领域有着重要的用途(例如本人的专业,GIS)。以后若有机会,我会补充、完善这个列表。 计算几何题的特点与做题要领: 1.大部分不会很难,少部分题目思路很巧妙 2.做计算几何

阿蒋的专栏 1464

【叉积性质】POJ 2318 TOYS && POJ 2398 Toy Storage

http://acm.pku.edu.cn/JudgeOnline/problem?id=2318 //#pragma comment(linker, "/STACK:102400000,102400000") #include #include #include #include #include #include #include #include #include

学无纸巾 315

【POJ - 2398 Toy Storage】 点与线的关系

B - Toy Storage Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunate

于心有愧丶 326

poj 2318 TOYS & poj 2398 Toy Storage (叉积)

链接:poj 2318 题意:有一个矩形盒子,盒子里有一些木块线段。而且这些线段坐标是依照顺序给出的。 有n条线段,把盒子分层了n+1个区域,然后有m个玩具。这m个玩具的坐标是已知的,问最后每一个区域有多少个玩具 分析:从左往右。直到推断玩具是否在线段的逆时针方向为止。这个就须要...

aoe41606的博客 134
上一篇: hdu5386 Cover(逆向思维+模拟)
下一篇: ACM经典算法之数学问题模板
林下的码路
博客等级 码龄12年 295粉丝 506原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值