MicrosoftOA #49
Minimum Replacements Required to Make a Matrix Balanced
估计中等可在线判题
Replace some ? with R or W; the remaining ? may be left as they are. Every row and every column must have equal numbers of R and W. Find the minimum number of replacements, or output −1 if it is impossible.
This site uses a standard input/output protocol.
输入格式
The first line contains N, followed by two strings of length N over R/W/?. This site uses 1≤N≤100000.
输出格式
Output a single integer, the answer.
样例 1
输入:
5
WR???
R???W
输出:
4
样例 2
输入:
1
R
R
输出:
-1
样例 3
输入:
2
??
??
输出:
0
样例说明
Example 1: first fill the two single ? cells, then use one double-? column to balance the row counts, for 4 replacements. Example 2: a fixed same-color column can never be balanced, so −1. Example 3: with all ?, both colors count 0 and are already balanced, so 0 replacements.