Submission #110504

#TimeUsernameProblemLanguageResultExecution timeMemory
110504AngusRitossaLamps (JOI19_lamps)C++14
47 / 100
1060 ms8660 KiB
#include <bits/stdc++.h>
using namespace std;
int memo[1000010][2], done[1000010][2], a[1000010], b[1000010], n;
int dp(int i, int t)
{
	if (i == n) return 0;
	if (done[i][t]) return memo[i][t];
	done[i][t] = 1;
	if (a[i] == (b[i]^t)) return memo[i][t] = dp(i+1, t);
	int ans = dp(i+1, !t) + !t; // Remove/add a toggle, therefore curr is correct
	int extracost = 1, isok = 1, istoggled = t;
	for (int j = i; j < n; j++)
	{
		if ((b[j] == b[i]) != isok)
		{
			isok = !isok;
			istoggled = !istoggled;
			if (istoggled) extracost++;
		}
		int am = dp(j+1, istoggled)+extracost;
		ans = min(ans, am);
	}
	return memo[i][t] = ans;
}
int main()
{
	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';
	}
	for (int i = n-1; i >= 0; i--)
	{
		for (int t = 0; t < 2; t++)
		{
			if (a[i] == (b[i]^t)) 
			{ 
				memo[i][t] = memo[i+1][t];
				continue;
			}
			int ans = memo[i+1][!t] + !t; // Remove/add a toggle, therefore curr is correct
			int extracost = 1, isok = 1, istoggled = t;
			for (int j = i; j < n; j++)
			{
				if ((b[j] == b[i]) != isok)
				{
					isok = !isok;
					istoggled = !istoggled;
					if (istoggled) extracost++;
				}
				int am = memo[j+1][istoggled]+extracost;
				ans = min(ans, am);
			}
		 	memo[i][t] = ans;
		}
	}
	int ans = memo[0][0];
	printf("%d\n", ans);
}

Compilation message (stderr)

lamp.cpp: In function 'int main()':
lamp.cpp:27:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
lamp.cpp:31:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %c", &c);
   ~~~~~^~~~~~~~~~~
lamp.cpp:37:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %c", &c);
   ~~~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...