#include "hiccup.h"
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair <int, int> pii;
int HicCup(string s) {
int n = s.size();
int h = 0, hc = 0;
bool flag = false, flag2 = false;
for(int i = 0; i < n; i++) {
if(s[i] == 'H') {
flag2 = true;
h++;
flag = false;
}
if(s[i] == 'C') {
h--;
hc++;
flag = false;
flag2 = false;
}
if(s[i] == '!' && !flag) {
if(hc == 0) return -1;
if(flag2) return -1;
hc--;
flag = true;
}
if(h < 0) return -1;
}
if(h != 0) return -1;
int lb = 0, ub = 1000000;
while(lb < ub) {
int m = (lb + ub + 1) / 2;
vector <pii> v;
stack <int> H;
for(int i = 0; i < n; i++) {
if(s[i] == '!' && !v.empty()) {
v[v.size()-1].se++;
while(v[v.size()-1].se < m && i < n - 1 && s[i+1] == '!') {
i++, v[v.size()-1].se++;
}
if(v[v.size()-1].se == m) v.pop_back();
continue;
}
if(s[i] == 'C') {
bool flag = false;
while(!v.empty() && H.top() < v[v.size()-1].fi) {
if(v[v.size()-1].se < m) flag = true;
v.pop_back();
}
v.emplace_back(H.top(), 0);
if(flag) break;
H.pop();
}
if(s[i] == 'H') H.push(i);
while(!v.empty() && v[v.size()-1].se >= m) v.pop_back();
}
if(v.empty()) lb = m;
else ub = m-1;
}
return lb;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
7 ms |
512 KB |
Output is correct |
5 |
Correct |
18 ms |
3328 KB |
Output is correct |
6 |
Correct |
12 ms |
3328 KB |
Output is correct |
7 |
Correct |
13 ms |
3328 KB |
Output is correct |
8 |
Correct |
20 ms |
3328 KB |
Output is correct |
9 |
Correct |
20 ms |
3200 KB |
Output is correct |
10 |
Correct |
13 ms |
3328 KB |
Output is correct |
11 |
Correct |
5 ms |
384 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 |
7 ms |
512 KB |
Output is correct |
5 |
Correct |
18 ms |
3328 KB |
Output is correct |
6 |
Correct |
12 ms |
3328 KB |
Output is correct |
7 |
Correct |
13 ms |
3328 KB |
Output is correct |
8 |
Correct |
20 ms |
3328 KB |
Output is correct |
9 |
Correct |
20 ms |
3200 KB |
Output is correct |
10 |
Correct |
13 ms |
3328 KB |
Output is correct |
11 |
Correct |
14 ms |
3328 KB |
Output is correct |
12 |
Correct |
14 ms |
3328 KB |
Output is correct |
13 |
Correct |
12 ms |
3328 KB |
Output is correct |
14 |
Correct |
5 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 |
640 KB |
Output is correct |
19 |
Correct |
26 ms |
3328 KB |
Output is correct |
20 |
Correct |
21 ms |
3328 KB |
Output is correct |
21 |
Correct |
31 ms |
3328 KB |
Output is correct |
22 |
Correct |
21 ms |
3200 KB |
Output is correct |
23 |
Correct |
33 ms |
3328 KB |
Output is correct |
24 |
Correct |
19 ms |
3328 KB |
Output is correct |
25 |
Correct |
19 ms |
3328 KB |
Output is correct |
26 |
Correct |
19 ms |
3328 KB |
Output is correct |
27 |
Correct |
36 ms |
3328 KB |
Output is correct |
28 |
Correct |
35 ms |
3200 KB |
Output is correct |
29 |
Correct |
36 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 |
8 ms |
640 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |