답안 #219292

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
219292 2020-04-05T05:18:18 Z rama_pang Lamps (JOI19_lamps) C++14
0 / 100
5 ms 384 KB
#include <bits/stdc++.h>
using namespace std;

const int INF = 1e7;

int N;
string A, B;

void Read() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  cin >> N >> A >> B;
}

int NaiveDP() {
  vector<array<array<int, 4>, 4>> dp(N + 1); // dp[pos][second_last_operation][last_operation]
  // dp[pos] means A[pos...N] = B[pos...N]
  // operation 0 = skip operation
  // operation 1 = off operation
  // operation 2 = on operation
  // operation 3 = xor operation
  

  for (int pos = N - 1; pos >= 0; pos--) {
    for (int second_last = 0; second_last < 4; second_last++) {
      for (int last = 0; last < 4; last++) {
        int &res = dp[pos][second_last][last] = INF;
        
        // operation skip (0)
        for (int nxt = pos; nxt < N; nxt++) {
          if (A[pos] != B[pos]) break;
          res = min(res, dp[nxt + 1][last][0]);
        }

        // operation off (1)
        for (int nxt = pos; nxt < N; nxt++) {
          if (B[nxt] != '0') break;
          res = min(res, 1 + dp[nxt + 1][last][1]);
          res = min(res, 1 - (second_last == 1 && last != 0 && last != 3) + dp[nxt + 1][0][1]);
        }

        // operation on (2)
        for (int nxt = pos; nxt < N; nxt++) {
          if (B[nxt] != '1') break;
          res = min(res, 1 + dp[nxt + 1][last][2]);
          res = min(res, 1 - (second_last == 2 && last != 0 && last != 3) + dp[nxt + 1][0][2]);
        }

        // operation xor (3)
        for (int nxt = pos; nxt < N; nxt++) {
          if (A[nxt] == B[nxt]) break;
          res = min(res, 1 - (second_last == 1 && last == 2) + dp[nxt + 1][last][3]);
          res = min(res, 1 - (second_last == 2 && last == 1) + dp[nxt + 1][last][3]);
        }
      }
    }
  }

  return dp[0][0][0];
}

int main() {
  Read();
  cout << NaiveDP() << "\n";
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Incorrect 4 ms 384 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Incorrect 4 ms 384 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Incorrect 5 ms 384 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Incorrect 4 ms 384 KB Output isn't correct
7 Halted 0 ms 0 KB -