POJ TOYS(叉积的妙用+二分答案)
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, 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. 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 Sample Output 0: 2 Hint As the example illustrates, toys that fall on the boundary of the box are "in" the box.
Source |
[Submit] [Go Back] [Status] [Discuss]
这道题是我为数不多的能想出思路来的题,
它给的隔板都是排好序的
所以我们可以二分一个答案,
然后判断这个点在中点的哪个地方,
注意在这个地方有一个小技巧,
我们可以把点看成向量的相对向量,,,
大概就是这个意思,不是特别严谨,
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
inline void read(int &n)
{
char c=getchar();bool flag=;n=;
while(c<''||c>'') c=='-'?flag=,c=getchar():c=getchar();
while(c>=''&&c<='') n=n*+(c-),c=getchar();if(flag==)n=-n;
}
int n,m,x1,yy1,x2,y2;
struct node
{
int x,y;
}point[MAXN];
struct Line
{
node a,b;
}line[MAXN];
int cross(node a,node b,node c)
{
return (c.x-a.x)*(c.y-b.y)-(c.x-b.x)*(c.y-a.y);
}
int ans[MAXN];
void calc(int num)
{
int l=,r=n-;
while(l<r)
{
int mid=(l+r)>>;
if(cross(point[num],line[mid].a,line[mid].b)>) l=mid+;
else r=mid;
}
if(cross(point[num],line[l].a,line[l].b)<)
ans[l]++;
else ans[l+]++; }
int main()
{
while(scanf("%d",&n)&&n)
{
scanf("%d%d%d%d%d",&m,&x1,&yy1,&x2,&y2);
for(int i=;i<n;i++)
{
int p,q;read(p);read(q);
line[i].a.x=p;
line[i].a.y=yy1;
line[i].b.x=q;
line[i].b.y=y2;
}
memset(ans,,sizeof(ans));
for(int i=;i<m;i++)
{
int p,q;read(p);read(q);
point[i].x=p;
point[i].y=q;
}
for(int i=;i<m;i++)
calc(i); for(int i=;i<=n;i++)
printf("%d: %d\n",i,ans[i]);
printf("\n");
}
return ;
}
POJ TOYS(叉积的妙用+二分答案)的更多相关文章
- POJ 1226 Substrings(后缀数组+二分答案)
[题目链接] http://poj.org/problem?id=1226 [题目大意] 求在每个给出字符串中出现的最长子串的长度,字符串在出现的时候可以是倒置的. [题解] 我们将每个字符串倒置,用 ...
- poj 3294 Life Forms - 后缀数组 - 二分答案
题目传送门 传送门I 传送门II 题目大意 给定$n$个串,询问所有出现在严格大于$\frac{n}{2}$个串的最长串.不存在输出'?' 用奇怪的字符把它们连接起来.然后求sa,hei,二分答案,按 ...
- poj 3415 Common Substrings - 后缀数组 - 二分答案 - 单调栈
题目传送门 传送点I 传送点II 题目大意 给定串$A, B$,求$A$和$B$长度大于等于$k$的公共子串的数量. 根据常用套路,用一个奇怪的字符把$A$,$B$连接起来,然后二分答案,然后按mid ...
- Poj 1743 Musical Theme(后缀数组+二分答案)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...
- Poj 3261 Milk Patterns(后缀数组+二分答案)
Milk Patterns Case Time Limit: 2000MS Description Farmer John has noticed that the quality of milk g ...
- POJ 2018 Best Cow Fences (二分答案构造新权值 or 斜率优化)
$ POJ~2018~Best~Cow~ Fences $(二分答案构造新权值) $ solution: $ 题目大意: 给定正整数数列 $ A $ ,求一个平均数最大的长度不小于 $ L $ 的子段 ...
- poj 1275 Cashier Employment - 差分约束 - 二分答案
A supermarket in Tehran is open 24 hours a day every day and needs a number of cashiers to fit its n ...
- POJ 2018 Best Cow Fences(二分答案)
题目链接:http://poj.org/problem?id=2018 题目给了一些农场,每个农场有一定数量的奶牛,农场依次排列,问选择至少连续排列F个农场的序列,使这些农场的奶牛平均数量最大,求最大 ...
- [poj 2773] Happy 2006 解题报告 (二分答案+容斥原理)
题目链接:http://poj.org/problem?id=2773 题目大意: 给出两个数m,k,要求求出从1开始与m互质的第k个数 题解: #include<algorithm> # ...
随机推荐
- Leetcode_num1_Single Number
好久没有做题啦.从今天開始刷Leetcode的题.希望坚持的时间能长一点. 先从ac率最高的Single Number開始吧. 题目: Given an array of integers, ever ...
- 使用docker搭建hadoop分布式集群
使用docker搭建部署hadoop分布式集群 在网上找了非常长时间都没有找到使用docker搭建hadoop分布式集群的文档,没办法,仅仅能自己写一个了. 一:环境准备: 1:首先要有一个Cento ...
- Loopback測试软件AX1用户手冊 V3.1
点击:AX1 软件下载 1. 什么是AX1 AX1程序是基于windows的PC程序,用来评估 iinChip™的性能,也即是wiznet的硬件TCP/IP芯片. AX1通过网络与iinChip™评估 ...
- jquery outerHeight方法 outerWidth方法 获取元素实际宽度高度
曾经写代码中,每当须要获取元素的实际"宽度"(这里的宽度是指元素宽度加上其边距)时,都须要用元素宽度加上margin值才行,今天发现一个叫outerWidth(options)的方 ...
- [iOS]字符串转字典
有点时候,我们json中有post请求的网址,这个时候我们须要把网址字符串转换成body体 字典 放在post请求中 NSString *body = [self.url_C_ component ...
- Stop being a perfectionist
节选自 7 Things You Need To Stop Doing To Be More Productive, Backed By Science “We found that perfecti ...
- Sublime Text 3 注册码 激活码 版本号 Build 3143
—– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA ...
- Linux部署之批量自动安装系统之DHCP篇
1. 安装:yum install dhcp 2. Ip配置信息 3. Dhcp配置文件如下 4. 配置完后检查语法是否错误 ...
- vector迭代器
https://www.cnblogs.com/quant-lee/p/6618829.html
- 动态Axios配置
推荐使用Vue-cli工具来创建和管理项目,就算刚开始不熟悉,用着用着便可知晓其中的奥妙.前一段时间官方所推荐的数据请求插件还是Vue-resource,但现在已经变了,变成了Axios,不用知道为什 ...