Submission #538647

#TimeUsernameProblemLanguageResultExecution timeMemory
538647Soumya1Lamps (JOI19_lamps)C++17
100 / 100
54 ms27760 KiB
#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int N = 1e6 + 5;
int dp[N][3][2]; // dp[i][x][y] = (set (0 / 1 / nill), toggle (yes / no))
int apply(int a, int x, int y) {
  if (x <= 1) a = x;
  return (a ^ y);
}
int cost(int x, int y, int prevx, int prevy) {
  int r = 0;
  if (x <= 1) r += x != prevx;
  if (y != 0) r += y != prevy;
  return r;
}
void testCase() {
  int n;
  string a, b;
  cin >> n >> a >> b;
  a = '#' + a;
  b = '#' + b;
  for (int x = 0; x < 3; x++) {
    for (int y = 0; y < 2; y++) {
      dp[0][x][y] = N * 50;
    }
  }
  dp[0][2][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int x = 0; x < 3; x++) {
      for (int y = 0; y < 2; y++) {
        dp[i][x][y] = N * 50;
        if (apply(a[i] - '0', x, y) != b[i] - '0') continue;
        for (int prevx = 0; prevx < 3; prevx++) {
          for (int prevy = 0; prevy < 2; prevy++) {
            dp[i][x][y] = min(dp[i][x][y], dp[i - 1][prevx][prevy] + cost(x, y, prevx, prevy));
          }
        }
        // debug(i, x, y, dp[i][x][y]);
      }
    }
  }
  int ans = N * 50;
  for (int x = 0; x < 3; x++) {
    for (int y = 0; y < 2; y++) {
      ans = min(ans, dp[n][x][y]);
    }
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  testCase();
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...