UVa 1398 Meteor 解题报告(扫描线)

来源:互联网 发布:mac mini 定制 编辑:程序博客网 时间:2024/05/16 02:47

1398 - Meteor

Time limit: 3.000 seconds


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 meteormi moves along the trajectorypi + t×vi over timet, where t is a non-negative real value,pi is the starting point ofmi and vi is the velocity ofmi. The pointpi = (xi,yi) is represented byX-coordinatexi andY-coordinateyi in the(X,Y)-plane, and the velocity vi = (ai,bi) is a non-zero vector with two componentsai and bi in the(X,Y)-plane. For example, if pi = (1, 3) andvi = (-2, 5), then the meteormi will be at the position (0, 5.5) at timet = 0.5 becausepi +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, andp5 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 integersw andh (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 integern, the number of input points (meteors),1$ \le$n$ \le$100, 000. Each of the next n lines contain four integersxi,yi, ai, andbi;(xi,yi) is the starting pointpi and(ai,bi) is the nonzero velocity vectorvi of thei-th meteor;xi andyi are integer values between -200,000 and 200,000, andai andbi are integer values between -10 and 10. Note that at least one ofai andbi is not zero. These four values are separated by single spaces. We assume that all starting pointspi 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


    解题报告: 统计所有流星进入和离开给定区域的时间,然后按先后顺序排序,扫描一遍,统计答案。    要注意的是,相同时间,终止点时间的优先级高一点,否则会多统计一次的。代码如下:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <vector>#include <queue>#include <map>#include <string>using namespace std;#define ff(i, n) for(int i=0;i<(n);i++)#define fff(i, n, m) for(int i=(n);i<=(m);i++)#define dff(i, n, m) for(int i=(n);i>=(m);i--)#define mem(a) memset((a), 0, sizeof(a))typedef long long LL;typedef unsigned long long ULL;void work();int main(){#ifdef ACM    freopen("in.txt", "r", stdin);//    freopen("in.txt", "w", stdout);#endif // ACM    work();}/*****************************************/void calculate(int x, int v, int w, double & l, double & r){    if(v == 0)    {        if(x <= 0 || x >= w)            r = l - 1;    }    else if(v > 0)    {        l = max(l, -(double)x/v);        r = min(r, (double)(w-x)/v);    }    else    {        l = max(l, (double)(w-x)/v);        r = min(r, -(double)x/v);    }}struct Node{    double t;    int isEnd;    bool operator<(const Node& cmp) const    {        return t==cmp.t ? isEnd > cmp.isEnd : t < cmp.t;    }} node[222222];void work(){    int T;    scanf("%d", &T);    ff(cas, T)    {        int w, h, n;        scanf("%d%d%d", &w, &h, &n);        int tot = 0;        ff(i, n)        {            int x, y, vx, vy;            scanf("%d%d%d%d", &x, &y, &vx, &vy);            double l = 0, r = 1e9;            calculate(x, vx, w, l, r);            calculate(y, vy, h, l, r);            if(l < r)            {                node[tot].isEnd = 0;                node[tot].t = l;                tot++;                node[tot].isEnd = 1;                node[tot].t = r;                tot++;            }        }        sort(node, node+tot);        int ans = 0;        int cnt = 0;        ff(i, tot)        {            if(node[i].isEnd)                cnt--;            else                cnt++, ans=max(ans, cnt);        }        printf("%d\n", ans);    }}



    因为速度是-10 到 10的整数,而最终我们只要比较时间的大小。所以计算过程中我们可以乘上lcm(1, 2, 3, ... 10)让时间也变成整数。

    因为终止时间的优先级比较高,换句话说就是如果时间一样,终止时间看起来比较小。我们可以给时间乘2,终止时间单独减1,这样可以直接用一个int类型的数组记录结果,并且判断时直接判断是否为奇数就知道是不是终止时间了。代码如下:

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <vector>#include <queue>#include <map>#include <string>using namespace std;#define ff(i, n) for(int i=0;i<(n);i++)#define fff(i, n, m) for(int i=(n);i<=(m);i++)#define dff(i, n, m) for(int i=(n);i>=(m);i--)#define mem(a) memset((a), 0, sizeof(a))typedef long long LL;typedef unsigned long long ULL;void work();int main(){#ifdef ACM    freopen("in.txt", "r", stdin);//    freopen("in.txt", "w", stdout);#endif // ACM    work();}/*****************************************/void cal(int x, int v, int w, int & l, int & r){    if(v == 0)    {        if(x <=0 || x >= w) r = l - 1;    }    else if(v > 0)    {        l = max(l, -x*2520/v);        r = min(r, (w-x)*2520/v);    }    else    {        l = max(l, (w-x)*2520/v);        r = min(r, -x*2520/v);    }}int num[222222];void work(){    int T;    scanf("%d", &T);    ff(cas, T)    {        int w, h, n;        scanf("%d%d%d", &w, &h, &n);        int tot = 0;        ff(i, n)        {            int x, y, vx, vy;            scanf("%d%d%d%d", &x, &y, &vx, &vy);            int l = 0, r = 1e9;            cal(x, vx, w, l, r);            cal(y, vy, h, l, r);            if(l < r)            {                num[tot++] = l * 2;                num[tot++] = r * 2 - 1;            }        }        sort(num, num+tot);        int cnt = 0;        int ans = 0;        ff(i, tot)        {            if(num[i]&1)                cnt--;            else                cnt++, ans = max(ans, cnt);        }        printf("%d\n", ans);    }}

0 0