#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <numeric>
using namespace std;
string s, t;
void flip(string &s)
{
for (int i = 0; i < (int)s.size(); i++) {
s[i] = s[i] == '0' ? '1' : '0';
}
}
/*int minimum_cost2(vector<int> v)
{
bool dp[10][10][10] = {};
int zero = 0, one = 0;
vector<int> up;
for (int i:v) {
if (i == 0) zero++;
else if (i == 1) one++;
else up.push_back(i);
}
if (up.empty()) return 0;
dp[0][0][0] = true;
for (int i = 0; i < up.size(); i++) {
for (int j = 0; j <= zero; j++) {
for (int k = 0; k <= one; k++) {
//dp[i][j][k] ���
for (int p = 0; p <= up[i] / 2; p++) { //up[i]�� 0 p��
int q = up[i] - 2 * p;
if (j >= p && k >= q) dp[i+1][j][k] |= dp[i][j-p][k-q];
}
}
}
}
int ans = 20;
for (int j = 0; j <= zero; j++) {
for (int k = 0; k <= one; k++) {
if (dp[(int)up.size()-1][])
}
}
}*/
int minimum_cost(vector<int> v)
{
int zr = accumulate(v.begin(), v.end(), 0), on = (int)v.size() - 1;
if (zr > 2 * (on + 1)) return -1;
int one = 0, zero = 0;
vector<int> up;
for (int i:v) {
if (i == 1) one++;
else if (i == 0) zero++;
else if (i >= 3) up.push_back(i);
}
int req_one = 0;
for (int &i:up) {
if (i % 2 == 1) {
req_one++; i--;
}
}
int ans = 0;
ans += req_one;
if (one > req_one) {
one -= req_one;
}
else {
req_one -= one;
one = 0;
zero -= (req_one + 1) / 2;
}
int req_ev = 0;
for (int i:up) req_ev += (i - 2) / 2;
if (req_ev <= zero) {
ans += req_ev;
}
else {
ans += zero; req_ev -= zero;
ans += req_ev * 2;
}
return ans;
}
int main()
{
/* int n; cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
cout << minimum_cost(v) << '\n';
return 0;*/
cin >> s >> t;
int n = s.size();
int k = t.size();
if (k == 1) {
bool ouc[2];
for (int i = 0; i < n; i++) ouc[s[i]-'0'] = true;
if (ouc[t[0]-'0']) cout << -1;
else cout << 0;
}
else if (k == 2) {
if (t[0] != t[1]) {
if (t[0] == '1') flip(s);
int ans = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '1') continue;
while (i > 0 && s[i-1] == '0') --i;
ans++;
}
if (s[n-1] == '0') --ans;
cout << ans;
}
else {
if (t[0] == '1') flip(s);
int ans = 0;
int cnt = 0;
for (int i = 0; i < n; i++) cnt += s[i] == '0';
if (cnt > (n+1)/2) cout << -1;
else {
for (int i = 0; i < n; i++) {
if (s[i] == '1') continue;
int l = i;
while (i + 1 < n && s[i] == s[i+1]) i++;
int r = i;
ans += r - l;
}
cout << ans;
}
}
}
else {
if (t[0] != t[2]) {
if (t[0] == '1') {
flip(s); flip(t);
}
if (t[1] == '1') {
t[1] = '0';
flip(s);
reverse(s.begin(), s.end());
}
int cnt = 0;
int st = n - 1;
while (st >= 0 && s[st] == '0') st--;
for (int i = st; i >= 0; i--) {
if (s[i] == '1') continue;
int l = i;
while (i > 0 && s[i-1] == '0') i--;
int r = i;
cnt += l - r >= 1;
}
cout << cnt;
}
else if (t[0] != t[1]) {
if (t[0] == '1') flip(s);
int cnt = 0;
for (int i = 1; i < n - 1; i++) {
if (s[i-1] == '0' && s[i] == '1' && s[i+1] == '0') cnt++;
}
cout << (cnt + 1) / 2;
}
else {
if (t[0] == '1') flip(s);
vector<int> v;
for (int i = 0; i < n; i++) {
if (s[i] == '1') continue;
int l = i;
while (i < n - 1 && s[i+1] == '0') i++;
int r = i;
v.push_back(r - l + 1);
}
for (int i = 0; i < n - 1; i++) {
if (s[i] == '1' && s[i+1] == '1') v.push_back(0);
}
if (s[0] == '1') v.push_back(0);
if (s[n-1] == '1') v.push_back(0);
cout << minimum_cost(v);
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
612 KB |
Output is correct |
4 |
Correct |
3 ms |
612 KB |
Output is correct |
5 |
Correct |
3 ms |
612 KB |
Output is correct |
6 |
Correct |
3 ms |
612 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
612 KB |
Output is correct |
7 |
Correct |
3 ms |
612 KB |
Output is correct |
8 |
Correct |
3 ms |
612 KB |
Output is correct |
9 |
Correct |
4 ms |
612 KB |
Output is correct |
10 |
Correct |
4 ms |
612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
612 KB |
Output is correct |
7 |
Correct |
3 ms |
612 KB |
Output is correct |
8 |
Correct |
3 ms |
612 KB |
Output is correct |
9 |
Correct |
4 ms |
612 KB |
Output is correct |
10 |
Correct |
4 ms |
612 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
3 ms |
612 KB |
Output is correct |
21 |
Correct |
4 ms |
612 KB |
Output is correct |
22 |
Correct |
4 ms |
612 KB |
Output is correct |
23 |
Correct |
4 ms |
612 KB |
Output is correct |
24 |
Correct |
4 ms |
612 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
4 ms |
612 KB |
Output is correct |
27 |
Correct |
5 ms |
612 KB |
Output is correct |
28 |
Correct |
3 ms |
612 KB |
Output is correct |
29 |
Correct |
5 ms |
612 KB |
Output is correct |
30 |
Correct |
4 ms |
612 KB |
Output is correct |
31 |
Correct |
6 ms |
612 KB |
Output is correct |
32 |
Correct |
4 ms |
612 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
612 KB |
Output is correct |
8 |
Correct |
5 ms |
660 KB |
Output is correct |
9 |
Correct |
4 ms |
612 KB |
Output is correct |
10 |
Correct |
4 ms |
612 KB |
Output is correct |
11 |
Correct |
4 ms |
612 KB |
Output is correct |
12 |
Correct |
5 ms |
612 KB |
Output is correct |
13 |
Correct |
4 ms |
612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
4 ms |
612 KB |
Output is correct |
11 |
Correct |
4 ms |
612 KB |
Output is correct |
12 |
Correct |
4 ms |
612 KB |
Output is correct |
13 |
Correct |
4 ms |
612 KB |
Output is correct |
14 |
Correct |
4 ms |
628 KB |
Output is correct |
15 |
Correct |
4 ms |
612 KB |
Output is correct |
16 |
Correct |
4 ms |
612 KB |
Output is correct |
17 |
Correct |
4 ms |
612 KB |
Output is correct |
18 |
Correct |
3 ms |
612 KB |
Output is correct |
19 |
Correct |
4 ms |
616 KB |
Output is correct |
20 |
Correct |
3 ms |
612 KB |
Output is correct |
21 |
Correct |
3 ms |
612 KB |
Output is correct |
22 |
Correct |
3 ms |
612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
612 KB |
Output is correct |
8 |
Correct |
5 ms |
660 KB |
Output is correct |
9 |
Correct |
4 ms |
612 KB |
Output is correct |
10 |
Correct |
4 ms |
612 KB |
Output is correct |
11 |
Correct |
4 ms |
612 KB |
Output is correct |
12 |
Correct |
5 ms |
612 KB |
Output is correct |
13 |
Correct |
4 ms |
612 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
4 ms |
612 KB |
Output is correct |
24 |
Correct |
4 ms |
612 KB |
Output is correct |
25 |
Correct |
4 ms |
612 KB |
Output is correct |
26 |
Correct |
4 ms |
612 KB |
Output is correct |
27 |
Correct |
4 ms |
628 KB |
Output is correct |
28 |
Correct |
4 ms |
612 KB |
Output is correct |
29 |
Correct |
4 ms |
612 KB |
Output is correct |
30 |
Correct |
4 ms |
612 KB |
Output is correct |
31 |
Correct |
3 ms |
612 KB |
Output is correct |
32 |
Correct |
4 ms |
616 KB |
Output is correct |
33 |
Correct |
3 ms |
612 KB |
Output is correct |
34 |
Correct |
3 ms |
612 KB |
Output is correct |
35 |
Correct |
3 ms |
612 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
5 ms |
2136 KB |
Output is correct |
39 |
Correct |
6 ms |
1120 KB |
Output is correct |
40 |
Correct |
7 ms |
1108 KB |
Output is correct |
41 |
Correct |
6 ms |
1120 KB |
Output is correct |
42 |
Correct |
5 ms |
1120 KB |
Output is correct |
43 |
Correct |
4 ms |
1120 KB |
Output is correct |
44 |
Correct |
4 ms |
1372 KB |
Output is correct |
45 |
Correct |
7 ms |
1120 KB |
Output is correct |
46 |
Correct |
4 ms |
1372 KB |
Output is correct |
47 |
Correct |
4 ms |
1244 KB |
Output is correct |
48 |
Correct |
5 ms |
1120 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
5 ms |
612 KB |
Output is correct |
55 |
Correct |
5 ms |
612 KB |
Output is correct |
56 |
Correct |
7 ms |
612 KB |
Output is correct |
57 |
Correct |
4 ms |
612 KB |
Output is correct |
58 |
Correct |
6 ms |
612 KB |
Output is correct |
59 |
Correct |
4 ms |
600 KB |
Output is correct |
60 |
Correct |
4 ms |
612 KB |
Output is correct |
61 |
Correct |
0 ms |
212 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
1 ms |
212 KB |
Output is correct |
64 |
Correct |
0 ms |
212 KB |
Output is correct |
65 |
Correct |
4 ms |
612 KB |
Output is correct |
66 |
Correct |
4 ms |
596 KB |
Output is correct |
67 |
Correct |
6 ms |
676 KB |
Output is correct |
68 |
Correct |
4 ms |
612 KB |
Output is correct |
69 |
Correct |
3 ms |
612 KB |
Output is correct |
70 |
Correct |
3 ms |
612 KB |
Output is correct |
71 |
Correct |
4 ms |
612 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |