• LA 3905 Meteor


    给出一些点的初始位置(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)是不会出现负数的情况的。

     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
    ", ans);
    77     }
    78     return 0;
    79 }
    代码君
  • 相关阅读:
    CodeForces 500C New Year Book Reading
    CodeForces 460B Little Dima and Equation 枚举
    CodeForces 451B Sort the Array
    【jquery】jQuery实现轮播图
    【IDEA】IDEA技巧记录
    【eclipse】日常使用eclipse记录
    【SSM】spring配置文件中读取配置文件的三种方式
    【Git】IDEA克隆和提交项目于码云
    semantic UI—表单验证
    【spring Data Jpa】JPA生成数据库表
  • 原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/3887490.html
Copyright © 2020-2023  润新知