Submission #1093596

# Submission time Handle Problem Language Result Execution time Memory
1093596 2024-09-27T05:47:32 Z IBory Flip it and Stick it (CCO23_day2problem1) C++17
18 / 25
34 ms 10432 KB
#include <bits/stdc++.h>
using namespace std;

int Count(string S, string T) {
	int ret = 0;
	for (int i = 0; i + T.size() <= S.size(); ++i) {
		ret += (S.substr(i, T.size()) == T);
	}
	return ret;
}

int Solve(string S, string T) {
	if (T.size() == 1) return (S.find(T) == string::npos ? 0 : -1);
	int N = S.size();

	if (T == "011" || T == "100") {
		reverse(S.begin(), S.end());
		reverse(T.begin(), T.end());
	}
	if (T[0] == '1') {
		for (char& c : S) c ^= 1;
		for (char& c : T) c ^= 1;
	}

	int zero = 0;
	for (char c : S) zero += (c == '0');

	if (T == "01") return Count(S, T);
	if (T == "00") {
		int lim = (N + 1) / 2;
		if (lim < zero) return -1;
		return Count(S, T);
	}

	if (T == "001") return Count(S, T);
	if (T == "010") return (Count(S, T) + 1) / 2;
	if (T == "000") {
		int lim = N - N / 3;
		if (lim < zero) return -1;
		
		int ret = 0;
		for (int t = 0; t < 2; ++t) {
			for (int i = 0; i + 3 <= N; ++i) {
				if (S.back() == '0') break;
				if (S[i] == '1' || S[i + 1] == '1' || S[i + 2] == '1') continue;
				ret++;
				reverse(S.begin() + i + 2, S.end());
				break;
			}
			reverse(S.begin(), S.end());
		}

		multiset<int> X;
		int cur = 0;
		for (char c : S) {
			if (c == '0') cur++;
			else {
				X.insert(cur);
				cur = 0;
			}
		}
		X.insert(cur);

		while (3 <= *X.rbegin()) {
			ret++;
			int a = *X.begin(), b = *X.rbegin();
			int n = min(2 - a, b - 2);
			X.erase(X.find(a));
			X.erase(X.find(b));
			X.insert(a + n);
			X.insert(b - n);
		}

		return ret;
	}
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	string S, T;
	cin >> S >> T;
	cout << Solve(S, T);
	return 0;
}

Compilation message

Main.cpp: In function 'int Solve(std::string, std::string)':
Main.cpp:76:1: warning: control reaches end of non-void function [-Wreturn-type]
   76 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 996 KB Output is correct
4 Correct 1 ms 1156 KB Output is correct
5 Correct 1 ms 992 KB Output is correct
6 Correct 1 ms 996 KB Output is correct
7 Correct 1 ms 996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 3 ms 1252 KB Output is correct
7 Correct 3 ms 1252 KB Output is correct
8 Correct 3 ms 1380 KB Output is correct
9 Correct 3 ms 1252 KB Output is correct
10 Correct 3 ms 1252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 3 ms 1252 KB Output is correct
7 Correct 3 ms 1252 KB Output is correct
8 Correct 3 ms 1380 KB Output is correct
9 Correct 3 ms 1252 KB Output is correct
10 Correct 3 ms 1252 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 456 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 3 ms 1244 KB Output is correct
21 Correct 3 ms 1380 KB Output is correct
22 Correct 3 ms 1248 KB Output is correct
23 Correct 3 ms 1252 KB Output is correct
24 Correct 3 ms 1252 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 3 ms 1380 KB Output is correct
27 Correct 3 ms 1376 KB Output is correct
28 Correct 1 ms 996 KB Output is correct
29 Correct 3 ms 1384 KB Output is correct
30 Correct 3 ms 1252 KB Output is correct
31 Correct 3 ms 1248 KB Output is correct
32 Correct 3 ms 1252 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 3 ms 1252 KB Output is correct
8 Correct 3 ms 1252 KB Output is correct
9 Correct 3 ms 1252 KB Output is correct
10 Correct 3 ms 1252 KB Output is correct
11 Correct 2 ms 1252 KB Output is correct
12 Correct 3 ms 1252 KB Output is correct
13 Correct 3 ms 1188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 600 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 3 ms 1252 KB Output is correct
11 Correct 5 ms 1252 KB Output is correct
12 Correct 3 ms 1252 KB Output is correct
13 Correct 3 ms 1252 KB Output is correct
14 Correct 3 ms 1252 KB Output is correct
15 Correct 3 ms 1252 KB Output is correct
16 Correct 3 ms 1252 KB Output is correct
17 Correct 2 ms 1252 KB Output is correct
18 Correct 3 ms 1252 KB Output is correct
19 Correct 2 ms 1252 KB Output is correct
20 Correct 3 ms 1252 KB Output is correct
21 Correct 2 ms 1252 KB Output is correct
22 Correct 3 ms 1252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 3 ms 1252 KB Output is correct
8 Correct 3 ms 1252 KB Output is correct
9 Correct 3 ms 1252 KB Output is correct
10 Correct 3 ms 1252 KB Output is correct
11 Correct 2 ms 1252 KB Output is correct
12 Correct 3 ms 1252 KB Output is correct
13 Correct 3 ms 1188 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 600 KB Output is correct
20 Correct 0 ms 600 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 3 ms 1252 KB Output is correct
24 Correct 5 ms 1252 KB Output is correct
25 Correct 3 ms 1252 KB Output is correct
26 Correct 3 ms 1252 KB Output is correct
27 Correct 3 ms 1252 KB Output is correct
28 Correct 3 ms 1252 KB Output is correct
29 Correct 3 ms 1252 KB Output is correct
30 Correct 2 ms 1252 KB Output is correct
31 Correct 3 ms 1252 KB Output is correct
32 Correct 2 ms 1252 KB Output is correct
33 Correct 3 ms 1252 KB Output is correct
34 Correct 2 ms 1252 KB Output is correct
35 Correct 3 ms 1252 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 32 ms 10432 KB Output is correct
39 Correct 18 ms 4068 KB Output is correct
40 Correct 1 ms 996 KB Output is correct
41 Correct 34 ms 4068 KB Output is correct
42 Correct 34 ms 4064 KB Output is correct
43 Correct 25 ms 4068 KB Output is correct
44 Correct 33 ms 5600 KB Output is correct
45 Correct 1 ms 992 KB Output is correct
46 Correct 22 ms 4320 KB Output is correct
47 Incorrect 19 ms 4068 KB Output isn't correct
48 Halted 0 ms 0 KB -