#include "hiccup.h"
#include <iostream>
#include <vector>
int HicCup(std::string S) {
int N = S.size();
int count = 0;
int subcount = 0;
int min = 0;
int rmin = -1;
int set = 0;
std::vector<char> arr;
std::vector<int> arr2;
for (int i = 0; i < N; i++) {
if (!arr.empty()) {
if (arr.back() == 'H' && S[i] == 'C') {
arr.pop_back();
i++;
set++;
while (S[i] == '!' && i < N) {
i++;
count++;
subcount++;
}
if (i < N && S[i] == 'C') {
if (rmin == -1) {
rmin = subcount;
}
else if (rmin > subcount) {
rmin = subcount;
}
i--;
subcount = 0;
continue;
}
arr2.push_back(subcount);
subcount = 0;
i--;
continue;
}
else if (arr.back() == 'H' && S[i] == '!') {
return -1;
}
else if (arr.back() == 'H' && S[i] == 'H') {
arr.push_back('H');
}
}
else {
if (S[i] == 'C') {
return -1;
}
else if (S[i] == 'H') {
arr.push_back('H');
}
}
if (S[i] == '!' && set == 0) {
return -1;
}
}
if (!arr.empty()) {
return -1;
}
int Sum = 0;
int sum = 0;
for (int i = 0; i < (int)arr2.size(); i++) {
sum += arr2[i];
}
Sum = sum;
if (rmin != -1 && rmin < arr2.back()) {
arr2[(int)arr2.size() - 2] += arr2[(int)arr2.size() - 1] - rmin;
arr2[(int)arr2.size() - 1] = rmin;
}
while (1) {
sum = Sum;
min = arr2.back();
int debt = 0;
int can = 1;
for (int i = 0; i < (int)arr2.size(); i++) {
if (arr2[i] > min) {
if (debt > 0) {
int backup = arr2[i];
while (debt > 0 && backup > min) {
debt--;
backup--;
}
sum -= backup;
}
else {
sum -= arr2[i];
}
}
else if (arr2[i] == min) {
sum -= arr2[i];
}
else {
sum -= arr2[i];
if (min - arr2[i] > sum - ((int)arr2.size() - i - 1) * min) {
can = 0;
break;
}
else {
sum -= min - arr2[i];
debt += min - arr2[i];
}
}
}
if (can == 1) {
return min;
}
else {
arr2[(int)arr2.size() - 1]--;
arr2[(int)arr2.size() - 2]++;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
23 ms |
4720 KB |
Output is correct |
6 |
Correct |
9 ms |
3448 KB |
Output is correct |
7 |
Correct |
9 ms |
3576 KB |
Output is correct |
8 |
Correct |
23 ms |
4720 KB |
Output is correct |
9 |
Correct |
23 ms |
4720 KB |
Output is correct |
10 |
Correct |
9 ms |
3576 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
23 ms |
4720 KB |
Output is correct |
6 |
Correct |
9 ms |
3448 KB |
Output is correct |
7 |
Correct |
9 ms |
3576 KB |
Output is correct |
8 |
Correct |
23 ms |
4720 KB |
Output is correct |
9 |
Correct |
23 ms |
4720 KB |
Output is correct |
10 |
Correct |
9 ms |
3576 KB |
Output is correct |
11 |
Correct |
12 ms |
3960 KB |
Output is correct |
12 |
Correct |
10 ms |
3704 KB |
Output is correct |
13 |
Correct |
9 ms |
3448 KB |
Output is correct |
14 |
Correct |
2 ms |
252 KB |
Output is correct |
15 |
Correct |
9 ms |
3448 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
3 ms |
632 KB |
Output is correct |
19 |
Correct |
10 ms |
3576 KB |
Output is correct |
20 |
Correct |
10 ms |
3576 KB |
Output is correct |
21 |
Correct |
10 ms |
3448 KB |
Output is correct |
22 |
Correct |
9 ms |
3320 KB |
Output is correct |
23 |
Correct |
10 ms |
3448 KB |
Output is correct |
24 |
Correct |
10 ms |
3704 KB |
Output is correct |
25 |
Correct |
23 ms |
5868 KB |
Output is correct |
26 |
Correct |
13 ms |
4160 KB |
Output is correct |
27 |
Correct |
10 ms |
3880 KB |
Output is correct |
28 |
Correct |
10 ms |
3448 KB |
Output is correct |
29 |
Correct |
331 ms |
3956 KB |
Output is correct |
30 |
Correct |
3 ms |
256 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
5 ms |
668 KB |
Output is correct |
33 |
Correct |
2 ms |
256 KB |
Output is correct |