CF1592F2.Alice and Recoloring 2

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The difference between the versions is in the costs of operations. Solution for one version won't work for another!

Alice has a grid of size n×mn \times m, initially all its cells are colored white. The cell on the intersection of ii-th row and jj-th column is denoted as (i,j)(i, j). Alice can do the following operations with this grid:

  • Choose any subrectangle containing cell (1,1)(1, 1), and flip the colors of all its cells. (Flipping means changing its color from white to black or from black to white).

    This operation costs 11 coin.

  • Choose any subrectangle containing cell (n,1)(n, 1), and flip the colors of all its cells.

    This operation costs 33 coins.

  • Choose any subrectangle containing cell (1,m)(1, m), and flip the colors of all its cells.

    This operation costs 44 coins.

  • Choose any subrectangle containing cell (n,m)(n, m), and flip the colors of all its cells.

    This operation costs 22 coins.

As a reminder, subrectangle is a set of all cells (x,y)(x, y) with x1≤x≤x2x_1 \le x \le x_2, y1≤y≤y2y_1 \le y \le y_2 for some 1≤x1≤x2≤n1 \le x_1 \le x_2 \le n, 1≤y1≤y2≤m1 \le y_1 \le y_2 \le m.

Alice wants to obtain her favorite coloring with these operations. What's the smallest number of coins that she would have to spend? It can be shown that it's always possible to transform the initial grid into any other.

不同版本之间的区别在于操作的花费。一个版本的解法无法适用于另一个版本!

爱丽丝有一个大小为 n×mn \times m 的网格,初始时所有格子均为白色。第 ii 行第 jj 列的格子记作 (i,j)(i, j)。爱丽丝可以对这个网格执行以下操作:

  • 任选一个包含格子 (1,1)(1, 1) 的子矩形,并翻转该子矩形中所有格子的颜色(翻转指将白色变为黑色,或黑色变为白色)。
    此操作花费 11 枚硬币。

  • 任选一个包含格子 (n,1)(n, 1) 的子矩形,并翻转该子矩形中所有格子的颜色。
    此操作花费 33 枚硬币。

  • 任选一个包含格子 (1,m)(1, m) 的子矩形,并翻转该子矩形中所有格子的颜色。
    此操作花费 44 枚硬币。

  • 任选一个包含格子 (n,m)(n, m) 的子矩形,并翻转该子矩形中所有格子的颜色。
    此操作花费 22 枚硬币。

需要提醒的是:子矩形是指满足 x1≤x≤x2x_1 \le x \le x_2、y1≤y≤y2y_1 \le y \le y_2 的所有格子 (x,y)(x, y) 组成的集合,其中 1≤x1≤x2≤n1 \le x_1 \le x_2 \le n,1≤y1≤y2≤m1 \le y_1 \le y_2 \le m。

爱丽丝希望借助这些操作得到她最喜爱的染色方案。她最少需要花费多少枚硬币?可以证明:从初始网格出发,总能通过若干次操作得到任意目标染色方案。

输入格式

The first line of the input contains 22 integers n,mn, m (1≤n,m≤5001 \le n, m \le 500) — the dimensions of the grid.

The ii-th of the next nn lines contains a string sis_i of length mm, consisting of letters W and B. The jj-th character of string sis_i is W if the cell (i,j)(i, j) is colored white in the favorite coloring of Alice, and B if it's colored black.

输入的第一行包含两个整数 n,mn, m(1≤n,m≤5001 \le n, m \le 500)——表示网格的尺寸。

接下来的 nn 行中,第 ii 行包含一个长度为 mm 的字符串 sis_i,由字母 W 和 B 组成。字符串 sis_i 的第 jj 个字符为 W 表示 Alice 最喜欢的染色方案中单元格 (i,j)(i, j) 被染成白色,为 B 表示被染成黑色。

输出格式

Output the smallest number of coins Alice would have to spend to achieve her favorite coloring.

输出Alice为实现她最喜欢的染色方案所需花费的最少硬币数量。

输入输出样例

  • 输入#1

    3 3
    WWW
    WBB
    WBB

    输出#1

    2
  • 输入#2

    10 15
    WWWBBBWBBBBBWWW
    BBBBWWWBBWWWBBB
    BBBWWBWBBBWWWBB
    BBWBWBBWWWBBWBW
    BBBBWWWBBBWWWBB
    BWBBWWBBBBBBWWW
    WBWWBBBBWWBBBWW
    WWBWWWWBBWWBWWW
    BWBWWBWWWWWWBWB
    BBBWBWBWBBBWWBW

    输出#2

    68

说明/提示

In the first sample, it's optimal to just apply the fourth operation once to the rectangle containing cells (2,2),(2,3),(3,2),(3,3)(2, 2), (2, 3), (3, 2), (3, 3). This would cost 22 coins.

在第一个样例中,最优策略是仅对包含格子 (2,2),(2,3),(3,2),(3,3)(2, 2), (2, 3), (3, 2), (3, 3) 的矩形应用第四种操作一次,花费为 22 枚金币。

输入解题思路,AI测评打分。不知道怎么写?

首页