LA 3905 Meteor 扫描线

The famous Korean internet company nhn has provided an internet-based photo
service which allows The famous Korean internet company users to directly take a
photo of an astronomical phenomenon in space by controlling a high-performance
telescope owned by nhn. A few days later, a meteoric shower, known as the
biggest one in this century, is expected. nhn has announced a photo competition
which awards the user who takes a photo containing as many meteors as possible
by using the photo service. For this competition, nhn provides the information
on the trajectories of the meteors at their web page in advance. The best way to
win is to compute the moment (the time) at which the telescope can catch the
maximum number of meteors.

You have n <tex2html_verbatim_mark>meteors, each
moving in uniform linear motion; the meteor mi <tex2html_verbatim_mark>moves
along the trajectory pi + t×vi<tex2html_verbatim_mark>over
time t <tex2html_verbatim_mark>,
where t <tex2html_verbatim_mark>is a
non-negative real value, pi <tex2html_verbatim_mark>is
the starting point of mi <tex2html_verbatim_mark>and vi <tex2html_verbatim_mark>is
the velocity ofmi <tex2html_verbatim_mark>.
The point pi =
(xiyi) <tex2html_verbatim_mark>is
represented by X <tex2html_verbatim_mark>-coordinate xi <tex2html_verbatim_mark>and Y <tex2html_verbatim_mark>-coordinate yi <tex2html_verbatim_mark>in
the (XY) <tex2html_verbatim_mark>-plane,
and the velocity vi =
(aibi) <tex2html_verbatim_mark>is
a non-zero vector with two components ai <tex2html_verbatim_mark>and bi <tex2html_verbatim_mark>in
the (XY) <tex2html_verbatim_mark>-plane.
For example, if pi = (1,
3) <tex2html_verbatim_mark>and vi = (-2,
5) <tex2html_verbatim_mark>, then the meteor mi <tex2html_verbatim_mark>will
be at the position (0, 5.5) at time t =
0.5 <tex2html_verbatim_mark>because pi + t×vi =
(1, 3) + 0.5×(-2, 5) = (0, 5.5) <tex2html_verbatim_mark>. The
telescope has a rectangular frame with the lower-left corner (0, 0) and the
upper-right corner (wh) <tex2html_verbatim_mark>.
Refer to Figure 1. A meteor is said to be in the telescope frame if the meteor
is in the interior of the frame (not on the boundary of the frame). For exam!
ple, in Figure 1, p2p3p4 <tex2html_verbatim_mark>,
and p5 <tex2html_verbatim_mark>cannot
be taken by the telescope at any time because they do not pass the interior of
the frame at all. You need to compute a time at which the number of meteors in
the frame of the telescope is maximized, and then output the maximum number of
meteors.

<tex2html_verbatim_mark>

Input


Your program is to read the input from standard input. The input consists
of T <tex2html_verbatim_mark>test cases.
The number of test cases T <tex2html_verbatim_mark>is given in
the first line of the input. Each test case starts with a line containing two
integers w<tex2html_verbatim_mark>and h <tex2html_verbatim_mark>(1wh100, 000) <tex2html_verbatim_mark>, the
width and height of the telescope frame, which are separated by single space.
The second line contains an integer n <tex2html_verbatim_mark>, the number
of input points (meteors), 1n100, 000 <tex2html_verbatim_mark>. Each of the
next n <tex2html_verbatim_mark>lines contain
four integers xiyiai <tex2html_verbatim_mark>,
and bi <tex2html_verbatim_mark>; (xiyi) <tex2html_verbatim_mark>is
the starting point pi <tex2html_verbatim_mark>and (aibi) <tex2html_verbatim_mark>is
the nonzero velocity vector vi <tex2html_verbatim_mark>of
the i <tex2html_verbatim_mark>-th
meteor; xi <tex2html_verbatim_mark>and yi <tex2html_verbatim_mark>are
integer values between -200,000 and 200,000, and ai <tex2html_verbatim_mark>and bi <tex2html_verbatim_mark>are
integer values between -10 and 10. Note that at least one of ai <tex2html_verbatim_mark>and bi <tex2html_verbatim_mark>is
not zero. These four values are separated by single spaces. We assume that all
starting points pi <tex2html_verbatim_mark>are
distinct.

Output


Your program is to write to standard output. Print the maximum number of
meteors which can be in the telescope frame at some moment.

Sample
Input


2
4 2
2
-1 1 1 -1
5 2 -1 -1
13 6
7
3 -2 1 3
6 9 -2 -1
8 0 -1 -1
7 6 10 0
11 -2 2 1
-2 4 6 -1
3 2 -5 -1

Sample
Output


1

2

题目大意:XOY坐标系上,给n个点(每个点的起始坐标跟速度),求在矩形区域最多能同时出现多少个点。

先把每个点在矩形上出现的时间段求出来(左右端点(左标记为0,右标记为1)push进vector容器,),二级排序。

从左只有处理各个端点,每遇到一个左端点,计数器加一;每遇到一个右端点,计数器减一。每次更新最大值。


 1 #include <iostream>
2 #include <cstdio>
3 #include <cmath>
4 #include <vector>
5 #include <algorithm>
6 using namespace std;
7
8 const double eps=1e-9;
9 double max(double a,double b){ return a-b>eps?a:b;}
10 double min(double a,double b){ return b-a>eps?a:b;}
11
12 struct node
13 {
14 double x;
15 int i;
16 node(double x=0,int i=0):x(x),i(i){}
17 bool operator<(const node &A)const{
18 if(fabs(A.x-x)>eps) return x<A.x;
19 else return i>A.i;
20 }
21 };
22 int w,h,n;
23 vector<node> v;
24
25 void update(int x,int a,int w,double &L,double &R)
26 {
27 if(a==0)
28 {
29 if(x<=0 || x>=w) R=L-1;
30 }
31 else if(a>0)
32 {
33 L=max(L,-(double)x/a);
34 R=min(R,(double)(w-x)/a);
35 }
36 else
37 {
38 L=max(L,(double)(w-x)/a);
39 R=min(R,-(double)x/a);
40 }
41 }
42
43 void Push()
44 {
45 int x,y,a,b;
46 scanf("%d %d %d %d",&x,&y,&a,&b);
47 double L=0,R=1e9;
48 update(x,a,w,L,R);
49 update(y,b,h,L,R);
50 if(R-L>eps)//经过矩形的起止时间
51 {
52 v.push_back(node(L,0));
53 v.push_back(node(R,1));
54 }
55 }
56
57 int main()
58 {
59 int T,i;
60 scanf("%d",&T);
61 while(T--)
62 {
63 v.clear();
64 scanf("%d %d %d",&w,&h,&n);
65 for(i=0;i<n;i++) Push();
66 sort(v.begin(),v.end());
67 int cnt=0,ans=0;
68 for(i=0;i<v.size();i++)
69 {
70 if(v[i].i==0) ans=max(ans,++cnt);
71 else cnt--;
72 }
73 printf("%d\n",ans);
74 }
75 return 0;
76 }

时间: 2024-10-08 10:08:58

LA 3905 Meteor 扫描线的相关文章

LA 3905 Meteor

给出一些点的初始位置(x, y)及速度(a, b)和一个矩形框,求能同时出现在矩形框内部的点数的最大值. 把每个点进出矩形的时刻分别看做一个事件,则每个点可能对应两个事件,进入事件和离开事件. 按这些事件的发生时间进行排序,然后逐个扫描,遇到进入事件cnt++,遇到离开事件--cnt,用ans记录cnt的最大值. 对时间相同情况的处理,当一个进入事件的时刻和离开事件的时刻相同时,应该先处理离开事件后处理进入事件. 因为速度(a, b)是-10~10的整数,所以乘以LCM(1,2,3,,,10)

LA - 3905 ——Meteor 流星

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=16454 1.对于每一颗流星而言,真正有意义的是它穿越矩形的有效时间,所以其实我们需要得到所有流星的有效时间 2.这样的话,原问题就转化更加具体的:某个时刻最多同时穿过多少个时间段? 解决方案: 将每一个时间区间,转化为两个事件:开始事件和结束事件.我们对所有事件按照时间排序,然后我们有一个初始化为0的tot变量计数,接着遍历这个序列,遇到开始事件就+1,遇到结束时间就

3905 - Meteor

The famous Korean internet company nhn has provided an internet-based photo service which allows The famous Korean internet company users to directly take a photo of an astronomical phenomenon in space by controlling a high-performance telescope owne

[2016-03-20][UVALive][3905][Meteor]

时间:2016-03-20 20:14:20 星期日 题目编号:[2016-03-20][UVALive][3905][Meteor] 题目大意:给定一定大小的相框,相框的右下角为坐标原点,给定n个星星的起始位置和运动方向,问相框内最多能捕捉到多少星星 方法: 把星星的运动方向分解成x和y两个方向,计算两个方向经过相框的时间,得到每个星星在相框的时间范围,题目就转换成,最多区间相交问题 计算时间: 计算直线上x为起点,a为增量,经过边界为w的时间对应的l和r 已知 0 < x + at < w

【UVALive】3905 Meteor(扫描线)

题目 传送门:QWQ 分析 扫描线搞一搞. 按左端点排序,左端点相同时按右端点排序. 如果是左端点就$ cnt++ $,否则$ cnt-- $ 统计一下$ Max $就行了 代码 #include <bits/stdc++.h> using namespace std; void update(int x,int a,int w,double& L,double& R){ if(a==0){ if(x<=0||x>=w) R=L-1; } else if(a>

D - Meteor

LA 3905 题意:有一个照相机相框,只有在相框内的点才可以被拍到,每一个点都有起始位置(x,y)和速度(a,b),问这个相框最多可以拍摄到多少个点 思路:拍摄肯定是一个瞬时性的动作,那么最终拍摄到点的多少肯定与时间的选取有关,因此可以吧所有的点在相框内出现的时间转化为一个区间. 最后进行遍历,遇到左区间加一,遇到右区间减一 #include <iostream> #include <algorithm> #include <cstdio> using namespa

计算几何题目分类

转载 一.基础题目 1.1 有固定算法的题目 A, 最近点对问题最近点对问题的算法基于扫描线算法.ZOJ 2107    Quoit Design    典型最近点对问题POJ    3714    Raid    变种最近点对问题 B,最小包围圆最小包围圆的算法是一种增量算法,期望是O(n).ZOJ    1450    Minimal Circle  HDU    3007    Buried memory C,旋转卡壳POJ 3608    Bridge Across Islands   

LA 4127 - The Sky is the Limit (离散化 扫描线 几何模板)

题目链接 非原创 原创地址:http://blog.csdn.net/jingqi814/article/details/26117241 题意:输入n座山的信息(山的横坐标,高度,山底宽度),计算他们的轮廓线, 即露出来的表面边长,有些山是重叠的不计.空白地带不计,每座山都是等腰三角形. 分析:大白书P414页. 求小山的总长度,用一些虚线将其离散化,分成一段一段的,特征点:山脚,山顶,交点.这样就能保 证相邻两个扫描点之间再无交点.然后一最上面的点就是分割点,维护上一个点lastp即可. 1

LA 3029 - City Game (简单扫描线)

题目链接 题意:给一个m*n的矩阵, 其中一些格子是空地(F), 其他是障碍(R).找一个全部由F 组成的面积最大的子矩阵, 输出其面积乘以3的结果. 思路:如果用枚举的方法,时间复杂度是O(m^2 n^2); 因为不但要枚举每一个点,而且矩阵的大小不知道,所以还要枚举长和宽. 可以通过枚举每一个点,求该点所能构成的最大矩形的边界. 分别用le[], rig[] 和 up[] 表示左边界,右边界和 上边界. 1 #include <iostream> 2 #include <cstrin