码迷,mamicode.com
首页 > 其他好文 > 详细

LA 3905 Meteor

时间:2014-08-02 23:10:24      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   for   art   

给出一些点的初始位置(x, y)及速度(a, b)和一个矩形框,求能同时出现在矩形框内部的点数的最大值。

把每个点进出矩形的时刻分别看做一个事件,则每个点可能对应两个事件,进入事件和离开事件。

按这些事件的发生时间进行排序,然后逐个扫描,遇到进入事件cnt++,遇到离开事件--cnt,用ans记录cnt的最大值。

对时间相同情况的处理,当一个进入事件的时刻和离开事件的时刻相同时,应该先处理离开事件后处理进入事件。

因为速度(a, b)是-10~10的整数,所以乘以LCM(1,2,3,,,10) = 2520,可避免浮点数的运算。

后来我还在纳闷t≥0的条件是如何限制的,后来明白因为L的初值为0,所以max(L, t)是不会出现负数的情况的。

 

bubuko.com,布布扣
 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 const int maxn = 100000 + 10;
 9 const int LCM = 2520;
10 
11 struct Event
12 {
13     int x;
14     int type;
15     bool operator < (const Event a) const
16     {
17         return x < a.x || (x == a.x && type > a.type);
18     }
19 }events[maxn * 2];
20 
21 void update(int x, int a, int w, int &L, int &R)
22 {//0<x+at<w
23     if(a == 0)
24     {
25         if(x <= 0 || x >= w)
26             R = L - 1;
27     }
28     else if(a > 0)
29     {
30         L = max(L, -x*LCM/a);
31         R = min(R, (w-x)*LCM/a);
32     }
33     else
34     {
35         L = max(L, (w-x)*LCM/a);
36         R = min(R, -x*LCM/a);
37     }
38 }
39 
40 int main(void)
41 {
42     #ifdef LOCAL
43         freopen("3905in.txt", "r", stdin);
44     #endif
45 
46     int T;
47     scanf("%d", &T);
48     while(T--)
49     {
50         int w, h, n, e = 0;
51         scanf("%d%d%d", &w, &h, &n);
52         for(int i = 0; i < n; ++i)
53         {
54             int x, y, a, b;
55             scanf("%d%d%d%d", &x, &y, &a, &b);
56             int L = 0, R = (int)1e9;
57             update(x, a, w, L, R);
58             update(y, b, h, L ,R);
59             if(L < R)
60             {
61                 events[e].x = L;    //左端点事件
62                 events[e++].type = 0;
63                 events[e].x = R;
64                 events[e++].type = 1;
65             }
66         }
67         sort(events, events + e);
68         int cnt = 0, ans = 0;
69         for(int i = 0; i < e; ++i)
70         {
71             if(events[i].type == 0)
72                 ans = max(ans, ++cnt);
73             else
74                 --cnt;
75         }
76         printf("%d\n", ans);
77     }
78     return 0;
79 }
代码君

 

LA 3905 Meteor,布布扣,bubuko.com

LA 3905 Meteor

标签:style   blog   http   color   os   io   for   art   

原文地址:http://www.cnblogs.com/AOQNRMGYXLMV/p/3887490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!