#include "hiccup.h"
#include <stack>
using namespace std;
struct State {
int cur_state = 0;
int want = 0;
int closed = 0;
// 0 = need C, 1 = need !
int update(char ch) {
if (ch == 'C' && cur_state == 0) {
cur_state = 1;
return 1;
}
if (ch == '!' && cur_state == 1) {
want--;
return 1;
}
return 0;
}
};
int check(int x, const string& S) {
if (x == -1) return 1;
stack<State> ST;
int closed = 0;
for (char ch : S) {
//printf("processing %d, %c\n", x, ch);
if (ch == 'H') {
ST.push(State());
ST.top().want = x;
continue;
}
else if (ch == 'C') {
if (ST.empty()) return 0;
if (!ST.top().update(ch)) return 0;
if (ST.top().want == 0) {
ST.pop();
if (ST.empty()) closed = 1;
else ST.top().closed = 1;
//printf("Popped\n");
}
}
else {
if (ST.empty() && !closed) return 0;
if (ST.empty() && closed) {
//printf("consumed\n");
continue;
}
if (!ST.top().update(ch)) {
if (ST.top().closed) {
//printf("consumed\n");
continue;
}
return 0;
}
if (ST.top().want == 0) {
ST.pop();
if (ST.empty()) closed = 1;
else ST.top().closed = 1;
//printf("Popped\n");
}
}
}
return ST.empty();
}
int HicCup(std::string S) {
int cntH = 0, cntSign = 0;
for (char ch : S) cntH += ch == 'H', cntSign += ch == '!';
int lo = -1, hi = cntSign/cntH;
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
if (check(mid, S)) lo = mid;
else hi = mid - 1;
}
return lo;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 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 |
27 ms |
3328 KB |
Output is correct |
6 |
Correct |
15 ms |
3328 KB |
Output is correct |
7 |
Correct |
14 ms |
3328 KB |
Output is correct |
8 |
Correct |
25 ms |
3328 KB |
Output is correct |
9 |
Correct |
26 ms |
3384 KB |
Output is correct |
10 |
Correct |
14 ms |
3328 KB |
Output is correct |
11 |
Correct |
5 ms |
128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 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 |
27 ms |
3328 KB |
Output is correct |
6 |
Correct |
15 ms |
3328 KB |
Output is correct |
7 |
Correct |
14 ms |
3328 KB |
Output is correct |
8 |
Correct |
25 ms |
3328 KB |
Output is correct |
9 |
Correct |
26 ms |
3384 KB |
Output is correct |
10 |
Correct |
14 ms |
3328 KB |
Output is correct |
11 |
Correct |
15 ms |
3328 KB |
Output is correct |
12 |
Correct |
19 ms |
3328 KB |
Output is correct |
13 |
Correct |
16 ms |
3328 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
15 ms |
3328 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
640 KB |
Output is correct |
19 |
Correct |
38 ms |
3328 KB |
Output is correct |
20 |
Correct |
27 ms |
3328 KB |
Output is correct |
21 |
Correct |
39 ms |
3328 KB |
Output is correct |
22 |
Correct |
65 ms |
3200 KB |
Output is correct |
23 |
Correct |
73 ms |
3328 KB |
Output is correct |
24 |
Correct |
20 ms |
3328 KB |
Output is correct |
25 |
Correct |
24 ms |
3328 KB |
Output is correct |
26 |
Correct |
27 ms |
3328 KB |
Output is correct |
27 |
Correct |
49 ms |
3376 KB |
Output is correct |
28 |
Correct |
54 ms |
3200 KB |
Output is correct |
29 |
Correct |
63 ms |
3072 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 |
128 KB |
Output is correct |