#include "hiccup.h"
#include <stack>
#include <vector>
#include <iostream>
using namespace std;
bool try_to_pop(stack<pair<int,int>> &st, int X){
// we want H_a C_b where a<=0 and b>=X
if(st.size() < 2) return false;
auto [b1, b2] = st.top(); st.pop();
auto [a1, a2] = st.top(); st.pop();
if(a1 != 1 || b1 != 2 || a2 > 0 || abs(b2) < X){
st.push({a1,a2}); st.push({b1,b2});
return false;
}
// combine ??(-a2) with !!(b2-X)
int newex = -a2 + abs(b2)-X;
if(st.empty()) st.push({0, -newex});
else if(st.top().second > 0) st.top().second+= newex;
else st.top().second-= newex;
return true;
}
bool possible(vector<pair<int,int>> &V, int X){
stack<pair<int,int>> st;
for(auto [ct, ex]: V){
st.push({ct,ex});
while(try_to_pop(st, X));
}
if(st.empty()) return true;
if(st.size() > 1) return false;
return st.top().first == 0 && st.top().second <= 0;
}
int HicCup(std::string S) {
int N = S.size();
vector<pair<int,int>> V;
// first: type. blank 0, H 1, C 2
// second: !
for(char c: S){
if(c == '!'){
if(V.empty()) V.push_back({0, 1});
else V.back().second++;
}
else if(c == 'H') V.push_back({1, 0});
else V.push_back({2, 0});
}
//for(auto [a,b]: V) cout<<a<<' '<<b<<endl;
int lo = 0, hi = 1000000, ans = -1;
while(lo <= hi){
int mid = (lo+hi)/2;
//cout<<"TRYING "<<mid<<endl;
if(possible(V, mid)) ans = mid, lo = mid+1;
else hi = mid-1;
}
if(ans > -1) return ans;
return -1;
}
Compilation message
hiccup.cpp: In function 'int HicCup(std::__cxx11::string)':
hiccup.cpp:37:6: warning: unused variable 'N' [-Wunused-variable]
int N = S.size();
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
8 ms |
384 KB |
Output is correct |
4 |
Correct |
39 ms |
1148 KB |
Output is correct |
5 |
Correct |
909 ms |
19428 KB |
Output is correct |
6 |
Correct |
872 ms |
19428 KB |
Output is correct |
7 |
Correct |
870 ms |
19428 KB |
Output is correct |
8 |
Correct |
935 ms |
19428 KB |
Output is correct |
9 |
Correct |
930 ms |
19412 KB |
Output is correct |
10 |
Correct |
886 ms |
19428 KB |
Output is correct |
11 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
8 ms |
384 KB |
Output is correct |
4 |
Correct |
39 ms |
1148 KB |
Output is correct |
5 |
Correct |
909 ms |
19428 KB |
Output is correct |
6 |
Correct |
872 ms |
19428 KB |
Output is correct |
7 |
Correct |
870 ms |
19428 KB |
Output is correct |
8 |
Correct |
935 ms |
19428 KB |
Output is correct |
9 |
Correct |
930 ms |
19412 KB |
Output is correct |
10 |
Correct |
886 ms |
19428 KB |
Output is correct |
11 |
Correct |
624 ms |
15444 KB |
Output is correct |
12 |
Correct |
171 ms |
6504 KB |
Output is correct |
13 |
Correct |
18 ms |
3328 KB |
Output is correct |
14 |
Correct |
6 ms |
384 KB |
Output is correct |
15 |
Correct |
33 ms |
3712 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
12 ms |
768 KB |
Output is correct |
19 |
Correct |
21 ms |
3456 KB |
Output is correct |
20 |
Correct |
36 ms |
3712 KB |
Output is correct |
21 |
Correct |
17 ms |
3328 KB |
Output is correct |
22 |
Correct |
16 ms |
3200 KB |
Output is correct |
23 |
Correct |
16 ms |
3144 KB |
Output is correct |
24 |
Correct |
49 ms |
3964 KB |
Output is correct |
25 |
Correct |
658 ms |
15700 KB |
Output is correct |
26 |
Correct |
181 ms |
6376 KB |
Output is correct |
27 |
Correct |
17 ms |
3328 KB |
Output is correct |
28 |
Correct |
15 ms |
3072 KB |
Output is correct |
29 |
Correct |
16 ms |
3200 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
7 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
640 KB |
Output is correct |
33 |
Correct |
6 ms |
384 KB |
Output is correct |