UVA 1398 - 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 meteors, each moving in uniform linear motion; the meteor mi moves along the trajectory pi + t×vi over time t , where t is a non-negative real value, pi is the starting point of mi and vi is the velocity of mi . The point pi = (xi, yi) is represented by X -coordinate xi and Y -coordinate yi in the (X, Y) -plane, and the velocity vi = (ai, bi) is a non-zero vector with two components ai and bi in the (X, Y) -plane. For example, if pi = (1, 3) and vi = (-2, 5) , then the meteor mi will be at the position (0, 5.5) at time t = 0.5 because pi + t×vi = (1, 3) + 0.5×(-2, 5) = (0, 5.5) . The telescope has a rectangular frame with the lower-left corner (0, 0) and the upper-right corner (w, h) . 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, p2, p3, p4 , and p5 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.

\epsfbox{p3905.eps}

Input 

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


有n个流星,各自以不同速度朝不同方向运动,求所有时刻,同时在矩形区域中的最大流星数,边界上的不算。


所有流星,能求出进入矩形和离开矩形区域的时间。这个时间段可以在数轴上表示出来,只要求出数轴上的点被不同时间段重叠次数最大的值即可。

从左往右扫描,遇到时间起点计数++,遇到时间终点计数--,维护此过程中的最大计数。

注意这里有一个问题,对于终点和起点重合的时间点t,先--后++,因为t时刻计算完的计数值,实际上是刚过t的时刻在矩阵中的流星数。终点的应当减去,起点的加上,如果先加后减,则把将要离开的流星也算上了,会导致max值更新,导致错误答案。


对于除法,若只需计算后的相对大小关系,为提高精度避免浮点运算,可以将被除数乘以所有除数的最小公倍数(应在整数范围内)。

 

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <algorithm>

using namespace std;

const int MAX = 100000 + 5;
const int MAX_T = 1000000000 + 5;

struct point
{
    int t,type;//t = -1 表示是下界 1表示上界 
    
    bool operator <(const point& p)const
    {
        return t < p.t || (t == p.t && type > p.type);//某一时间点在边界上的 不算 x实际 
    }
}p[MAX * 2];
 

int x,y,a,b,min_t,max_t; 

void caculate_t(int edg, int pos,int v)
{
    if(v == 0)
    {
        if(pos <= 0 || pos >= edg)max_t = -1;//注意 边界不能算 
        return;
    } 
    
    int t1 = max(0, -2520 / v * pos);
    int t2 = max(0, 2520 / v * (edg - pos));
    //printf("pos :%d  edg:%d v:%d t1:%d  t2:%d\n",pos,edg,v,t1,t2);
    min_t = max(min_t, min(t1, t2));
    max_t = min(max_t, max(t1, t2)); 
    
    //printf("pos :%d  edg:%d v:%d l:%d  r:%d======\n",pos,edg,v,min_t,max_t);
}

void get_result(int n)
{
     int m = 0,cnt = 0;
     for(int i = 0; i < n; i++)
     {
         if(p[i].type < 0)m = max(++cnt, m);
         else cnt--;
         
        
     }
     printf("%d\n",m);
}

int main(int argc, char *argv[])
{
    
    int t,w,h,n;
    
    
    scanf("%d",&t);
    
    while(t--)
    {
        scanf("%d%d%d",&w,&h,&n);
        int cnt = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d%d%d",&x,&y,&a,&b);
        
            min_t = 0; max_t = MAX_T;
            caculate_t(w, x, a);
            caculate_t(h, y, b);
        
            if(max_t > 0 && max_t > min_t)
            {
                p[cnt++] = (point){min_t, -1};
                p[cnt++] = (point){max_t, 1};
            }
        }
        
        sort(p, p + cnt);
        get_result(cnt);
        
        
        
    }
    
    //system("PAUSE");
    return EXIT_SUCCESS;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值