CF1041F Ray in the tube [枚举/map]

Author Avatar
空気浮遊 2018年09月16日
  • 在其它设备中阅读本文章

Problem

两根线平行于 x 轴,每根线有若干个传感器
随意发射激光,碰线反弹,问一个激光最多能碰多少个传感器

Solution

考虑步长。

步长为 6 可以分解为步长为 2(lowbit)。
因为 6 可以由 2 乘上奇数得来,因此 6 能到达的点 2 也能到。
对于所有数的 lowbit 都有此成立。

因此只需枚举 $2^k$ 步长即可。

考虑同一个线碰撞为两个坐标在膜两倍步长下同余。
不同线碰撞为一个坐标加上步长和另一个坐标膜两倍步长下同余。
map 统计即可。

注意激光可以垂直于 x 轴发射,一个巨大的坑。

Code

#include<bits/stdc++.h>
#define CLR(x, y) memset(x, y, sizeof(x))
using namespace std;

typedef long long ll;
template<typename T> inline void gn(T &x) {
    char ch=getchar(), pl=0; x=0;
    while(ch<'0' || ch>'9') pl=(ch=='-'), ch=getchar();
    while(ch>='0' && ch<='9') x=x*10+ch-'0', ch=getchar(); x*=pl?-1:1;
}

const int N=1e5+10;
int u[N], d[N];
map<int, int> ma;

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int n, na, m;
    gn(n), gn(na);
    for(int i=1;i<=n;i++) gn(u[i]);
    gn(m), gn(na);
    for(int i=1;i<=m;i++) gn(d[i]);
    int ans=0;
    for(int k=1;k<=1000000001;k<<=1) {
        ma.clear();
        for(int i=1;i<=n;i++) ma[u[i]%(k<<1)]++;
        for(int i=1;i<=m;i++) ma[(d[i]+k)%(k<<1)]++;
        for(auto x:ma) ans=max(ans, x.second);
    }
    ma.clear();
    for(int i=1;i<=n;i++) ma[u[i]]++;
    for(int i=1;i<=m;i++) ma[d[i]]++;
    for(auto x:ma) ans=max(ans, x.second);
    cout<<ans<<endl;
    return 0;
}