Submission #483173

#TimeUsernameProblemLanguageResultExecution timeMemory
483173hollwo_pelwLamps (JOI19_lamps)C++17
100 / 100
27 ms14016 KiB
/*
// is short or still long ???
hollwo_pelw's template(short)
// Note : -Dhollwo_pelw_local
*/

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>

using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;

void FAST_IO(string filein = "", string fileout = "", string fileerr = ""){
	if (fopen(filein.c_str(), "r")){
		freopen(filein.c_str(), "r", stdin);
		freopen(fileout.c_str(), "w", stdout);
#ifdef hollwo_pelw_local
		freopen(fileerr.c_str(), "w", stderr);
#endif
	}
	cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
}

void Hollwo_Pelw();

signed main(){
#ifdef hollwo_pelw_local
	FAST_IO("input.inp", "output.out", "error.err");
	auto start = chrono::steady_clock::now();
#else
	FAST_IO("JOI19_lamps.inp", "JOI19_lamps.out");
#endif
	int testcases = 1;
	// cin >> testcases;
	for (int test = 1; test <= testcases; test++){
		// cout << "Case #" << test << ": ";
		Hollwo_Pelw();
	}
#ifdef hollwo_pelw_local
	auto end = chrono::steady_clock::now();
	cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
	return 0;
}

const int N = 1e6 + 5;

// 0 = nothing
// 1 = set 0
// 2 = set 1
// -> !eq ? flip : not

int n, dp[N][3];
char a[N], b[N];

inline void chkmin(int &a, int b) { a = a < b ? a : b; }

void Hollwo_Pelw() {
	cin >> n >> (a + 1) >> (b + 1);
	memset(dp, 0x3f, sizeof dp);

	dp[0][0] = 0;
	for (int i = 1; i <= n; i++) for (int j = 0; j < 3; j++) {
		char cur = !j ? a[i] : (j - 1 + '0');
		bool flip = cur != b[i];

		chkmin(dp[i][j], dp[i - 1][0]
			+ (a[i - 1] == b[i - 1]) * flip // start flip
			+ (j != 0) // start set (0 / 1)
		);

		chkmin(dp[i][j], dp[i - 1][1]
			+ (i == 1 || b[i - 1] == '0') * flip // start flip
			+ (j == 2) // start set (0 / 1)
		);

		chkmin(dp[i][j], dp[i - 1][2]
			+ (i == 1 || b[i - 1] == '1') * flip // start flip
			+ (j == 1) // start set (0 / 1)
		);
	}

	cout << *min_element(dp[n], dp[n] + 3);
}

Compilation message (stderr)

lamp.cpp: In function 'void FAST_IO(std::string, std::string, std::string)':
lamp.cpp:18:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |   freopen(filein.c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
lamp.cpp:19:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |   freopen(fileout.c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...