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> # ...
随机推荐
- android的架构图
1.Applications 该层是Android应用程序层. 每一个应用必须利用android系统设计的应用框架(application framework)开发. 眼下的开发环境是eclipse ...
- javascript 获取指定范围随机数
<script type="text/javascript"> function GetRandomNum(Min,Max){ var Range = Max - Mi ...
- 【翻译自mos文章】怎么正确的计算一个ip地址的subnet id?
怎么正确的计算一个ip地址的subnet id? 来源于: How to calculate the correct subnet for an interface (文档 ID 1059759.1) ...
- C/S和B/S交互
近期一直在做C/S的项目,每天都超忙,抽个时间写篇博客,之前一直做C/S项目就是各种窗口.各种控件,拖来拖去,然后点进去写方法,做BS的时候呢,由于一直使用的是mvc,所以就是常常手写代码.或者拖引用 ...
- sicily 1146 采药 (动规)
打代码不走心会掉坑里的.. 下边是代码: //1146.采药 //t表示总时间 //m表示草药数 //w表示采药时间 //v表示草药价值 #include <iostream> using ...
- 页面的URL分析----window.location.href
window.location是页面的位置对象window.location.href是 location的一个属性值,并且它是location的默认属性. window.location直接赋值一个 ...
- POJ 1979 Red and Black (简单dfs)
题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define IN ...
- JavaScript在不同环境下的全局对象
Node.js 环境下,全局的对象是 global. 浏览器下 window === self 而不是 global,今天才发现的,我惊呆了!
- 对称平方数(to_string函数,stoi函数真香)
题目描述 打印所有不超过n(n<256)的,其平方具有对称性质的数.如11*11=121. 输入描述: 无 输出描述: 每行一个数,表示对称平方数. 示例1 输入 复制 无 输出 复制 无解题思 ...
- Python排序 插入排序
插入排序从前往后遍历数组的每一个元素,对每一位元素都将其插入到已经有序的部分数组中,所以插入排序的要点就是找出要插入元素在已经有序的部分中的位置,同时,由于插入排序采用原地排序(in-place)算法 ...