#P6234. [eJOI 2019] T形覆盖

[eJOI 2019] T形覆盖

Description

If you have played Tetris, you should have seen the following shape:

sqr

We call it a T-shaped tetromino. Its center is marked by ×\times.

Manca drew a rectangular grid with mm rows and nn columns. Rows are numbered from 00 to m1m-1, and columns are numbered from 00 to n1n-1. She marked some cells in the grid as special cells. Then, she wants her friend Nika to help her place T-shaped tetrominoes according to the following rules:

  • The number of special cells is the same as the number of T-shaped tetrominoes, and the center of each T-shaped tetromino must be on a special cell in the grid.
  • T-shaped tetrominoes must not overlap.
  • All parts of all tetrominoes must lie inside the grid.

Note that a T-shaped tetromino has four orientations: \bot \top \vdash \dashv.

If no arrangement exists, output No. Otherwise, find an arrangement that maximizes the sum of the numbers in the covered cells, and output this maximum value.

Input Format

The first line contains two integers m,nm,n, representing the number of rows and the number of columns.

The next mm lines each contain nn integers. The jj-th number in the ii-th line (indexed from 00) represents the value ai,ja_{i,j} in the cell at row ii and column jj.

The next line contains one integer kk, representing the number of special cells.

The next kk lines each contain two integers ri,cir_i,c_i, representing the position of the ii-th special cell.

Output Format

If an arrangement exists, output the maximum possible sum of the numbers in the covered cells. Otherwise, output No.

5 6
7 3 8 1 0 9
4 6 2 5 8 3
1 9 7 3 9 5
2 6 8 4 5 7
3 8 2 7 3 6
3
1 1
2 2
3 4
67
5 6
7 3 8 1 0 9
4 6 2 5 8 3
1 9 7 3 9 5
2 6 8 4 5 7
3 8 2 7 3 6
3
1 1
2 2
3 3
No

Hint

Explanation of Sample Input/Output 1

One optimal arrangement is as follows:

  • The tetromino centered at (1,1)(1,1) is oriented as \dashv.
  • The tetromino centered at (2,2)(2,2) is oriented as \vdash.
  • The tetromino centered at (3,4)(3,4) is oriented as \bot.

Constraints and Notes

This problem uses bundled testdata and has 7 subtasks.

  • Subtask 1 (5 points): k103k\le 10^3, and it is guaranteed that for all iji\ne j, max(rirj,cicj)>2\max(|r_i-r_j|,|c_i-c_j|)>2.
  • Subtask 2 (10 points): k103k\le 10^3, and it is guaranteed that for all iji\ne j, if max(rirj,cicj)2\max(|r_i-r_j|,|c_i-c_j|)\le 2, then (ri,ci)(r_i,c_i) and (rj,cj)(r_j,c_j) share an edge.
  • Subtask 3 (10 points): k103k\le 10^3, and it is guaranteed that for all iji\ne j, max(rirj,cicj)2\max(|r_i-r_j|,|c_i-c_j|)\ne 2.
  • Subtask 4 (10 points): All special cells are in the same row.
  • Subtask 5 (15 points): k10k\le 10.
  • Subtask 6 (20 points): k103k\le 10^3.
  • Subtask 7 (30 points): No additional constraints.

For all testdata: 1knm1061\le k\le nm\le 10^6, aij[0,103]a_{ij}\in[0,10^3], ri[0,m)r_i\in[0,m), ci[0,n)c_i\in[0,n).


Notes

The original problem is from: eJOI2019 Problem C. T - Covering.

The translation is from: LibreOJ

Translated by ChatGPT 5