题解:P9848 [ICPC 2021 Nanjing R] Cloud Retainer's Game
发现球运动的轨迹相当统一,考虑
首先将所有挡板和球放在同一个数组中按照
第一这样很难写,而且复杂度也爆炸,我们考虑优化,我们发现一个球经过每个位置向下或这向上飞都相当于从某个最靠前的底部开始向上飞。
如图:
黄线从蓝挡板处反弹,相当于从反向延长线和最低线的交点开始向上飞,当然位置要模
所以我们考虑维护两个数组
记住每个我们维护的
代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
int T, H, n, m, f[N][2];
struct node {
int x, y, o;
bool operator < (const node b) const {
return x < b.x;
}
};
node a[N];
unordered_map <int, int> sum, MX;
signed main() {
// freopen("ball.in","r",stdin);
// freopen("ball.out","w",stdout);
scanf("%lld", &T);
while (T -- ) {
scanf("%lld%lld", &H, &n);
H *= 2;
for (int i = 1; i <= n; ++ i ) {
scanf("%lld%lld", &a[i].x, &a[i].y);
a[i].o = 0;
}
scanf("%lld", &m);
for (int i = 1; i <= m; ++ i ) {
scanf("%lld%lld", &a[i + n].x, &a[i + n].y);
a[i + n].o = 1;
}
sort(a + 1, a + n + m + 1);
sum.clear();
MX.clear();
for (int i = n + m; i >= 0; -- i ) {
if (a[i].o) {
++ sum[(a[i].x - a[i].y + H) % H];
++ sum[(a[i].x + a[i].y) % H];
continue;
}
int s0 = sum[(a[i].x + a[i].y) % H], s1 = sum[(a[i].x - a[i].y + H) % H];
int m0 = MX[(a[i].x + a[i].y) % H], m1 = MX[(a[i].x - a[i].y + H) % H];
f[i][0] = s0;
f[i][1] = s1;
f[i][0] = max(f[i][0], m0 + s0);
f[i][1] = max(f[i][1], m1 + s1);
MX[(a[i].x + a[i].y) % H] = max(MX[(a[i].x + a[i].y) % H], max(f[i][0], f[i][1]) - s0);
MX[(a[i].x - a[i].y + H) % H] = max(MX[(a[i].x - a[i].y + H) % H], max(f[i][0], f[i][1]) - s1);
}
printf("%lld\n", max(f[0][0], f[0][1]));
}
return 0;
}