QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#297311 | #6628. Flip it and Stick it | Max_s_xaM | 0 | 0ms | 0kb | C++14 | 5.9kb | 2024-01-04 10:24:18 | 2024-01-04 10:24:18 |
answer
#include <iostream>
#include <algorithm>
#include <cstring>
typedef long long ll;
typedef double lf;
// #define DEBUG 1
struct IO
{
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
char buf[MAXSIZE], *p1, *p2;
char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
IO() : p1(buf), p2(buf), pp(pbuf) {}
~IO() {fwrite(pbuf, 1, pp - pbuf, stdout);}
#endif
#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? ' ' : *p1++)
#define blank(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')
template <typename T>
void Read(T &x)
{
#if DEBUG
std::cin >> x;
#else
bool sign = 0; char ch = gc(); x = 0;
for (; !isdigit(ch); ch = gc())
if (ch == '-') sign = 1;
for (; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ 48);
if (sign) x = -x;
#endif
}
void Read(char *s)
{
#if DEBUG
std::cin >> s;
#else
char ch = gc();
for (; blank(ch); ch = gc());
for (; !blank(ch); ch = gc()) *s++ = ch;
*s = 0;
#endif
}
void Read(char &c) {for (c = gc(); blank(c); c = gc());}
void Push(const char &c)
{
#if DEBUG
putchar(c);
#else
if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
*pp++ = c;
#endif
}
template <typename T>
void Write(T x)
{
if (x < 0) x = -x, Push('-');
static T sta[35];
int top = 0;
do sta[top++] = x % 10, x /= 10; while (x);
while (top) Push(sta[--top] ^ 48);
}
template <typename T>
void Write(T x, char lst) {Write(x), Push(lst);}
} IO;
#define Read(x) IO.Read(x)
#define Write(x, y) IO.Write(x, y)
#define Put(x) IO.Push(x)
using namespace std;
const int MAXN = 2e5 + 10;
int n, m;
char s[MAXN], t[4];
int cnt0, cnt1;
inline void Flip()
{
for (int i = 1; i <= n; i++) s[i] = (s[i] == '0' ? '1' : '0');
for (int i = 1; i <= m; i++) t[i] = (t[i] == '0' ? '1' : '0');
}
inline void Solution1()
{
bool flag = 1;
for (int i = 1; i <= n; i++)
if (s[i] == '0') {flag = 0; break;}
if (flag) cout << "0\n";
else cout << "-1\n";
}
inline void Solution2_1()
{
if (cnt0 > cnt1 + 1) return cout << "-1\n", void();
int ans = 0;
for (int i = 1; i < n; i++)
if (s[i] == '0' && s[i + 1] == '0') ans++;
cout << ans << "\n";
}
inline void Solution2_2()
{
int ans = 0;
for (int i = 2; i <= n; i++)
if (s[i - 1] == '0' && s[i] == '1') ans++;
cout << ans << "\n";
}
char str[MAXN];
int top;
inline void Solution3_1()
{
if (cnt0 > cnt1 * 2 + 2) return cout << "-1\n", void();
if (cnt0 == cnt1 * 2 + 2)
{
int sum = 0, ans = 0;
for (int i = 1; i <= n; i++)
{
if (s[i] == '0') str[++top] = '0';
else if (top == 2 && str[1] == '0' && str[2] == '0') str[top = 1] = '1';
else if (top > 2 && str[top - 2] == '1' && str[top - 1] == '0' && str[top] == '0') top -= 2;
else str[++top] = '1';
}
if (top >= 3 && str[top - 2] == '1' && str[top - 1] == '0' && str[top] == '0') top -= 2;
cnt0 = cnt1 = 0;
for (int i = 1; i <= top; i++)
cnt0 += (str[i] == '0'), cnt1 += (str[i] == '1');
for (int i = 1; i <= top; i++)
{
if (str[i] == '0') sum++;
if (str[i] == '0' && sum % 2 == 0 && sum != cnt0 && str[i + 1] == '1') ans++;
}
cout << cnt1 - ans << "\n";
}
else
{
int ans = 0;
for (int i = 3; i <= n; i++)
if (s[i - 2] == '0' && s[i - 1] == '0' && s[i] == '0') ans++, i++;
cout << ans << "\n";
}
// int res = 0, sum = 0;
// for (int i = 2; i <= n; i++)
// {
// if (i == 2 && s[1] == '0' && s[2] == '1') res++;
// if (i == 3 && s[1] == '0' && s[2] == '0' && s[3] == '1') sum++;
// if (i == n && s[i - 1] == '1' && s[i] == '0') res++;
// if (i == n && s[i - 2] == '1' && s[i - 1] == '0' && s[i] == '0') sum++;
// if (i > 2 && s[i - 2] == '1' && s[i - 1] == '0' && s[i] == '1') res++;
// if (i > 3 && s[i - 3] == '1' && s[i - 2] == '0' && s[i - 1] == '0' && s[i] == '1') sum++;
// }
// cnt0 -= sum * 2, cnt1 -= sum;
// if (cnt0 - res <= (cnt1 - res) * 2 + 2)
// {
// int ans = 0;
// for (int i = 3; i <= n; i++)
// if (s[i - 2] == '0' && s[i - 1] == '0' && s[i] == '0') ans++, i++;
// cout << ans << "\n";
// }
// else
// {
// int x = 2 * cnt1 - cnt0 + 2;
// cnt0 -= x, cnt1 -= x;
// cout << (cnt0 - 1) / 2 << "\n";
// }
}
inline void Solution3_2()
{
int ans = 0;
for (int i = 2; i < n; i++)
if (s[i - 1] == '0' && s[i] == '1' && s[i + 1] == '0')
ans++;
cout << (ans + 1) / 2 << "\n";
}
inline void Solution3_3()
{
if (t[2] == '1') reverse(s + 1, s + n + 1), Flip();
int ans = 0;
for (int i = 3; i <= n; i++)
if (s[i] == '1' && s[i - 1] == '0' && s[i - 2] == '0')
ans++;
cout << ans << "\n";
}
int main()
{
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#if DEBUG
#else
ios::sync_with_stdio(0), cin.tie(0);
#endif
Read(s + 1), Read(t + 1), n = strlen(s + 1), m = strlen(t + 1);
if (n < m) return cout << "0\n", 0;
if (t[1] == '1') Flip();
for (int i = 1; i <= n; i++) cnt0 += (s[i] == '0'), cnt1 += (s[i] == '1');
if (m == 1) Solution1();
else if (m == 2 && t[1] == t[2]) Solution2_1();
else if (m == 2) Solution2_2();
else if (m == 3 && t[1] == t[2] && t[2] == t[3]) Solution3_1();
else if (m == 3 && t[1] == t[3]) Solution3_2();
else Solution3_3();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Dangerous Syscalls
Test #1:
score: 0
Dangerous Syscalls
input:
1 0
output:
result:
Subtask #2:
score: 0
Dangerous Syscalls
Test #8:
score: 0
Dangerous Syscalls
input:
0 01
output:
result:
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Dangerous Syscalls
Test #40:
score: 0
Dangerous Syscalls
input:
11 011
output:
result:
Subtask #5:
score: 0
Dangerous Syscalls
Test #53:
score: 0
Dangerous Syscalls
input:
11 011
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #4:
0%