#include "hiccup.h"
using namespace std;
int valid(const string& S) {
int h = 0, th = 0;
for (char ch : S) {
if (ch == 'H') h++, th++;
if (ch == 'C') {
if (h == 0) return 0;
h--;
}
if (ch == '!') continue;
}
return (h == 0 && th > 0);
}
int check(int x, const string& S) {
int h = 0, sign = 0;
for (char ch : S) {
if (ch == 'H') h++;
else if (ch == 'C') h--, sign += x;
else {
sign--;
if (sign < 0) sign = 0;
}
}
return sign == 0;
}
int HicCup(std::string S) {
int cntH = 0, cntSign = 0;
if (!valid(S)) return -1;
for (char ch : S) cntH += ch == 'H', cntSign += ch == '!';
int lo = 0, hi = cntSign/cntH;
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
if (check(mid, S)) lo = mid;
else hi = mid - 1;
}
return lo;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 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 |
21 ms |
3328 KB |
Output is correct |
6 |
Correct |
13 ms |
3328 KB |
Output is correct |
7 |
Correct |
13 ms |
3328 KB |
Output is correct |
8 |
Correct |
21 ms |
3328 KB |
Output is correct |
9 |
Correct |
22 ms |
3072 KB |
Output is correct |
10 |
Correct |
15 ms |
3328 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 |
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 |
21 ms |
3328 KB |
Output is correct |
6 |
Correct |
13 ms |
3328 KB |
Output is correct |
7 |
Correct |
13 ms |
3328 KB |
Output is correct |
8 |
Correct |
21 ms |
3328 KB |
Output is correct |
9 |
Correct |
22 ms |
3072 KB |
Output is correct |
10 |
Correct |
15 ms |
3328 KB |
Output is correct |
11 |
Correct |
15 ms |
3328 KB |
Output is correct |
12 |
Correct |
15 ms |
3328 KB |
Output is correct |
13 |
Correct |
14 ms |
3328 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Incorrect |
25 ms |
3328 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |