#include "hiccup.h"
#include<bits/stdc++.h>
using namespace std;
int HicCup(std::string S) {
int N = S.size();
stack<int> st;
bool chk=true,r=false;
for(auto i:S){
if(i=='H') st.push(1);
else if(i=='C'){
if(!st.empty()&&st.top()) st.pop(),r=true;
else chk=false;
}
else if(!r) chk=false;
}
if(!st.empty()) chk=false;
if(!chk) return -1;
else{
int l=0,r=1000000,Max=0;
while(l<=r){
int mid=(l+r)/2;
stack<pair<int,int>> s;
for(auto i:S){
if(i=='H') s.push({1,0});
else if(i=='C') {
if(s.empty()) while(1);
s.pop(),s.push({2,0});
}
else{
if(!s.empty()){
int c=s.top().second; s.pop();
s.push({2,c+1});
}
if(!s.empty()&&s.top().second==mid) s.pop();
}
}
while(!s.empty()&&s.top().second==mid) s.pop();
if(s.empty()) l=mid+1,Max=max(Max,mid);
else r=mid-1;
}
return Max;
}
}
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 |
16 ms |
716 KB |
Output is correct |
5 |
Correct |
314 ms |
7504 KB |
Output is correct |
6 |
Correct |
21 ms |
3328 KB |
Output is correct |
7 |
Correct |
20 ms |
3328 KB |
Output is correct |
8 |
Correct |
307 ms |
7496 KB |
Output is correct |
9 |
Correct |
310 ms |
7500 KB |
Output is correct |
10 |
Correct |
23 ms |
3372 KB |
Output is correct |
11 |
Correct |
6 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 |
16 ms |
716 KB |
Output is correct |
5 |
Correct |
314 ms |
7504 KB |
Output is correct |
6 |
Correct |
21 ms |
3328 KB |
Output is correct |
7 |
Correct |
20 ms |
3328 KB |
Output is correct |
8 |
Correct |
307 ms |
7496 KB |
Output is correct |
9 |
Correct |
310 ms |
7500 KB |
Output is correct |
10 |
Correct |
23 ms |
3372 KB |
Output is correct |
11 |
Correct |
19 ms |
3328 KB |
Output is correct |
12 |
Correct |
16 ms |
3200 KB |
Output is correct |
13 |
Correct |
14 ms |
3328 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Execution timed out |
1100 ms |
3456 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |