现在的位置: 首页 > 综合 > 正文

UVA 1398 – Meteor(排序+扫描方法+几何)

2014年11月20日 ⁄ 综合 ⁄ 共 3840字 ⁄ 字号 评论关闭

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×viover
time t , where t is a non-negative real value, pi is the starting point of mi and vi is
the velocity ofmi . The point pi = (xiyi) is represented by X -coordinate xi and Y -coordinate yi in
the (XY) -plane, and the velocity vi = (aibi) is a non-zero vector with two components ai and bi in
the (XY) -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 (wh) . 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 , 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$wh$ \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 xiyiai , and bi ; (xiyi) is
the starting point pi and(aibi) 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

题意:一些流星,有起始点和运动坐标,你现在有一个w * h的框,框左下角点为(0,0),要拍一张照,使得那个时间框内(不包括边框上)流星最多。问最多拍几颗流星。

思路:流星的起点和矢量都知道,框也知道,就可以求出流星进入框的时间和出框时间,然后保存下进框和出框时间,去遍历这个数组,按时间排序,然后扫描过去,这正是模拟了流星进出的过程。在过程中记录最大值即可,但是要注意,相同时间的话,要优先计算出框的流星,因为假如一个时间是一进一出,那么总量等于没变,而先加后减反而会使答案增大。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define INF 0x3f3f3f3f
using namespace std;
void scanf_(int &num)
{
    char in;
    bool neg=false;
    while(((in=getchar()) > '9' || in<'0') && in!='-') ;
    if(in=='-')
    {
        neg=true;
        while((in=getchar()) >'9' || in<'0');
    }
    num=in-'0';
    while(in=getchar(),in>='0'&&in<='9')
        num*=10,num+=in-'0';
    if(neg)
        num=0-num;
}
const int N = 100005;
const int LCM = 2520;

int t, n, en, w, h, l, r;

struct E {
	int t, flag;
} e[N * 2];

bool cmp(E a, E b) {
	if (a.t != b.t)
		return a.t < b.t;
	return a.flag < b.flag;
}

void g(int x, int xd, int w) {
	if (xd == 0) {
		if (x <= 0 || x >= w) r = 0;
	}
	else if (xd > 0) {
		l = max(l, -x * LCM / xd);
		r = min(r, (w - x) * LCM / xd);
	}
	else {
		l = max(l, (w - x) * LCM / xd);
		r = min(r, -x * LCM / xd);
	}
}

void init() {
	en = 0;
	scanf("%d%d%d", &w, &h, &n);
	for (int i = 0; i < n; i++) {
		int x, y, xd, yd;
		scanf_(x); scanf_(y); scanf_(xd); scanf_(yd);
		l = 0, r = INF;
		g(x, xd, w);
		g(y, yd, h);
		if (l < r) {
			e[en].t = l; e[en++].flag = 1;
			e[en].t = r; e[en++].flag = -1;
		}
	}
}

int solve() {
	int ans = 0, num = 0;
	sort(e, e + en, cmp);
	for (int i = 0; i < en; i++) {
		num += e[i].flag;
		ans = max(ans, num);
	}
	return ans;
}

int main() {
	scanf("%d", &t);
	while (t--) {
		init();
		printf("%d\n", solve());
	}
	return 0;
}

抱歉!评论已关闭.