#P8490. [IOI 2022] 鲶鱼塘

[IOI 2022] 鲶鱼塘

Background

Due to technical limitations, please do not use C++ 14 (GCC 9) to submit this problem.

This is an interactive problem. You only need to implement the function required in the code.

Your code does not need to include any additional header files, and you do not need to implement the main function.

Problem Description

Bu Dengklek has a catfish pond. The pond is a grid of N×NN \times N cells. Each cell is a square of the same size. The columns of the grid are numbered from west to east as 00 to N1N - 1, and the rows are numbered from south to north as 00 to N1N - 1. We denote the cell in column cc and row rr (0cN10 \le c \le N - 1, 0rN10 \le r \le N - 1) as cell (c,r)(c, r).

There are MM catfish in the pond, numbered from 00 to M1M - 1, and each is in a different cell. For each ii with 0iM10 \le i \le M - 1, catfish ii is in cell (Xi,Yi)(X_i, Y_i) and has weight WiW_i grams.

Bu Dengklek wants to build some dikes to catch catfish. A dike of length kk in column cc (for all 0cN10 \le c \le N - 1 and 1kN1 \le k \le N) is a rectangle that goes from row 00 up to row k1k - 1, covering cells (c,0),(c,1),,(c,k1)(c, 0), (c, 1), \ldots, (c, k - 1). For each column, Bu Dengklek may build a dike of some length of her choice, or build no dike.

Catfish ii (for all 0iM10 \le i \le M - 1) can be caught if there is a dike directly adjacent to its west side or east side, and there is no dike covering the cell it is in; that is, if:

  • At least one of the cells (Xi1,Yi)(X_i - 1, Y_i) or (Xi+1,Yi)(X_i + 1, Y_i) is covered by some dike, and
  • No dike covers cell (Xi,Yi)(X_i, Y_i).

For example, consider a pond with N=5N = 5 and M=4M = 4 catfish:

  • Catfish 00 is in cell (0,2)(0, 2) and has weight 55 grams.
  • Catfish 11 is in cell (1,1)(1, 1) and has weight 22 grams.
  • Catfish 22 is in cell (4,4)(4, 4) and has weight 11 gram.
  • Catfish 33 is in cell (3,3)(3, 3) and has weight 33 grams.

Bu Dengklek can build dikes as follows:

Before building dikes After building dikes

The number in a cell indicates the weight of the catfish in that cell.
Shaded cells are covered by dikes.
In this scenario, catfish 00 (in cell (0,2)(0, 2)) and catfish 33 (in cell (3,3)(3, 3)) can be caught.
Catfish 11 (in cell (1,1)(1, 1)) is not caught because a dike covers its cell. Catfish 22 (in cell (4,4)(4, 4)) is not caught because there is no dike directly adjacent to its west or east side.

Bu Dengklek wants to build dikes so that the total weight of caught catfish is as large as possible. Your task is to compute the maximum total weight of catfish that Bu Dengklek can catch by building dikes.

Input Format

You need to implement the following function:

int64 max_weights(int N, int M, int[] X, int[] Y, int[] W)
  • NN: the size of the pond.
  • MM: the number of catfish.
  • XX, YY: two arrays of length MM giving the positions of the catfish.
  • WW: an array of length MM giving the weights of the catfish.
  • The function should return an integer representing the maximum total weight of catfish that Bu Dengklek can catch by building dikes.
  • This function will be called exactly once.

Output Format

Consider the following call:

max_weights(5, 4, [0, 1, 4, 3], [2, 1, 4, 3], [5, 2, 1, 3])

The explanation of this example can be found earlier in the statement.

After building the described dikes, Bu Dengklek can catch catfish 00 and 33, with total weight 5+3=85 + 3 = 8 grams.
Since it is impossible to build dikes that catch catfish with total weight greater than 88 grams, the function should return 88.

Hint

Constraints

  • 2N100  0002 \le N \le 100\;000
  • 1M300  0001 \le M \le 300\;000
  • 0XiN10 \le X_i \le N - 1, 0YiN10 \le Y_i \le N - 1 (for all 0iM10 \le i \le M - 1)
  • 1Wi1091 \le W_i \le 10^9 (for all 0iM10 \le i \le M - 1)
  • No two catfish will be in the same cell.
    In other words, XiX[j]X_i \neq X[j] or YiY[j]Y_i \neq Y[j] (for all 0i<jM10 \le i \lt j \le M - 1).

Subtasks

  1. (3 points) XiX_i is even (for all 0iM10 \le i \le M - 1).
  2. (6 points) Xi1X_i \le 1 (for all 0iM10 \le i \le M - 1).
  3. (9 points) Yi=0Y_i = 0 (for all 0iM10 \le i \le M - 1).
  4. (14 points) N300N \le 300, Yi8Y_i \le 8 (for all 0iM10 \le i \le M - 1).
  5. (21 points) N300N \le 300.
  6. (17 points) N3000N \le 3000.
  7. (14 points) In each column, there are at most 22 catfish.
  8. (16 points) No additional constraints.

Sample grader

The sample grader reads input in the following format:

  • Line 11: N  MN \; M
  • Line 2+i2 + i (0iM10 \le i \le M - 1): Xi  Yi  WiX_i \; Y_i \; W_i

The sample grader prints your answer in the following format:

  • Line 11: the return value of max_weights.

Notes

In the statement, when giving the function interface, the following generic type names are used: void, int, int64, int[] (array), and int[][] (array of arrays).

In C++, the grader will use appropriate data types or implementations as shown in the following tables:

void int int64 int[]
void int long long std::vector<int>
int[][] Length of array a
std::vector<std::vector<int>> a.size()

Translated by ChatGPT 5