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> # ...
随机推荐
- 大话设计模式C++实现-第17章-适配器模式
一.UML图 关键词:Client须要Request()函数,Adaptee提供的是SpecificRequest()函数,Adapter提供一个Request()函数将Adaptee和Specifi ...
- Win7上从硬盘安装Debian
近期一直想将笔记本搞成Win7+Debian双系统.由于无论怎样优化,2G内存的Win7笔记本上开个Linux虚拟机都实在吃力. 经过一段时间的资料搜索.并阅读Debian官方的安装文档,今天最终实现 ...
- zzulioj--1790-- 弹珠游戏(数学水题!)
弹珠游戏 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 14 Solved: 10 SubmitStatusWeb Board Descriptio ...
- Linux常用命令之rpm安装命令
转自:http://www.cnblogs.com/datasyman/p/6942557.html 在 Linux 操作系统下,几乎所有的软件均通过RPM 进行安装.卸载及管理等操作.RPM 的全称 ...
- Install Rails on ubuntu 12.04 LTS
There are basically there ways to install Rails development environment on your ubuntu linux system, ...
- SPSS学习小记
2013年1月8日 最近一直在SPSS中处理数据,涉及到函数部分,不是太懂,特记录于此,以便翻阅. SPSS判断字符变量中是否含有某字符串的表示方式: (INDEX(url,'ad')>0 ...
- 常用css框架 Sass/Less
Bootstrap less/sass Sass (Syntactically Awesome Stylesheets)是一种动态样式语言,Sass语法属于缩排语法,比css比多出好些功能(如变量.嵌 ...
- P2742 [USACO5.1]圈奶牛Fencing the Cows
题目描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. 输入输出格式 输入 ...
- matlab张量工具初步
最近从桑迪亚实验室下载了张量工具包.但是不太会用. 很多网上的方法, addpath(pwd) cd met; addpath(pwd) savepath M=ones(4,3,2); X=tenso ...
- Android ScrollView 滚动到顶部
有时候使用ScrollView,里边控件比较多的时候,打开界面,会滑到底部,如果要设置滑动到顶部,一般有两种方法 1.使用fullScrol(),scrollView.fullScroll(Scrol ...