#include "hiccup.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
#define __h 0
#define __hc 1
#define __t 2
int HicCup(std::string s) {
int n = s.size();
int p = 0, q = 0;
for (int i = 0; i < n; i++) {
if (i && s[i - 1] == 'H' && s[i] == '!')
return -1;
if (s[i] == 'H') p++;
if (s[i] == 'C') q++;
}
int r = n - p - q;
if (p != q) return -1;
if (p == 0) return -1;
if (s[0] == '!') return -1;
int R = r / p + 1;
int L = 0;
int u = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'H') u++;
else if (s[i] == 'C') {
if (u > 0) u--;
else return -1;
}
}
// [L, R)
while (L + 1 < R) {
int M = (L + R) / 2;
int flag = 1;
vector<PII> f;
// cout << "========================== " << M << endl;
for (int i = 0; i < n; i++) {
// cout << i << ' ' << s[i] << endl;
if (s[i] == 'H') f.push_back(PII(__h, 1));
else if (s[i] == 'C') {
while (f.size() && f.back().first == __t) f.pop_back();
if (!f.size() || f.back().first == __hc) {
flag = 0;
goto KKE;
}
f.pop_back();
f.push_back(PII(__hc, 1));
}
else if (s[i] == '!') {
if (f.size() && f.back().first == __t) f.back().second++;
else f.push_back(PII(__t, 1));
if (f.back().second >= M) {
f.pop_back();
if (f.size()) {
if (f.back().first == __hc) {
f.pop_back();
}
}
}
}
// cout << i << ' ' << flag << ' ' << f.size();
// for (auto &x : f)
// cout << " " << x.first << ',' << x.second; cout << endl;
}
// cout << "???" << endl;
while (f.size() && f.back().first == __t) f.pop_back();
// cout << flag << ' ' << f.size() << endl;
if (f.size()) flag = 0;
KKE:
// cout << "flag : " << flag << endl;
if (flag) L = M;
else R = M;
}
return L;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
26 ms |
3328 KB |
Output is correct |
6 |
Correct |
18 ms |
3328 KB |
Output is correct |
7 |
Correct |
18 ms |
3328 KB |
Output is correct |
8 |
Correct |
24 ms |
3328 KB |
Output is correct |
9 |
Correct |
25 ms |
3328 KB |
Output is correct |
10 |
Correct |
18 ms |
3328 KB |
Output is correct |
11 |
Correct |
5 ms |
432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
26 ms |
3328 KB |
Output is correct |
6 |
Correct |
18 ms |
3328 KB |
Output is correct |
7 |
Correct |
18 ms |
3328 KB |
Output is correct |
8 |
Correct |
24 ms |
3328 KB |
Output is correct |
9 |
Correct |
25 ms |
3328 KB |
Output is correct |
10 |
Correct |
18 ms |
3328 KB |
Output is correct |
11 |
Correct |
19 ms |
3328 KB |
Output is correct |
12 |
Correct |
14 ms |
3328 KB |
Output is correct |
13 |
Correct |
14 ms |
3328 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
14 ms |
3328 KB |
Output is correct |
16 |
Correct |
5 ms |
128 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
640 KB |
Output is correct |
19 |
Correct |
42 ms |
3328 KB |
Output is correct |
20 |
Correct |
31 ms |
3328 KB |
Output is correct |
21 |
Correct |
42 ms |
3328 KB |
Output is correct |
22 |
Correct |
69 ms |
3200 KB |
Output is correct |
23 |
Correct |
77 ms |
3328 KB |
Output is correct |
24 |
Correct |
21 ms |
3328 KB |
Output is correct |
25 |
Correct |
24 ms |
3328 KB |
Output is correct |
26 |
Correct |
30 ms |
3328 KB |
Output is correct |
27 |
Correct |
51 ms |
3328 KB |
Output is correct |
28 |
Correct |
59 ms |
3072 KB |
Output is correct |
29 |
Correct |
66 ms |
3328 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
10 ms |
640 KB |
Output is correct |
33 |
Correct |
5 ms |
432 KB |
Output is correct |