A14429 | Gregor and the Pawn Game
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There is a chessboard of size $n$ by $n$ . The square in the $i$ -th row from top and $j$ -th column from the left is labelled $(i,j)$ .
Currently, Gregor has some pawns in the $n$ -th row. There are also enemy pawns in the $1$ -st row. On one turn, Gregor moves one of his pawns. A pawn can move one square up (from $(i,j)$ to $(i-1,j)$ ) if there is no pawn in the destination square. Additionally, a pawn can move one square diagonally up (from $(i,j)$ to either $(i-1,j-1)$ or $(i-1,j+1)$ ) if and only if there is an enemy pawn in that square. The enemy pawn is also removed.
Gregor wants to know what is the maximum number of his pawns that can reach row $1$ ?
Note that only Gregor takes turns in this game, and the enemy pawns never move. Also, when Gregor's pawn reaches row $1$ , it is stuck and cannot make any further moves.
Currently, Gregor has some pawns in the $n$ -th row. There are also enemy pawns in the $1$ -st row. On one turn, Gregor moves one of his pawns. A pawn can move one square up (from $(i,j)$ to $(i-1,j)$ ) if there is no pawn in the destination square. Additionally, a pawn can move one square diagonally up (from $(i,j)$ to either $(i-1,j-1)$ or $(i-1,j+1)$ ) if and only if there is an enemy pawn in that square. The enemy pawn is also removed.
Gregor wants to know what is the maximum number of his pawns that can reach row $1$ ?
Note that only Gregor takes turns in this game, and the enemy pawns never move. Also, when Gregor's pawn reaches row $1$ , it is stuck and cannot make any further moves.
输入格式
The first line of the input contains one integer $t$ ( $1\le t\le 2\cdot 10^4$ ) — the number of test cases. Then $t$ test cases follow.
Each test case consists of three lines. The first line contains a single integer $n$ ( $2\le n\le 2\cdot{10}^{5}$ ) — the size of the chessboard.
The second line consists of a string of binary digits of length $n$ , where a $1$ in the $i$ -th position corresponds to an enemy pawn in the $i$ -th cell from the left, and $0$ corresponds to an empty cell.
The third line consists of a string of binary digits of length $n$ , where a $1$ in the $i$ -th position corresponds to a Gregor's pawn in the $i$ -th cell from the left, and $0$ corresponds to an empty cell.
It is guaranteed that the sum of $n$ across all test cases is less than $2\cdot{10}^{5}$ .
Each test case consists of three lines. The first line contains a single integer $n$ ( $2\le n\le 2\cdot{10}^{5}$ ) — the size of the chessboard.
The second line consists of a string of binary digits of length $n$ , where a $1$ in the $i$ -th position corresponds to an enemy pawn in the $i$ -th cell from the left, and $0$ corresponds to an empty cell.
The third line consists of a string of binary digits of length $n$ , where a $1$ in the $i$ -th position corresponds to a Gregor's pawn in the $i$ -th cell from the left, and $0$ corresponds to an empty cell.
It is guaranteed that the sum of $n$ across all test cases is less than $2\cdot{10}^{5}$ .
输出格式
For each test case, print one integer: the maximum number of Gregor's pawns which can reach the $1$ -st row.
输入输出样例
输入 #1
4 3 000 111 4 1111 1111 3 010 010 5 11001 00000
输出 #1
3 4 0 0
In the first example, Gregor can simply advance all $3$ of his pawns forward. Thus, the answer is $3$ .
In the second example, Gregor can guarantee that all $4$ of his pawns reach the enemy row, by following the colored paths as demonstrated in the diagram below. Remember, only Gregor takes turns in this "game"!
In the third example, Gregor's only pawn is stuck behind the enemy pawn, and cannot reach the end.
In the fourth example, Gregor has no pawns, so the answer is clearly $0$ .
In the second example, Gregor can guarantee that all $4$ of his pawns reach the enemy row, by following the colored paths as demonstrated in the diagram below. Remember, only Gregor takes turns in this "game"!
In the third example, Gregor's only pawn is stuck behind the enemy pawn, and cannot reach the end.
In the fourth example, Gregor has no pawns, so the answer is clearly $0$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted