C. The Hard Work of Paparazzi(Codeforces Global Round 11)
C. The Hard Work of Paparazzi
https://codeforces.com/contest/1427/problem/C
题意
给你一个 \(r*r\) 的地图,和 \(n\)个明星出现的坐标和时间,记者的初始位置是 \((1,1)\),记者从一个点 \((x,y)\) 转移到另一个点的时间花费为\(|x-x'|+|y-y'|\),问你记者可以拍到的最多的明星数。
思路
首先可以想到 \(n^2\) 的 \(dp\),\(dp[i]\) 表示第i个时间可以拍到的最大明星数,转移方程为 \(dp[i] = max(dp[i], dp[j] + 1), 1 <= j <= i, t_i - t_j >= |x_i - x_j| + |y_i - y_j|\),但是时间复杂度为 \(n^2\),并且此时 \(r\) 没有用到,所以需要使用 \(r\) 来优化,因为地图大小为 \(r*r\),所以发现两个点相距最大的时间花费为 \(4 * r\) (即从起点出发回到起点),所以 \(j\) 的取值优化到了 \(i - 4 * r <= j <= i\),因为在 \(4*r\)的时间里可以跑完地图上所以点,故只取此时的 \(j\) 来更新 \(i\) 就可以了。
Code
/*---------------------------------------------------------------
∧_∧
∧_∧ (´<_` )
( ´_ゝ`) / ⌒i
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
--------------------------------------------------------------*/
#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define req(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define fi first
#define se second
#define PI pair<int,int>
#define PII pair<ll,PI>
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
const ll mod = 1e9 + 7;
inline ll read(){
ll x=0;
int f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
inline void print(ll x){
if(x<0)putchar('-'),x=-x;
if(x>9)print(x/10);
putchar(x%10^48);
}
struct $
{
ll t,x,y;
}a[N];
ll dp[N];
void slove(){
memset(dp,-1,sizeof(dp));
ll r,n;
r = read();
n = read();
for (int i = 1; i <= n; ++i)
{
a[i].t = read();
a[i].x = read();
a[i].y = read();
}
a[0].x = 1;
a[0].y = 1;
a[0].t = 0;
dp[0] = 0;
for (int i = 1; i <= n; ++i)
{
for (int j = max(0ll,i - 4 * r); j < i; ++j)
{
if (dp[j] != -1 && a[i].t - a[j].t >= abs(a[i].x - a[j].x) + abs(a[i].y - a[j].y))
{
dp[i] = max(dp[i], dp[j] + 1);
}
}
}
ll ans = 0;
for (int i = 1; i <= n; ++i)
{
ans = max(ans, dp[i]);
}
print(ans);
puts("");
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("1.in", "r", stdin);
//freopen("1.out", "w", stdout);
#endif
int t = 1;
// t = read();
while(t--){
slove();
}
return 0;
}
Code will change the world !

浙公网安备 33010602011771号