Submission #102080

#TimeUsernameProblemLanguageResultExecution timeMemory
102080aintaLamps (JOI19_lamps)C++17
100 / 100
72 ms25848 KiB
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int n, w[1010000][2], D[1010000][2][2];
char p[1010000], q[1010000];
int cnt;
int res, cc = 0;
vector<int>V;
int main() {
	int i, j, k;
	scanf("%d", &n);
	scanf("%s", p + 1);
	scanf("%s", q + 1);
	p[0] = p[n + 1] = '0';
	q[0] = q[n + 1] = '0';
	for (i = 1; i <= n+1; i++) {
		w[i][0] = p[i] - '0';
		w[i][1] = q[i] - '0';
	}
	for(int ii=0;ii<=n;ii++)for (i = 0; i < 2; i++)for (j = 0; j < 2; j++)D[ii][i][j] = 1e9;
	D[0][0][0] = 0;
	for (i = 1; i <= n; i++) {
		D[i - 1][0][0] = min(D[i - 1][0][0], D[i - 1][1][0]);
		D[i - 1][0][1] = min(D[i - 1][0][1], D[i - 1][1][1]);
		for (j = 0; j < 2; j++) {
			for (k = 0; k < 2; k++) {
				if (D[i - 1][j][k] > 1e8)continue;
				int d = D[i - 1][j][k];
				if (j == 0 && k == 0) {
					if (w[i][0] == w[i][1]) {
						D[i][0][0] = min(D[i][0][0], d);
					}
					else {
						D[i][0][1] = min(D[i][0][1], d + 1);
						D[i][1][0] = min(D[i][1][0], d + 1);
					}
					continue;
				}
				if (j == 0 && k == 1) {
					if (w[i][0] != w[i][1]) {
						D[i][0][1] = min(D[i][0][1], d);
					}
					else {
						D[i][0][0] = min(D[i][0][0], d);
						D[i][1][1] = min(D[i][1][1], d + 1);
					}
				}
				if (j == 1 && k == 0) {
					if (w[i-1][1] == w[i][1]) {
						D[i][1][0] = min(D[i][1][0], d);
					}
					else {
						if (w[i][0] == w[i][1]) {
							D[i][0][0] = min(D[i][0][0], d);
						}
						else {
							D[i][0][1] = min(D[i][0][1], d + 1);
						}
						D[i][1][1] = min(D[i][1][1], d + 1);
					}
					continue;
				}
				if (j == 1 && k == 1) {
					if (w[i - 1][1] == w[i][1]) {
						D[i][1][1] = min(D[i][1][1], d);
					}
					else {
						if (w[i][0] != w[i][1]) {
							D[i][0][1] = min(D[i][0][1], d);
						}
						else {
							D[i][0][0] = min(D[i][0][0], d + 1);
						}
						D[i][1][0] = min(D[i][1][0], d);
					}
					continue;
				}
			}
		}
	}
	int r = 1e9;
	for (i = 0; i < 2; i++)for (j = 0; j < 2; j++)r = min(r, D[n][i][j]);
	printf("%d\n", r);
	return 0;
}

Compilation message (stderr)

lamp.cpp: In function 'int main()':
lamp.cpp:12:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
lamp.cpp:13:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", p + 1);
  ~~~~~^~~~~~~~~~~~~
lamp.cpp:14:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", q + 1);
  ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...