#include "hiccup.h"
#include <bits/stdc++.h>
using namespace std;
int N;
std::string S;
int myS[1000010];
int top;
int state[1000010];
bool able(int v)
{
top = 0;
for (int i = 0; i < N; i++)
{
top++;
if (S[i] == 'H')
state[top] = 1;
if (S[i] == 'C')
{
if (state[top - 1] == 1)
state[top] = 2;
else return false;
}
if (S[i] == '!')
{
if (state[top - 1] <= 1) top--;
else state[top] = state[top - 1] + 1;
}
if (state[top] == v + 2)
top -= v + 2;
}
return state[top] == 0;
}
int HicCup(std::string S_) {
S = S_;
N = S.size();
stack <int> ST;
for (int i = 0; i < N; i++)
{
if (S[i] == 'H') ST.push(i);
if (S[i] == 'C')
{
if (ST.empty()) return -1;
myS[ST.top()] = i;
ST.pop();
}
}
if (!ST.empty()) return -1;
for (int i = 0; i < N; i++)
if (S[i] == '!')
{
if (i == 0) return -1;
if (S[i - 1] == 'H') return -1;
}
int s, e, v;
s = 0; e = (1 << 20) - 1;
while (s < e)
{
v = (s + e) / 2;
if (able(v + 1)) s = v + 1;
else e = v;
}
return s;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 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 |
6 ms |
640 KB |
Output is correct |
5 |
Correct |
24 ms |
8192 KB |
Output is correct |
6 |
Correct |
14 ms |
4352 KB |
Output is correct |
7 |
Correct |
14 ms |
4352 KB |
Output is correct |
8 |
Correct |
25 ms |
8184 KB |
Output is correct |
9 |
Correct |
25 ms |
8192 KB |
Output is correct |
10 |
Correct |
13 ms |
4352 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 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 |
6 ms |
640 KB |
Output is correct |
5 |
Correct |
24 ms |
8192 KB |
Output is correct |
6 |
Correct |
14 ms |
4352 KB |
Output is correct |
7 |
Correct |
14 ms |
4352 KB |
Output is correct |
8 |
Correct |
25 ms |
8184 KB |
Output is correct |
9 |
Correct |
25 ms |
8192 KB |
Output is correct |
10 |
Correct |
13 ms |
4352 KB |
Output is correct |
11 |
Correct |
15 ms |
5120 KB |
Output is correct |
12 |
Correct |
16 ms |
5760 KB |
Output is correct |
13 |
Correct |
15 ms |
4480 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
16 ms |
8064 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 |
768 KB |
Output is correct |
19 |
Correct |
41 ms |
8192 KB |
Output is correct |
20 |
Correct |
30 ms |
8192 KB |
Output is correct |
21 |
Correct |
65 ms |
8392 KB |
Output is correct |
22 |
Correct |
56 ms |
6648 KB |
Output is correct |
23 |
Correct |
86 ms |
8192 KB |
Output is correct |
24 |
Correct |
27 ms |
8192 KB |
Output is correct |
25 |
Correct |
27 ms |
8184 KB |
Output is correct |
26 |
Correct |
30 ms |
8060 KB |
Output is correct |
27 |
Correct |
72 ms |
8832 KB |
Output is correct |
28 |
Correct |
82 ms |
8436 KB |
Output is correct |
29 |
Correct |
85 ms |
8320 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
12 ms |
1024 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |