#include <bits/stdc++.h>
#include "hiccup.h"
//~ #include "grader.cpp"
using namespace std;
const int N = 1000001;
int n;
string s;
vector <int> h;
bool valid(){
int o = 0;
for(auto &i : s){
o += i == 'H';
o -= i == 'C';
if(o < 0) return 0;
}
if(o) return 0;
for(auto &i : s){
if(i == 'H') o = 0;
else if(i == 'C') o = 1;
else if(o == 0) return 0;
}
return 1;
}
bool check(int x){
set <int> positive;
vector <int> g(n);
for(int i = n - 1 ; i >= 0 ; i--){
if(s[i] == '!'){
g[h[i]]++;
if(g[h[i]] == 1) positive.insert(h[i]);
}
else if(s[i] == 'C'){
if(g[h[i]] < x) return 0;
g[h[i]] -= x;
if(g[h[i]] == 0) positive.erase(h[i]);
while(positive.size() && *positive.rbegin() > h[i]){
auto it = --positive.end();
g[*it] = 0;
positive.erase(it);
}
}
}
return 1;
}
int HicCup(std::string S) {
s = S;
n = s.size();
if(!valid() || s == string(n, '!')) return -1;
h.resize(n);
h[0] = 1;
for(int i = 1 ; i < n ; i++){
h[i] = h[i - 1];
if(s[i] == 'H') h[i]++;
else if(s[i] == 'C') h[i]--;
//~ else v[h[i]].push_back(i);
}
int l = 0, r = n;
while(l <= r){
int mid = (l + r) / 2;
if(check(mid)) l = mid + 1;
else r = mid - 1;
}
return l - 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
7 ms |
896 KB |
Output is correct |
5 |
Correct |
58 ms |
12204 KB |
Output is correct |
6 |
Correct |
13 ms |
4260 KB |
Output is correct |
7 |
Correct |
23 ms |
4344 KB |
Output is correct |
8 |
Correct |
56 ms |
12204 KB |
Output is correct |
9 |
Correct |
53 ms |
12196 KB |
Output is correct |
10 |
Correct |
14 ms |
4352 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
7 ms |
896 KB |
Output is correct |
5 |
Correct |
58 ms |
12204 KB |
Output is correct |
6 |
Correct |
13 ms |
4260 KB |
Output is correct |
7 |
Correct |
23 ms |
4344 KB |
Output is correct |
8 |
Correct |
56 ms |
12204 KB |
Output is correct |
9 |
Correct |
53 ms |
12196 KB |
Output is correct |
10 |
Correct |
14 ms |
4352 KB |
Output is correct |
11 |
Correct |
14 ms |
4352 KB |
Output is correct |
12 |
Correct |
15 ms |
4480 KB |
Output is correct |
13 |
Correct |
14 ms |
4352 KB |
Output is correct |
14 |
Correct |
6 ms |
384 KB |
Output is correct |
15 |
Correct |
16 ms |
4352 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
6 ms |
384 KB |
Output is correct |
18 |
Correct |
7 ms |
640 KB |
Output is correct |
19 |
Correct |
80 ms |
12160 KB |
Output is correct |
20 |
Correct |
56 ms |
12160 KB |
Output is correct |
21 |
Correct |
74 ms |
12204 KB |
Output is correct |
22 |
Correct |
109 ms |
11628 KB |
Output is correct |
23 |
Correct |
180 ms |
12128 KB |
Output is correct |
24 |
Correct |
53 ms |
12204 KB |
Output is correct |
25 |
Correct |
63 ms |
12128 KB |
Output is correct |
26 |
Correct |
53 ms |
12204 KB |
Output is correct |
27 |
Correct |
72 ms |
12128 KB |
Output is correct |
28 |
Correct |
113 ms |
11872 KB |
Output is correct |
29 |
Correct |
131 ms |
12204 KB |
Output is correct |
30 |
Correct |
6 ms |
472 KB |
Output is correct |
31 |
Correct |
6 ms |
384 KB |
Output is correct |
32 |
Correct |
10 ms |
1408 KB |
Output is correct |
33 |
Correct |
6 ms |
512 KB |
Output is correct |