题目链接:codeforces1427C The Hard Work of Paparazzi
题目思路:
直接 O ( n 2 ) O(n^2) O(n2) 枚举。观察 r r r 比较小,当 i ≥ 2 ∗ r i \ge 2*r i≥2∗r 时,不用从头扫一遍,只要从 i − 2 ∗ r i-2*r i−2∗r 开始即可,用一个数组维护前 i i i 个的最大值。
参考代码:
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e5 + 10;
int r, n;
int t[N], x[N], y[N];
int dp[N], tmp[N];
int main() {
cin >> r >> n;
for (int i = 1; i <= n; i++) {
cin >> t[i] >> x[i] >> y[i];
}
memset(dp, -0x3f, sizeof(dp));
dp[0] = 0;
x[0] = y[0] = 1;
for (int i = 1; i <= n; i++) {
if (i >= r*2) dp[i] = max(dp[i], tmp[i-2*r] + 1);
for (int j = max(0, i - 2*r); j <= i-1; j++) {
if (abs(x[i] - x[j]) + abs(y[i] - y[j]) <= t[i] - t[j]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
tmp[i] = max(tmp[i-1], dp[i]);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
return 0;
}