#include <bits/stdc++.h>
using namespace std;
#define int long long
//0 là giữ nguyên
//1 là flip
//2 là gán 0
//3 là gán 0 + flip
//4 là gán 1
//5 là gán 1 + flip
int flip[] = {0, 1, 0, 1, 0, 1};
int assign[] = {0, 0, 1, 1, 2, 2};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
//có một cách chọn tối ưu mà các đoạn set không giao nhau/các đoạn flip cũng không giao nhau.
//set và flip có thể giao
//luôn có cách chọn tối ưu. mà thao tác set trước toggle
int n; cin >> n;
string a, b; cin >> a >> b;
vector<int> dp(6, 1e9);
dp[0] = 0;
for(int i = 0; i < n; i++){
vector<int> nxt(6, 1e9);
for(int k = 0; k < 6; k++){
int val;
if(assign[k] == 0) val = (a[i] - '0');
else if(assign[k] == 1) val = 0;
else val = 1;
if(flip[k]) val ^= 1;
if(val != (b[i] - '0')) continue; //ta tính giá trị ở state này
for(int j = 0; j < 6; j++){
if(dp[j] == 1e9) continue;
int cost = dp[j];
if(flip[k] && !flip[j]) cost++;
if(assign[k] && assign[k] != assign[j]) cost++; //tính cost để từ dp j qua nxt k
nxt[k] = min(nxt[k], cost);
}
}
dp = nxt;
}
int ans = 1e9;
for (int i = 0; i < 6; i++) ans = min(ans, dp[i]);
cout << ans;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |