CF1705D.Mark and Lightbulbs

普及+/提高

通过率:0%

时间限制:1.50s

内存限制:256MB

AC君温馨提醒

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

题目描述

Mark has just purchased a rack of nn lightbulbs. The state of the lightbulbs can be described with binary string s=s1s2…sns = s_1s_2\dots s_n, where si=1s_i=\texttt{1} means that the ii-th lightbulb is turned on, while si=0s_i=\texttt{0} means that the ii-th lightbulb is turned off.

Unfortunately, the lightbulbs are broken, and the only operation he can perform to change the state of the lightbulbs is the following:

  • Select an index ii from 2,3,…,n−12,3,\dots,n-1 such that si−1≠si+1s_{i-1}\ne s_{i+1}.
  • Toggle sis_i. Namely, if sis_i is 0\texttt{0}, set sis_i to 1\texttt{1} or vice versa.

Mark wants the state of the lightbulbs to be another binary string tt. Help Mark determine the minimum number of operations to do so.

马克刚刚购买了一排 nn 个灯泡。灯泡的状态可以用一个二进制字符串 s=s1s2…sns = s_1s_2\dots s_n 来描述,其中 si=1s_i=\texttt{1} 表示第 ii 个灯泡处于开启状态,而 si=0s_i=\texttt{0} 表示第 ii 个灯泡处于关闭状态。

不幸的是,这些灯泡损坏了,马克唯一能执行的、用于改变灯泡状态的操作如下:

  • 选择一个下标 ii,满足 i∈{2,3,…,n−1}i \in \{2,3,\dots,n-1\} 且 si−1≠si+1s_{i-1}\ne s_{i+1};
  • 翻转 sis_i 的值(即若 sis_i 为 0\texttt{0},则将其改为 1\texttt{1};若为 1\texttt{1},则改为 0\texttt{0})。

马克希望将灯泡的状态变为另一个二进制字符串 tt。请帮助马克确定达成目标所需的最少操作次数。

输入格式

The first line of the input contains a single integer qq (1≤q≤1041\leq q\leq 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (3≤n≤2⋅1053\leq n\leq 2\cdot 10^5) — the number of lightbulbs.

The second line of each test case contains a binary string ss of length nn — the initial state of the lightbulbs.

The third line of each test case contains a binary string tt of length nn — the final state of the lightbulbs.

It is guaranteed that the sum of nn across all test cases does not exceed 2⋅1052\cdot 10^5.

输入的第一行包含一个整数 qq(1≤q≤1041\leq q\leq 10^4),表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(3≤n≤2⋅1053\leq n\leq 2\cdot 10^5),表示灯泡的数量。

每个测试用例的第二行包含一个长度为 nn 的二进制字符串 ss,表示灯泡的初始状态。

每个测试用例的第三行包含一个长度为 nn 的二进制字符串 tt,表示灯泡的目标状态。

保证所有测试用例中 nn 的总和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, print a line containing the minimum number of operations Mark needs to perform to transform ss to tt. If there is no such sequence of operations, print −1-1.

对于每个测试用例,输出一行,包含 Mark 将 ss 变换为 tt 所需的最少操作次数。如果不存在这样的操作序列,则输出 −1-1。

输入输出样例

  • 输入#1

    4
    4
    0100
    0010
    4
    1010
    0100
    5
    01001
    00011
    6
    000101
    010011

    输出#1

    2
    -1
    -1
    5

说明/提示

In the first test case, one sequence of operations that achieves the minimum number of operations is the following.

  • Select i=3i=3, changing 0100\texttt{01}\color{red}{\texttt{0}}\texttt{0} to 0110\texttt{01}\color{red}{\texttt{1}}\texttt{0}.
  • Select i=2i=2, changing 0110\texttt{0}\color{red}{\texttt{1}}\texttt{10} to 0010\texttt{0}\color{red}{\texttt{0}}\texttt{10}.

In the second test case, there is no sequence of operations because one cannot change the first digit or the last digit of ss.

In the third test case, even though the first digits of ss and tt are the same and the last digits of ss and tt are the same, it can be shown that there is no sequence of operations that satisfies the condition.

In the fourth test case, one sequence that achieves the minimum number of operations is the following:

  • Select i=3i=3, changing 000101\texttt{00}\color{red}{\texttt{0}}\texttt{101} to 001101\texttt{00}\color{red}{\texttt{1}}\texttt{101}.
  • Select i=2i=2, changing 001101\texttt{0}\color{red}{\texttt{0}}\texttt{1101} to 011101\texttt{0}\color{red}{\texttt{1}}\texttt{1101}.
  • Select i=4i=4, changing 011101\texttt{011}\color{red}{\texttt{1}}\texttt{01} to 011001\texttt{011}\color{red}{\texttt{0}}\texttt{01}.
  • Select i=5i=5, changing 011001\texttt{0110}\color{red}{\texttt{0}}\texttt{1} to 011011\texttt{0110}\color{red}{\texttt{1}}\texttt{1}.
  • Select i=3i=3, changing 011011\texttt{01}\color{red}{\texttt{1}}\texttt{011} to 010011\texttt{01}\color{red}{\texttt{0}}\texttt{011}.

在第一个测试用例中,一种能够达到最少操作次数的操作序列如下:

  • 选择 i=3i=3,将 0100\texttt{01}\color{red}{\texttt{0}}\texttt{0} 变为 0110\texttt{01}\color{red}{\texttt{1}}\texttt{0}。
  • 选择 i=2i=2,将 0110\texttt{0}\color{red}{\texttt{1}}\texttt{10} 变为 0010\texttt{0}\color{red}{\texttt{0}}\texttt{10}。

在第二个测试用例中,不存在可行的操作序列,因为无法修改字符串 ss 的第一位或最后一位。

在第三个测试用例中,尽管 ss 和 tt 的第一位相同、最后一位也相同,但可以证明并不存在满足条件的操作序列。

在第四个测试用例中,一种能够达到最少操作次数的操作序列如下:

  • 选择 i=3i=3,将 000101\texttt{00}\color{red}{\texttt{0}}\texttt{101} 变为 001101\texttt{00}\color{red}{\texttt{1}}\texttt{101}。
  • 选择 i=2i=2,将 001101\texttt{0}\color{red}{\texttt{0}}\texttt{1101} 变为 011101\texttt{0}\color{red}{\texttt{1}}\texttt{1101}。
  • 选择 i=4i=4,将 011101\texttt{011}\color{red}{\texttt{1}}\texttt{01} 变为 011001\texttt{011}\color{red}{\texttt{0}}\texttt{01}。
  • 选择 i=5i=5,将 011001\texttt{0110}\color{red}{\texttt{0}}\texttt{1} 变为 011011\texttt{0110}\color{red}{\texttt{1}}\texttt{1}。
  • 选择 i=3i=3,将 011011\texttt{01}\color{red}{\texttt{1}}\texttt{011} 变为 010011\texttt{01}\color{red}{\texttt{0}}\texttt{011}。

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

首页