This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e6 + 500;
int A[N], B[N], dp[N][2][8], n;
int f(int x, int inv, int msk){
if(x == n) return 0;
if(dp[x][inv][msk] != -1) return dp[x][inv][msk];
int ret = N;
if(msk & 1)
ret = min(ret, f(x, inv, msk - 1));
if(msk & 2)
ret = min(ret, f(x, inv, msk - 2));
if(msk & 4)
ret = min(ret, f(x, inv, msk - 4));
if(msk == 4)
ret = min(ret, f(x, inv, 1));
int zlm = B[x] ^ inv, jos = 0, ol = msk;
if(A[x] == 0 && zlm == 0){
if(msk & 4) msk -= 4;
if((msk & 3) == 1)
msk = 0;
}
if(A[x] == 1 && zlm == 1){
if((msk & 6) == 2)
msk -= 2;
}
if(A[x] == 1 && zlm == 0){
if(msk & 4) msk -= 4;
if(!(msk & 2))
msk += 2, jos++;
}
if(A[x] == 0 && zlm == 1){
if(msk == 3)
msk = 1;
if(msk == 2)
msk += 4, jos++;
if(msk == 0)
msk = 1, jos++;
}
ret = min(ret, jos + min(f(x + 1, inv, msk), f(x + 1, !inv, msk) + (!inv)));
return dp[x][inv][ol] = ret;
}
int main(){
memset(dp, -1, sizeof(dp));
scanf("%d", &n);
for(int i = 0;i < n;i++){
char c; scanf(" %c", &c);
A[i] = c - '0';
}
for(int i = 0;i < n;i++){
char c; scanf(" %c", &c);
B[i] = c - '0';
}
printf("%d\n", min(f(0, 0, 0), 1 + f(0, 0, 1)));
}
Compilation message (stderr)
lamp.cpp: In function 'int main()':
lamp.cpp:53:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
lamp.cpp:55:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
char c; scanf(" %c", &c);
~~~~~^~~~~~~~~~~
lamp.cpp:59:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
char c; scanf(" %c", &c);
~~~~~^~~~~~~~~~~
# | 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... |