#P10299. [CCC 2024 S5] Chocolate Bar Partition

[CCC 2024 S5] Chocolate Bar Partition

Problem Description

Maxwell has a chocolate bar that he wants to share with his friends. The chocolate bar can be viewed as a 2×N2 \times N grid of small squares, and the tastiness of each square is given by a 2×N2 \times N integer array Ti,jT_{i,j}. Maxwell wants to divide the entire chocolate bar into several connected components, such that the average tastiness of the squares in each connected component is the same. Maxwell wants to know the maximum number of connected components he can partition the chocolate bar into, under the rules above.

A part is considered a connected component if every square in it can be reached by moving up, down, left, or right.

Input Format

The first line contains a positive integer NN, representing the length of the chocolate bar.

The second line contains NN space-separated integers. The jj-th integer is the tastiness T1,jT_{1,j} of the square in row 1 and column jj.

Similarly, the third line contains NN space-separated integers. The jj-th integer is the tastiness T2,jT_{2,j} of the square in row 2 and column jj.

Output Format

Output one integer, representing the maximum number of connected components Maxwell can cut the chocolate bar into.

2
5 4
6 5

2

5
1 0 1 2 0
0 2 0 3 1

5

Hint

Sample 1 Explanation

Partitioning the chocolate bar into 22 parts is optimal. One way is to take the single square in the bottom-right corner as one connected component, and the remaining three squares as the second connected component, as shown below.

The average tastiness of each connected component is 55.

Sample 2 Explanation

One way to partition the chocolate bar with equal averages is shown below:

Note that the average tastiness of each part is 11.

Constraints

This problem uses bundled testdata.

For all testdata, it is guaranteed that 1N2×1051 \leq N \leq 2 \times 10^5 and 0Ti,j1080 \leq T_{i,j} \leq 10^8.

The table below shows the distribution of 1515 points:

Points Range of NN Range of Ti,jT_{i,j}
22 N=2N = 2 0Ti,j50 \leq T_{i,j} \leq 5
1N81 \leq N \leq 8 0Ti,j200 \leq T_{i,j} \leq 20
11 1N201 \leq N \leq 20
22 1N1001 \leq N \leq 100
1N10001 \leq N \leq 1000 0Ti,j1000 \leq T_{i,j} \leq 100
33 1N20001 \leq N \leq 2000 0Ti,j1050 \leq T_{i,j} \leq 10^5
1N2×1051 \leq N \leq 2 \times 10^5 0Ti,j1080 \leq T_{i,j} \leq 10^8

Translated by ChatGPT 5