#include "hiccup.h"
#include<bits/stdc++.h>
//using namespace std;
int HicCup(std::string S) {
int N = S.size();
if ( S[0] != 'H' ) return -1;
int up = N+3, down = 0;
while( down + 1 < up ) {
int cur = (up + down)/2;
std::stack<int> boo;
//std::cout<<up<<" "<<down<<std::endl;
for ( int i = 0 ; i < N ; ++i ) {
//push next
if ( S[i] == 'H' ) {
boo.push(-2);
} else if ( S[i] == 'C' ) {
boo.push(-1);
} else {
if ( boo.empty() || boo.top() < 0 ) { //h or c is current
boo.push(1);
} else {
int temp = boo.top();
boo.pop();
boo.push(temp+1);
}
}
//pop if possible
while( boo.size() > 2 ){
int c = boo.top();
boo.pop();
int b = boo.top();
boo.pop();
int a = boo.top();
boo.pop();
if ( a == -2 && b == -1 && c >= cur ) { //하나 꺼낼 수 있다!
c -= cur;
if ( c ) {
if ( boo.empty() || boo.top() < 0 ) {
boo.push(c);
} else {
int temp = boo.top();
boo.pop();
boo.push(temp+c);
}
}
} else { //못 꺼내네 다시 넣어
boo.push(a);
boo.push(b);
boo.push(c);
break;
}
}
}
if ( boo.empty() || boo.size() < 2 ) {
down = cur;
} else {
up = cur;
}
}
//되면 down = (up + down)/2
//안되면 up = (up + down)/2
return down;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Incorrect |
6 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Incorrect |
6 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |