#include "hiccup.h"
#include <bits/stdc++.h>
using namespace std;
int r[1000005], l[1000005];
string s;
bool solve(int st, int dr, int mij){
int nr = 0;
for(int i = dr; i >= st ; --i){
if(s[i] == 'C'){
bool ok = solve(l[i] + 1, i - 1, mij);
if(!ok) return 0;
i = l[i];
nr -= mij;
if(nr < 0) return 0;
}
else if(s[i] == '!') ++nr;
}
return 1;
}
int HicCup(std::string S) {
s = S;
int N = S.size();
int nrH = 0, nrC = 0;
for(auto i : S){
if(i == '!' && (nrH == 0 || nrC == 0)) return -1;
else if(i == 'H') ++nrH;
else if(i == 'C'){
++nrC;
if(nrC > nrH) return -1;
}
}
for(int i = 0; i < N - 1 ; ++i)
if(S[i] == 'H' && S[i + 1] == '!') return -1;
if(nrH != nrC) return -1;
for(int i = 0; i < N ; ++i){
int nr = 0;
while(i < N && S[i] == 'H') ++nr, ++i;
if(nr < 2) continue ;
nr = 0;
while(i < N && S[i] == 'C') ++nr, ++i;
if(nr >= 2) return 0;
}
stack <int> sto;
for(int i = 0; i < N ; ++i){
if(S[i] == 'H') sto.push(i);
else if(S[i] == 'C'){
r[sto.top()] = i;
l[i] = sto.top();
sto.pop();
}
}
int st = 0, dr = N;
while(st <= dr){
int mij = (st + dr) / 2;
bool ok = solve(0, N - 1, mij);
if(ok) st = mij + 1;
else dr = mij - 1;
}
return dr;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
26 ms |
4352 KB |
Output is correct |
6 |
Correct |
15 ms |
4352 KB |
Output is correct |
7 |
Correct |
15 ms |
4352 KB |
Output is correct |
8 |
Correct |
27 ms |
4352 KB |
Output is correct |
9 |
Correct |
28 ms |
4352 KB |
Output is correct |
10 |
Correct |
15 ms |
4224 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
26 ms |
4352 KB |
Output is correct |
6 |
Correct |
15 ms |
4352 KB |
Output is correct |
7 |
Correct |
15 ms |
4352 KB |
Output is correct |
8 |
Correct |
27 ms |
4352 KB |
Output is correct |
9 |
Correct |
28 ms |
4352 KB |
Output is correct |
10 |
Correct |
15 ms |
4224 KB |
Output is correct |
11 |
Correct |
16 ms |
4352 KB |
Output is correct |
12 |
Correct |
16 ms |
4352 KB |
Output is correct |
13 |
Correct |
14 ms |
4352 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
16 ms |
4352 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
640 KB |
Output is correct |
19 |
Correct |
29 ms |
12032 KB |
Output is correct |
20 |
Correct |
32 ms |
12280 KB |
Output is correct |
21 |
Correct |
31 ms |
7552 KB |
Output is correct |
22 |
Correct |
34 ms |
4224 KB |
Output is correct |
23 |
Correct |
46 ms |
4352 KB |
Output is correct |
24 |
Correct |
31 ms |
12152 KB |
Output is correct |
25 |
Correct |
25 ms |
4352 KB |
Output is correct |
26 |
Correct |
37 ms |
12152 KB |
Output is correct |
27 |
Correct |
28 ms |
8440 KB |
Output is correct |
28 |
Correct |
35 ms |
4992 KB |
Output is correct |
29 |
Correct |
40 ms |
4352 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
7 ms |
896 KB |
Output is correct |
33 |
Correct |
5 ms |
512 KB |
Output is correct |