A14617 | Alice and Recoloring 2
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 \times m$ , initially all its cells are colored white. The cell on the intersection of $i$ -th row and $j$ -th column is denoted as $(i, j)$ . Alice can do the following operations with this grid:
- Choose any subrectangle containing cell $(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 $1$ coin.
- Choose any subrectangle containing cell $(n, 1)$ , and flip the colors of all its cells.
This operation costs $3$ coins.
- Choose any subrectangle containing cell $(1, m)$ , and flip the colors of all its cells.
This operation costs $4$ coins.
- Choose any subrectangle containing cell $(n, m)$ , and flip the colors of all its cells.
This operation costs $2$ coins.
As a reminder, subrectangle is a set of all cells $(x, y)$ with $x_1 \le x \le x_2$ , $y_1 \le y \le y_2$ for some $1 \le x_1 \le x_2 \le n$ , $1 \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.
Alice has a grid of size $n \times m$ , initially all its cells are colored white. The cell on the intersection of $i$ -th row and $j$ -th column is denoted as $(i, j)$ . Alice can do the following operations with this grid:
- Choose any subrectangle containing cell $(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 $1$ coin.
- Choose any subrectangle containing cell $(n, 1)$ , and flip the colors of all its cells.
This operation costs $3$ coins.
- Choose any subrectangle containing cell $(1, m)$ , and flip the colors of all its cells.
This operation costs $4$ coins.
- Choose any subrectangle containing cell $(n, m)$ , and flip the colors of all its cells.
This operation costs $2$ coins.
As a reminder, subrectangle is a set of all cells $(x, y)$ with $x_1 \le x \le x_2$ , $y_1 \le y \le y_2$ for some $1 \le x_1 \le x_2 \le n$ , $1 \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.
输入格式
The first line of the input contains $2$ integers $n, m$ ( $1 \le n, m \le 500$ ) — the dimensions of the grid.
The $i$ -th of the next $n$ lines contains a string $s_i$ of length $m$ , consisting of letters W and B. The $j$ -th character of string $s_i$ is W if the cell $(i, j)$ is colored white in the favorite coloring of Alice, and B if it's colored black.
The $i$ -th of the next $n$ lines contains a string $s_i$ of length $m$ , consisting of letters W and B. The $j$ -th character of string $s_i$ is W if the cell $(i, j)$ is colored white in the favorite coloring of Alice, and B if it's colored black.
输出格式
Output the smallest number of coins Alice would have to spend to achieve her favorite coloring.
输入输出样例
输入 #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)$ . This would cost $2$ coins.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted