#include "hiccup.h"
#include<bits/stdc++.h>
using namespace std;
int HicCup(std::string S) {
int n=S.size();
bool chk=true,av=false;
int t=0;
for(auto i:S){
if(i=='H') t++,av=false;
if(i=='C'){
if(t) t--,av=true;
else chk=false;
}
if(i=='!'&&!av) chk=false;
}
if(t||!chk) return -1;
int l=1,r=1000000,Max=0;
while(l<=r){
int mid=(l+r)/2;
bool ichk=true;
// printf("%d %d %d\n",l,r,mid);
stack<pair<int,int>> st;
for(auto i:S){
// printf("%c\n",i);
//if(!st.empty()) printf("%d %d\n",st.top().first,st.top().second);
if(i=='H') st.push({1,0});
if(i=='C'){
if(!st.empty()){
if(st.top().first==1) st.pop(),st.push({2,0});
else if(st.top().second<mid) {ichk=false;break;}
}
}
if(i=='!'&&!st.empty()){
if(!st.empty()&&st.top().first==2){
int c=st.top().second; st.pop();
st.push({2,c+1});
}
while(!st.empty()&&st.top().first==2&&st.top().second==mid) st.pop();
}
}
while(!st.empty()&&st.top().second==mid) st.pop();
if(st.empty()&&ichk) Max=max(Max,mid),l=mid+1;
else r=mid-1;
//printf("\n");
}
return Max;
}
// HCHC!!
Compilation message
hiccup.cpp: In function 'int HicCup(std::__cxx11::string)':
hiccup.cpp:6:6: warning: unused variable 'n' [-Wunused-variable]
int n=S.size();
^
# |
결과 |
실행 시간 |
메모리 |
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 |
19 ms |
3328 KB |
Output is correct |
6 |
Correct |
25 ms |
3328 KB |
Output is correct |
7 |
Correct |
18 ms |
3328 KB |
Output is correct |
8 |
Correct |
19 ms |
3328 KB |
Output is correct |
9 |
Correct |
18 ms |
3328 KB |
Output is correct |
10 |
Correct |
28 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 |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
19 ms |
3328 KB |
Output is correct |
6 |
Correct |
25 ms |
3328 KB |
Output is correct |
7 |
Correct |
18 ms |
3328 KB |
Output is correct |
8 |
Correct |
19 ms |
3328 KB |
Output is correct |
9 |
Correct |
18 ms |
3328 KB |
Output is correct |
10 |
Correct |
28 ms |
3328 KB |
Output is correct |
11 |
Correct |
19 ms |
3328 KB |
Output is correct |
12 |
Correct |
15 ms |
3328 KB |
Output is correct |
13 |
Correct |
14 ms |
3328 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
15 ms |
3328 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
6 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
640 KB |
Output is correct |
19 |
Correct |
69 ms |
3328 KB |
Output is correct |
20 |
Correct |
28 ms |
3328 KB |
Output is correct |
21 |
Correct |
164 ms |
3328 KB |
Output is correct |
22 |
Correct |
152 ms |
3200 KB |
Output is correct |
23 |
Correct |
303 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 |
22 ms |
3328 KB |
Output is correct |
27 |
Correct |
183 ms |
3328 KB |
Output is correct |
28 |
Correct |
301 ms |
3200 KB |
Output is correct |
29 |
Correct |
281 ms |
3328 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
256 KB |
Output is correct |
32 |
Correct |
25 ms |
640 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |