#include "hiccup.h"
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
using namespace std;
int HicCup(std::string S) {
int N = S.size();
int mask = 0;
int bal = 0;
for(int i=0; i<N; i++){
if(S[i] == '!' && mask != 3) return -1;
if(S[i] == 'H') bal++, mask |= 1;
if(S[i] == 'C') bal--, mask |= 2;
if(bal < 0) return -1;
}
if(bal > 0) return -1;
int cur[N], curcur[N], maskk[N];
int l=1, r=N, res=0;
while(l<=r){
int mid=(l+r)/2;
int ok = 1;
int level = 0;
cur[level] = 0;
curcur[level] = 0;
maskk[level] = 0;
for(int i=0; i<N; i++){
if(S[i] == 'H'){
maskk[level] |= 1;
level++;
cur[level] = 0;
curcur[level] = 0;
maskk[level] = 0;
} else if(S[i] == 'C'){
if(cur[level] != 0){
ok = 0;
break;
}
level--;
cur[level]++;
maskk[level] |= 2;
} else{
if(level == -1) return -1;
if(maskk[level] != 3) return -1;
if(cur[level]){
curcur[level]++;
if(curcur[level] == mid){
cur[level]--;
curcur[level] = 0;
}
}
}
}
if(cur[0] == 0 && ok) res = mid, l = mid+1;
else r = mid-1;
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
22 ms |
3456 KB |
Output is correct |
6 |
Correct |
15 ms |
3328 KB |
Output is correct |
7 |
Correct |
31 ms |
3328 KB |
Output is correct |
8 |
Correct |
19 ms |
3328 KB |
Output is correct |
9 |
Correct |
19 ms |
3456 KB |
Output is correct |
10 |
Correct |
13 ms |
3352 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
22 ms |
3456 KB |
Output is correct |
6 |
Correct |
15 ms |
3328 KB |
Output is correct |
7 |
Correct |
31 ms |
3328 KB |
Output is correct |
8 |
Correct |
19 ms |
3328 KB |
Output is correct |
9 |
Correct |
19 ms |
3456 KB |
Output is correct |
10 |
Correct |
13 ms |
3352 KB |
Output is correct |
11 |
Correct |
14 ms |
3328 KB |
Output is correct |
12 |
Correct |
14 ms |
3328 KB |
Output is correct |
13 |
Correct |
13 ms |
3328 KB |
Output is correct |
14 |
Correct |
6 ms |
384 KB |
Output is correct |
15 |
Correct |
13 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 |
384 KB |
Output is correct |
19 |
Correct |
33 ms |
3328 KB |
Output is correct |
20 |
Correct |
20 ms |
3288 KB |
Output is correct |
21 |
Correct |
56 ms |
3328 KB |
Output is correct |
22 |
Correct |
55 ms |
3200 KB |
Output is correct |
23 |
Correct |
79 ms |
3328 KB |
Output is correct |
24 |
Correct |
17 ms |
3200 KB |
Output is correct |
25 |
Correct |
19 ms |
3328 KB |
Output is correct |
26 |
Correct |
19 ms |
3328 KB |
Output is correct |
27 |
Correct |
61 ms |
3328 KB |
Output is correct |
28 |
Correct |
81 ms |
3320 KB |
Output is correct |
29 |
Correct |
80 ms |
3328 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
128 KB |
Output is correct |
32 |
Correct |
10 ms |
640 KB |
Output is correct |
33 |
Correct |
5 ms |
512 KB |
Output is correct |