#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();
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
32 ms |
1140 KB |
Output is correct |
5 |
Correct |
941 ms |
20336 KB |
Output is correct |
6 |
Correct |
900 ms |
20336 KB |
Output is correct |
7 |
Correct |
898 ms |
20336 KB |
Output is correct |
8 |
Correct |
946 ms |
20368 KB |
Output is correct |
9 |
Correct |
940 ms |
20340 KB |
Output is correct |
10 |
Correct |
892 ms |
20332 KB |
Output is correct |
11 |
Correct |
2 ms |
372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
32 ms |
1140 KB |
Output is correct |
5 |
Correct |
941 ms |
20336 KB |
Output is correct |
6 |
Correct |
900 ms |
20336 KB |
Output is correct |
7 |
Correct |
898 ms |
20336 KB |
Output is correct |
8 |
Correct |
946 ms |
20368 KB |
Output is correct |
9 |
Correct |
940 ms |
20340 KB |
Output is correct |
10 |
Correct |
892 ms |
20332 KB |
Output is correct |
11 |
Correct |
606 ms |
16616 KB |
Output is correct |
12 |
Correct |
160 ms |
7372 KB |
Output is correct |
13 |
Correct |
12 ms |
4304 KB |
Output is correct |
14 |
Correct |
2 ms |
368 KB |
Output is correct |
15 |
Correct |
26 ms |
4600 KB |
Output is correct |
16 |
Correct |
2 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
6 ms |
736 KB |
Output is correct |
19 |
Correct |
17 ms |
4420 KB |
Output is correct |
20 |
Correct |
30 ms |
4692 KB |
Output is correct |
21 |
Correct |
12 ms |
4228 KB |
Output is correct |
22 |
Correct |
11 ms |
4052 KB |
Output is correct |
23 |
Correct |
12 ms |
4280 KB |
Output is correct |
24 |
Correct |
43 ms |
4888 KB |
Output is correct |
25 |
Correct |
626 ms |
16532 KB |
Output is correct |
26 |
Correct |
168 ms |
7404 KB |
Output is correct |
27 |
Correct |
13 ms |
4312 KB |
Output is correct |
28 |
Correct |
11 ms |
4088 KB |
Output is correct |
29 |
Correct |
12 ms |
4332 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
372 KB |
Output is correct |
32 |
Correct |
3 ms |
632 KB |
Output is correct |
33 |
Correct |
2 ms |
372 KB |
Output is correct |