QOJ.ac

QOJ

Süre Sınırı: 1 s Bellek Sınırı: 256 MB Toplam puan: 100

#69. 燈

İstatistikler

一條長走廊入面有 $N$ 盞燈排成一行,編號由 $1$ 至 $N$。每盞燈嘅狀態唔係熄咗,就係着咗。有一個特別機關可以改變啲燈嘅狀態。每次操作,我哋可以做以下其中一樣嘢:

  • 揀整數 $p$ 同 $q$,滿足 $1 \le p \le q \le N$,將第 $p,p+1,\ldots,q$ 盞燈熄咗。
  • 揀整數 $p$ 同 $q$,滿足 $1 \le p \le q \le N$,將第 $p,p+1,\ldots,q$ 盞燈着咗。
  • 揀整數 $p$ 同 $q$,滿足 $1 \le p \le q \le N$,將第 $p,p+1,\ldots,q$ 盞燈嘅狀態反轉(由熄變着,或者由着變熄)。

啲燈而家嘅狀態用一個長度係 $N$ 嘅字串 $A$ 表示。如果第 $i$ 盞燈熄咗,$A$ 嘅第 $i$ 個字元($1 \le i \le N$)就係 0;如果着咗,就係 1。我哋想用盡量少嘅操作,令啲燈變成長度係 $N$ 嘅字串 $B$ 所表示嘅狀態。如果我哋想第 $i$ 盞燈熄咗,$B$ 嘅第 $i$ 個字元($1 \le i \le N$)就係 0;如果想佢着咗,就係 1。

請寫一個程式,畀定燈嘅數目、而家嘅狀態同目標狀態,計出達到目標狀態所需嘅最少操作次數。

輸入格式

由標準輸入讀入以下資料。

$$ \begin{array}{l} N \\ A \\ B \end{array} $$

輸出格式

向標準輸出寫一行,包含達到目標狀態所需嘅最少操作次數。

數據範圍

  • $1 \le N \le 1\,000\,000$。
  • $A$ 同 $B$ 都係長度為 $N$ 嘅字串。
  • $A$ 同 $B$ 入面每個字元都係 0 或者 1。

子任務

  1. (6 分)$N \le 18$。
  2. (41 分)$N \le 2\,000$。
  3. (4 分)$A$ 入面每個字元都係 0。
  4. (49 分)冇額外限制。

樣例

輸入格式 1

8
11011100
01101001

輸出格式 1

4

說明

喺呢個輸入樣例入面,我哋可以用 4 次操作達到目標狀態,例如如下:

  1. 反轉第 $1,2,3,4$ 盞燈嘅狀態。啲燈嘅狀態變成 00101100。
  2. 着咗第 $2$ 盞燈。啲燈嘅狀態變成 01101100。
  3. 反轉第 $6,7,8$ 盞燈嘅狀態。啲燈嘅狀態變成 01101011。
  4. 熄咗第 $6$ 同第 $7$ 盞燈。啲燈嘅狀態變成 01101001。

因為唔可能用少過 4 次操作達到目標狀態,所以輸出 4。

輸入格式 2

13
1010010010100
0000111001011

輸出格式 2

3

輸入格式 3

18
001100010010000110
110110001000100101

輸出格式 3

5

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download
#255EditorialOpen题解jianglyView

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.