H - ランプ (Lamps) 解説 /

実行時間制限: 1 sec / メモリ制限: 256 MiB

Score: 100 points

Problem Statement

There are N lamps in a row at a long hallway. The lamps are numbered from 1 to N. Each lamps has a state of either off or on. There is a special mechanism to change the states of the lamps. In an operation, we can do one of the followings:

  • Choose integers p and q with 1 \le p \le q \le N, and make the lamps p, p + 1, \ldots, q off.
  • Choose integers p and q with 1 \le p \le q \le N, and make the lamps p, p + 1, \ldots, q on.
  • Choose integers p and q with 1 \le p \le q \le N, and toggle the states of the lamps p, p + 1, \ldots, q (off to on, or on to off).

Current states of the lamps are represented by a string A of length N. The i-th (1 \le i \le N) character of A is 0 if the lamp i is off, and 1 if on. We want to make the states of the lamps to be those represented by a string B of length N, with as few operations as possible. The i-th (1 \le i \le N) character of B is 0 if we want to make the lamp i off, and 1 if on.

Write a program which, given the number of lamps, the current states and the target states, calculates the minimum number of operations needed to achieve the target states.

Input

Read the following data from the standard input.

N
A
B

Output

Write one line to the standard output. The output should contain the minimum number of operations needed to achieve the target states.

Constraints

  • 1 \le N \le 1\,000\,000.
  • A and B are strings of length N.
  • Each character in A and B is either 0 or 1.

Subtasks

  1. (6 points) N \le 18.
  2. (41 points) N \le 2\,000.
  3. (4 points) Each character in A is 0.
  4. (49 points) No additional constraints.

Sample Input and Output

Sample Input 1

8
11011100
01101001

Sample Output 1

4

In this sample input, we can achieve the target states in 4 operations, for example as follows:

  1. Toggle the states of the lamps 1, 2, 3 and 4. The states of the lamps become 00101100.
  2. Make the lamp 2 on. The states of the lamps become 01101100.
  3. Toggle the states of the lamps 6, 7 and 8. The states of the lamps become 01101011.
  4. Make the lamps 6 and 7 off. The states of the lamps become 01101001.

Since it is impossible to achieve the target states in less than 4 operations, output 4.

Sample Input 2

13
1010010010100
0000111001011

Sample Output 2

3

Sample Input 3

18
001100010010000110
110110001000100101

Sample Output 3

5

配点 : 100 点

問題文

長い廊下に N 個のランプが一列に並んでいる.ランプには左から順に 1 から N までの番号が付いている.各ランプの状態は off または on のいずれかである.

ランプの状態を変える特殊な機構が用意されており,1 回の操作で以下の 3 つのいずれかができる:

  • 1 \leqq p \leqq q \leqq N なる整数 p, q を選び,ランプ p, p + 1, \ldots, q をすべて off にする.
  • 1 \leqq p \leqq q \leqq N なる整数 p, q を選び,ランプ p, p + 1, \ldots, q をすべて on にする.
  • 1 \leqq p \leqq q \leqq N なる整数 p, q を選び,ランプ p, p + 1, \ldots, q の状態をすべて変更する (off なら on に, on なら off にする).

現在のランプの状態は長さ N の文字列 A で表される .A の i 文字目 (1 \leqq i \leqq N) は,ランプ i が off であるならば 0 であり,on であるならば 1 である.これから,できるだけ少ない回数の操作を行い,長さ N の文字列 B で表される目標の状態にしたい .B の i 文字目 (1 \leqq i \leqq N) は,ランプ i を off にしたいならば 0 であり,on にしたいならば 1 である.

ランプの個数,現在の状態と目標の状態が与えられたとき,必要な操作回数の最小値を求めるプログラムを作成せよ.

入力

入力は以下の形式で標準入力から与えられる.

N
A
B

出力

標準出力に,必要な操作回数の最小値を 1 行で出力せよ.

制約

  • 1 \leqq N \leqq 1\,000\,000.
  • A, B は長さ N の文字列である.
  • A, B の各文字は 0, 1 のいずれかである.

小課題

  1. (6 点) N \leqq 18.
  2. (41 点) N \leqq 2\,000.
  3. (4 点) A の各文字は 0 である.
  4. (49 点) 追加の制約はない.

入出力例

入力例 1

8
11011100
01101001

出力例 1

4

この入力例では,例えば以下のような 4 回の操作によって目標の状態を作ることができる:

  1. ランプ 1, 2, 3, 4 の状態を変更する.ランプの状態は 00101100 になる.
  2. ランプ 2 を on にする.ランプの状態は 01101100 になる.
  3. ランプ 6, 7, 8 の状態を変更する.ランプの状態は 01101011 になる.
  4. ランプ 6, 7 を off にする.ランプの状態は 01101001 になる.

3 回以下の操作では目標の状態を作ることはできないので,4 を出力する.

入力例 2

13
1010010010100
0000111001011

出力例 2

3

入力例 3

18
001100010010000110
110110001000100101

出力例 3

5

出典

JOI 2018/2019 春合宿 Contest Day3 問題2