Snuke Panic (1D)

Problem Statement

Takahashi is trying to catch many Snuke.

There are five pits at coordinates 00, 11, 22, 33, and 44 on a number line, connected to Snuke's nest.

Now, NN Snuke will appear from the pits. It is known that the ii-th Snuke will appear from the pit at coordinate X_iXi​ at time T_iTi​, and its size is A_iAi​.

Takahashi is at coordinate 00 at time 00 and can move on the line at a speed of at most 11.
He can catch a Snuke appearing from a pit if and only if he is at the coordinate of that pit exactly when it appears.
The time it takes to catch a Snuke is negligible.

Find the maximum sum of the sizes of Snuke that Takahashi can catch by moving optimally.

Constraints

  • 1 \leq N \leq 10^51≤N≤105
  • 0 < T_1 < T_2 < \ldots < T_N \leq 10^50<T1​<T2​<…<TN​≤105
  • 0 \leq X_i \leq 40≤Xi​≤4
  • 1 \leq A_i \leq 10^91≤Ai​≤109
  • All values in input are integers

        The general idea of ​​the topic is that at time t[i], an object with a body length a[i] will appear at the position x[i], and then it is necessary to control the object that moves at most one unit per second to eat the object that appears, and to make it eat The total length of the object is as large as possible.

         Because the range of movement is actually very small, there are only 5 unit lengths (0-4) in total, so the idea of ​​​​violent update can be considered, with det=t[i]-t[i-1 in two adjacent times ] time interval , during this time the takahashi of each unit (actually the optimal configuration of the takahashi at this position after the previous series of most operations) can be moved to the cell with a radius of det, and Bring the existing length of this cell over, if it can be larger, then update the reached position, that is, overwrite it. After the update is completed, it is the current optimal configuration at the moment before the object appears. At this time, when the object appears, you only need to add the length to the takahashi at this position at the corresponding position. It is necessary to pay attention to the relationship between position and time when adding the length. At least the reachable conditions must be met before the length can be added directly.

code show as below:

#include<stdio.h>
#include<algorithm>
using namespace std;
#define ll long long
int t[200000];
int x[200000];
int a[200000];
ll ans[10];
ll tmp[10];
int main()
{
	int n; scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d%d%d", t + i, x + i, a + i);
	}
	for (int i = 0; i < 5; i++) 
	{
		ans[i] = 0;//初始化
	}
	t[0] = 0;
	for (int i = 1; i <= n; i++)
	{
		int det = t[i] - t[i - 1];//计算时间间隔,也就是这段时间的扩散范围
		//printf("det=%d\n", det);
		for (int j = 0; j <= 4; j++)
		{
			tmp[j] = ans[j];//暂存,因为是要和更新之前的比较
		}
		for (int j = 0; j <= 4; j++)//逐点扩散
		{
			for (int k = max(0, j - det); k <= min(4, j + det); k++)
			{
				ans[k] = max(ans[k], tmp[j]);//和j位置的比较,因为是从j位置扩散过来的
			}
		}
		if(t[i]>=x[i])//必须要可以到达
			ans[x[i]] += a[i];//吃到
	}
	ll res = 0;
	for (int i = 0; i <= 4; i++)
	{
		res = max(res, ans[i]);//找出最大值
	}
	printf("%lld\n", res);
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_60360239/article/details/127251858