#P6764. [APIO2020] 粉刷墙壁

    ID: 5785 远端评测题 1500ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2020APIO交互题Special JudgeO2优化

[APIO2020] 粉刷墙壁

Description

It has been a while since the last time Pak Dengklek painted the walls in his house, so he wants to repaint them. The wall consists of NN sections, numbered from 00 to N1N - 1. In this problem, we assume there are KK different colors, represented by integers from 00 to K1K - 1 (for example, red is represented by 00, blue by 11, and so on). Pak Dengklek wants to paint wall section ii with color C[i]C[i].

To paint the wall, Pak Dengklek hired a contractor company with MM contractors, numbered from 00 to M1M - 1. Unfortunately for Pak Dengklek, contractors are only willing to paint using colors they like. Specifically, contractor jj likes A[j]A[j] colors, and only wants to paint using one of the following colors: color B[j][0]B[j][0], color B[j][1]B[j][1], \dots, or color B[j][A[j]1]B[j][A[j] − 1].

Pak Dengklek can give the contractor company some instructions. In a single instruction, Pak Dengklek gives two parameters xx and yy, where 0x<M0 \leq x < M and 0yNM0 \leq y \leq N - M. The contractor company will assign contractor ((x+l)modM)((x + l) \mod M) to paint wall section (y+l)(y + l), where 0l<M0 \leq l < M. If there exists an ll such that contractor ((x+l)modM)((x + l) \mod M) does not like color C[y+l]C[y + l], then that instruction is invalid.

Pak Dengklek has to pay for each instruction, so he wants to know the minimum number of instructions he must give so that every wall section is painted in its intended color, or determine that his goal is impossible. Each wall section may be painted multiple times, but every time it is painted, the color must be the one Pak Dengklek intends.

You must implement the function minimumInstructions:

  • minimumInstructions(N, M, K, C, A, B) - This function will be called exactly once by the grader.
    • NN: an integer representing the number of wall sections.
    • MM: an integer representing the number of contractors.
    • KK: an integer representing the number of colors.
    • CC: an integer sequence of length NN, representing the intended color of each wall section.
    • AA: an integer sequence of length MM, representing how many colors each contractor likes.
    • BB: a sequence (of length MM) whose elements are sequences, representing the specific colors each contractor likes.
    • The function must return an integer: the minimum number of instructions Pak Dengklek needs to ensure the wall is painted as intended; return 1-1 if it is impossible.

Input Format

The sample grader reads input in the following format:

N M K
C[0] C[1] ... C[N-1]
A[0] B[0][0] B[0][1] ... B[0][A[0]-1]
A[1] B[1][0] B[1][1] ... B[1][A[1]-1]
.
.
.
A[M-1] B[M-1][0] B[M-1][1] ... B[M-1][A[M-1]-1]

Output Format

The sample grader outputs the return value of the function minimumInstructions.

8 3 5
3 3 1 3 4 4 2 2
3 0 1 2
2 2 3
2 3 4

3

5 4 4
1 0 1 2 2
2 0 1
1 1
1 2
1 3

-1

Hint

In the first sample, N=8N = 8, M=3M = 3, K=5K = 5, C=[3,3,1,3,4,4,2,2]C = [3, 3, 1, 3, 4, 4, 2, 2], A=[3,2,2]A = [3, 2, 2], B=[[0,1,2],[2,3],[3,4]]B = [[0, 1, 2], [2, 3], [3, 4]]. Pak Dengklek can give the following instructions.

  1. x=1x = 1, y=0y = 0. This is a valid instruction. Contractor 11 can paint wall section 00, contractor 22 can paint wall section 11, and contractor 00 can paint wall section 22.
  2. x=0x = 0, y=2y = 2. This is a valid instruction. Contractor 00 can paint wall section 22, contractor 11 can paint wall section 33, and contractor 22 can paint wall section 44.
  3. x=2x = 2, y=5y = 5. This is a valid instruction. Contractor 22 can paint wall section 55, contractor 00 can paint wall section 66, and contractor 11 can paint wall section 77.

It is easy to see that Pak Dengklek cannot achieve his goal with fewer than 33 instructions, so minimumInstructions(8, 3, 5, [3, 3, 1, 3, 4, 4, 2, 2], [3, 2, 2], [[0, 1, 2], [2, 3], [3, 4]]) should return 33.

In the second sample, N=5N = 5, M=4M = 4, K=4K = 4, C=[1,0,1,2,2]C = [1, 0, 1, 2, 2], A=[2,1,1,1]A = [2, 1, 1, 1], B=[[0,1],[1],[2],[3]]B = [[0, 1], [1], [2], [3]]. Since contractor 33 only likes color 33, but no wall section is intended to be painted with that color, Pak Dengklek cannot give any valid instruction. Therefore, minimumInstructions(5, 4, 4, [1, 0, 1, 2, 2], [2, 1, 1, 1], [[0, 1], [1], [2], [3]]) should return 1-1.

For 0k<K0 \leq k < K, let f(k)f(k) denote the number of contractors who like color kk.

Constraints

  • 1N1051 \leq N \leq 10^5.
  • 1Mmin(N,5×104)1 \leq M \leq \min(N, 5 \times 10^4).
  • 1K1051 \leq K \leq 10^5.
  • 0C[i]<K0 \leq C[i] < K.
  • 1A[j]K1 \leq A[j] \leq K.
  • $0 \leq B[j][0] < B[j][1] < \dots < B[j][A[j] − 1] < K$.
  • f(k)24×105\sum f(k)^2 \leq 4\times 10^5.

Subtask 11 (1212 points)

  • f(k)1f(k) \leq 1.

Subtask 22 (1515 points)

  • N500N \leq 500.
  • Mmin(N,200)M \leq \min(N, 200).
  • f(k)21000\sum f(k)^2 \leq 1 000.

Subtask 33 (1313 points)

  • N500N \leq 500.
  • Mmin(N,200)M \leq \min(N, 200).

Subtask 44 (2323 points)

  • N20000N \leq 20 000.
  • Mmin(N,2000)M \leq \min(N, 2 000).

Subtask 55 (3737 points)

  • No additional constraints.

Translated by ChatGPT 5