第 1130 场 Codeforces Round(Div. 2)
B
青蛙跳跃
Frogs Jump
Codeforces
未评级
原题
← AC →

青蛙跳跃AI (CI翻译)

Given a $2 \times n$ grid, each cell of the grid is either empty or contains one frog. It is guaranteed that each column of the input grid contains at least one frog. You can choose $k$ frogs and remove them from the grid. After this, all remaining frogs will jump.

给定一个 $2 \times n$ 的网格,每个格子要么为空,要么有一只青蛙。保证输入网格的每一列中至少有一只青蛙。你可以选择 $k$ 只青蛙并将它们从网格中移除。之后,所有剩余的青蛙都会跳跃。AI (CI翻译)

Each frog can jump up, down, left, or right by $1$ cell, but cannot jump outside the grid. All frogs must choose a legal direction and jump simultaneously exactly once. After jumping, the number of frogs in each row and each column of the grid must remain unchanged.

每只青蛙可以向上、下、左或右跳一格,但不能跳出网格。所有青蛙都必须选择一个合法方向,并且恰好同时跳跃一次。跳跃后,网格中每一行和每一列的青蛙数量都必须保持不变。AI (CI翻译)

Note that:

注意:AI (CI翻译)

  • Even if two frogs' jump directions conflict, they can still jump to their respective target cells.
  • Multiple frogs are allowed to jump to the same cell, because a cell can hold arbitrarily many frogs.
  • 即使两只青蛙的跳跃方向相互冲突,它们仍然可以跳到各自的目标格子。
  • 多只青蛙可以跳到同一个格子,因为一个格子可以容纳任意多只青蛙。
AI (CI翻译)

Find the minimum $k$ such that there exists a valid jumping scheme.

求存在一种合法跳跃方案时,$k$ 的最小值。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

每个输入包含多组测试用例。第一行包含测试用例数量 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)

For each test case, the first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$), where $n$ is the number of columns in the grid.

每个测试用例的第一行包含一个整数 $n$($1 \le n \le 2 \cdot 10^5$),表示网格的列数。AI (CI翻译)

The next two lines each contain a string of length $n$; we use the character $\texttt{f}$ to represent a cell containing a frog, and the character $\texttt{o}$ to represent an empty cell.

接下来的两行各包含一个长度为 $n$ 的字符串;用字符 $\texttt{f}$ 表示该格子中有一只青蛙,用字符 $\texttt{o}$ 表示该格子为空。AI (CI翻译)

It is guaranteed that each column of the input grid contains at least one frog.

保证输入网格的每一列中至少有一只青蛙。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

保证所有测试用例的 $n$ 之和不超过 $2 \cdot 10^5$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output a single line containing the minimum $k$ such that there exists a valid jumping scheme.

对于每个测试用例,输出一行,包含存在合法跳跃方案时 $k$ 的最小值。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first test case, two frogs can jump to each other's cells.

在第一个测试用例中,两只青蛙可以跳到彼此所在的格子。AI (CI翻译)

In the second test case, the left frog can jump down, and the right frog can jump up. After the jumps, the total number of frogs in each row and each column remains $1$.

在第二个测试用例中,左边的青蛙可以向下跳,右边的青蛙可以向上跳。跳跃后,每一行和每一列的青蛙总数仍为 $1$。AI (CI翻译)

o f
f o
o f
f o
AI (CI翻译)

In the third test case, three frogs can all jump to the same cell $(1,2)$, although it is not a valid answer, since the number of frogs in the first column changes from $1$ to $0$.

在第三个测试用例中,三只青蛙可以都跳到同一个格子 $(1,2)$,但这不是一个合法答案,因为第一列的青蛙数量会从 $1$ 变为 $0$。AI (CI翻译)

o f f f o
o o o
o f f f o
o o o
AI (CI翻译)

样例

Input #1
4
2
ff
oo
2
fo
of
3
fof
ofo
1
f
o
Output #1
0
0
1
1
InputCopy OutputCopy
题目信息
题号
2272B
时间限制
1 s
内存限制
256 MB
标签
构造
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。