#include "hiccup.h"
#include <vector>
using namespace std;
int HicCup(std::string S) {
int N = S.size();
int l = -1, r = N;
while (l + 1 < r){
int m = (l + r) / 2;
vector<pair<char, int> > u;
bool good = true;
for (int i = 0; i < N; i++){
if (S[i] == 'H'){
u.push_back({ S[i],1 });
}
else if (S[i] == 'C'){
if (!u.empty() && u.back().first == 0) u.pop_back();
u.push_back({ S[i],1 });
}
else{
if (!u.empty() && u.back().first == '!'){
u.back().second++;
}
else if (!u.empty() && u.back().first == 0){
u.back().second++;
}
else{
u.push_back({ '!',1 });
}
}
while (1){
int z = u.size();
if (m == 0){
if (z >= 2 && u[z - 2].first == 'H' && u[z - 1].first == 'C'){
u.pop_back();
u.pop_back();
if (!u.empty() && u.back().first == 0);
else u.push_back({ 0,0 });
}
else break;
}
else{
if (z >= 3 && u[z - 3].first == 'H' && u[z - 2].first == 'C' && u[z - 1].first == '!' && u[z - 1].second >= m){
u.pop_back();
u.pop_back();
u.pop_back();
if (!u.empty() && u.back().first == 0);
else u.push_back({ 0,0 });
}
else if (z >= 3 && u[z - 3].first == 'H' && u[z - 2].first == 'C' && u[z - 1].first == 0 && u[z - 1].second >= m){
u.pop_back();
u.pop_back();
u.pop_back();
if (!u.empty() && u.back().first == 0);
else u.push_back({ 0,0 });
}
else if (z >= 4 && u[z - 4].first == 'H' && u[z - 3].first == 'C' && u[z - 2].first == 0 && u[z - 1].first == '!' && u[z - 2].second + u[z - 1].second >= m){
int r = u[z - 2].second + u[z - 1].second - m;
u.pop_back();
u.pop_back();
u.pop_back();
u.pop_back();
if (!u.empty() && u.back().second == 0) u.back().second += r;
else u.push_back({ 0,r });
}
else if (z >= 4 && u[z - 4].first == 'H' && u[z - 3].first == 'C' && u[z - 2].first == '!' && u[z - 1].first == 0 && u[z - 2].second + u[z - 1].second >= m){
int r = u[z - 2].second + u[z - 1].second - m;
u.pop_back();
u.pop_back();
u.pop_back();
u.pop_back();
if (!u.empty() && u.back().second == 0) u.back().second += r;
else u.push_back({ 0,r });
}
else break;
}
while (u.size() >= 2){
z = u.size();
if (u[z - 2].first == 0 && (u[z - 1].first == '!' || u[z - 1].first == 0)){
u[z - 2].second += u[z - 1].second;
u.pop_back();
}
else break;
}
}
}
while (!u.empty() && u.back().first == 0) u.pop_back();
if (!u.empty()) good = false;
if (good) l = m;
else r = m;
}
return l;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
16 ms |
1260 KB |
Output is correct |
5 |
Correct |
523 ms |
19504 KB |
Output is correct |
6 |
Correct |
492 ms |
19444 KB |
Output is correct |
7 |
Correct |
488 ms |
19396 KB |
Output is correct |
8 |
Correct |
513 ms |
19480 KB |
Output is correct |
9 |
Correct |
509 ms |
19552 KB |
Output is correct |
10 |
Correct |
484 ms |
19332 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
16 ms |
1260 KB |
Output is correct |
5 |
Correct |
523 ms |
19504 KB |
Output is correct |
6 |
Correct |
492 ms |
19444 KB |
Output is correct |
7 |
Correct |
488 ms |
19396 KB |
Output is correct |
8 |
Correct |
513 ms |
19480 KB |
Output is correct |
9 |
Correct |
509 ms |
19552 KB |
Output is correct |
10 |
Correct |
484 ms |
19332 KB |
Output is correct |
11 |
Correct |
490 ms |
18744 KB |
Output is correct |
12 |
Correct |
260 ms |
9736 KB |
Output is correct |
13 |
Correct |
110 ms |
3324 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
130 ms |
3892 KB |
Output is correct |
16 |
Correct |
2 ms |
452 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
11 ms |
632 KB |
Output is correct |
19 |
Correct |
132 ms |
3496 KB |
Output is correct |
20 |
Correct |
140 ms |
4084 KB |
Output is correct |
21 |
Correct |
113 ms |
3328 KB |
Output is correct |
22 |
Correct |
132 ms |
3064 KB |
Output is correct |
23 |
Correct |
148 ms |
3192 KB |
Output is correct |
24 |
Correct |
149 ms |
4428 KB |
Output is correct |
25 |
Correct |
520 ms |
18804 KB |
Output is correct |
26 |
Correct |
268 ms |
9840 KB |
Output is correct |
27 |
Correct |
120 ms |
3448 KB |
Output is correct |
28 |
Correct |
135 ms |
3232 KB |
Output is correct |
29 |
Correct |
134 ms |
3420 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
10 ms |
636 KB |
Output is correct |
33 |
Correct |
2 ms |
256 KB |
Output is correct |