#P14000. [eJOI 2025] Grid

    ID: 15788 远端评测题 500ms 256MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>动态规划 DP2025交互题eJOI(欧洲)

[eJOI 2025] Grid

题目描述

Simona dreams of uncountable riches. She is offered to play a game for a big prize.

Simona will be placed in cell (0,0)(0, 0) of a grid AA of size N×MN \times M filled with positive integers. She must reach cell (N1,M1)(N-1, M-1). To do this, she is allowed to repeatedly move from her current cell (x,y)(x, y) to any other cell (x+d,y)(x + d, y) or (x,y+d)(x, y + d), such that d>0d > 0. For each such move, Simona will receive reward coins Ax,yAx,yC|A_{x,y} - A_{x',y'}| - C, where xx', yy' are her new coordinates and CC is a constant cost fixed before the start of the journey. Note that if the expression Ax,yAx,yC|A_{x,y} - A_{x',y'}| - C evaluates to a negative number, Simona will lose coins. Note also that it is possible to end the game with a negative number of coins.

Help Simona determine the maximum number of coins she can finish the game with.

Note that a=a|a| = a if a0a \geq 0 and a=a|a| = -a, otherwise.

Implementation details

You have to implement the function max_profit:

long long max_profit(int N, int M, int C, std::vector<std::vector<int>> A)
  • NN, MM: the dimensions of the grid;
  • CC: the fixed cost constant for the test;
  • AA: vector of vectors of integers of size N×MN \times M, representing the two dimensional grid (indexed by row and then column).

This function will be called once for each test and has to return the maximum number of coins Simona can end the game with.

输入格式

The input format is the following:

  • line 1: three integers – the values of NN, MM and CC.
  • lines 2(N+1)2 - (N + 1): MM integers – the values of Ai,jA_{i,j}.

输出格式

The output format is the following:

  • line 1: one integer – the return value of the call.
5 6 4
20 24 31 33 36 40
25 23 25 31 32 39
31 26 21 24 31 35
32 28 25 21 26 28
36 35 28 24 21 27
27
2 2 100
1 2
3 4
-197

提示

Example

Consider the following call:

max_profit(5, 6, 4, {{20, 24, 31, 33, 36, 40},
{25, 23, 25, 31, 32, 39},
{31, 26, 21, 24, 31, 35},
{32, 28, 25, 21, 26, 28},
{36, 35, 28, 24, 21, 27}})

In this case the optimal path is $(0,0) \xrightarrow{7} (0,2) \xrightarrow{2} (1,2) \xrightarrow{10} (1,5) \xrightarrow{8} (4,5)$ and the number of coins achieved by following it is 7+2+10+8=277 + 2 + 10 + 8 = 27. Your function must return 2727.

max_profit(2, 2, 100, {{1, 2}, {3, 4}})

Here your function must return: 197-197. Note that the answer may be negative value.

Constraints

  • 1N,M1 \leq N, M
  • NM500000N \cdot M \leq 500000
  • 1Ai,j10000001 \leq A_{i,j} \leq 1000000 for 0i<N0 \leq i < N and 0j<M0 \leq j < M
  • 0C10000000 \leq C \leq 1000000

Subtasks

Subtask Points Required subtasks Additional constraints
0 00 - The example.
1 99 N=1,M200N = 1, M \leq 200
2 55 N=1,Ai,jAi,j+1N = 1, A_{i,j} \leq A_{i,j+1}
3 88 N=1,C=0N = 1, C = 0
4 1010 11 N=1,M50000N = 1, M \leq 50000
5 77 141-4 N=1N = 1
6 1515 11 N,M200N, M \leq 200
7 99 22 Ai,jAi+1,j,Ai,j+1A_{i,j} \leq A_{i+1,j}, A_{i,j+1}
8 1212 33 C=0C = 0
9 01,4,60-1, 4, 6 NM50000N \cdot M \leq 50000
10 1313 090-9 -