Submission #150972

# Submission time Handle Problem Language Result Execution time Memory
150972 2019-09-01T13:22:41 Z JustInCase HicCup (FXCUP4_hiccup) C++17
100 / 100
121 ms 3944 KB
#include <bits/stdc++.h>

#ifdef LOCAL
	#include "grader.cpp"
#else
	#include "hiccup.h"
#endif

#define hic_cup HicCup



bool check_is_valid_input(const std::string &s) {
	char last = '-';
	int32_t cntH = 0, cntC = 0;
	for(int32_t i = 0; i < s.size(); i++) {
		if(s[i] == 'H') {
			cntH++;
			last = 'H';
		}
		else if(s[i] == 'C') {
			cntC++;
			last = 'C';
		}
		else {
			if(last == 'H' || cntH == 0) {
				return false;
			}
		}

		if(cntC > cntH) {
			return false;
		}
	}
	
	if(cntH != cntC) {
		return false;
	}
	else {
		return true;
	}
}

bool check_is_x_string(const std::string &s, int32_t x) {
	std::stack< int32_t > stH;
	std::stack< std::pair< int32_t, int32_t > > st;
	
	for(int32_t i = 0; i < s.size(); i++) {
		if(s[i] == 'H') {
			stH.push(i);
		}
		else if(s[i] == 'C') {
			if(!st.empty() && st.top().first > stH.top()) {
				return false;
			}

			st.push({stH.top(), x});
			stH.pop();
		}
		else {
			if(!st.empty()) {
				auto aux = st.top();
				st.pop();

				aux.second--;
				if(aux.second != 0) {
					st.push(aux);
				}
			}
		}
	}
	
	if(st.empty()) {
		return true;
	}
	else {
		return false;
	}
}

int32_t hic_cup(std::string s) {
	if(!check_is_valid_input(s)) {
		return -1;
	}

	int32_t low = 1, high = s.size();
	while(low <= high) {
		int32_t mid = (low + high) / 2;

		if(check_is_x_string(s, mid)) {
			low = mid + 1;
		}
		else {
			high = mid - 1;
		}
	}

	return low - 1;
}

Compilation message

hiccup.cpp: In function 'bool check_is_valid_input(const string&)':
hiccup.cpp:16:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int32_t i = 0; i < s.size(); i++) {
                     ~~^~~~~~~~~~
hiccup.cpp: In function 'bool check_is_x_string(const string&, int32_t)':
hiccup.cpp:48:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int32_t i = 0; i < s.size(); i++) {
                     ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 252 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 16 ms 3704 KB Output is correct
6 Correct 9 ms 3704 KB Output is correct
7 Correct 9 ms 3832 KB Output is correct
8 Correct 15 ms 3704 KB Output is correct
9 Correct 15 ms 3704 KB Output is correct
10 Correct 9 ms 3804 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 252 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 16 ms 3704 KB Output is correct
6 Correct 9 ms 3704 KB Output is correct
7 Correct 9 ms 3832 KB Output is correct
8 Correct 15 ms 3704 KB Output is correct
9 Correct 15 ms 3704 KB Output is correct
10 Correct 9 ms 3804 KB Output is correct
11 Correct 10 ms 3708 KB Output is correct
12 Correct 10 ms 3704 KB Output is correct
13 Correct 9 ms 3704 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 9 ms 3704 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 3 ms 632 KB Output is correct
19 Correct 35 ms 3704 KB Output is correct
20 Correct 18 ms 3704 KB Output is correct
21 Correct 74 ms 3828 KB Output is correct
22 Correct 76 ms 3576 KB Output is correct
23 Correct 120 ms 3820 KB Output is correct
24 Correct 14 ms 3860 KB Output is correct
25 Correct 16 ms 3836 KB Output is correct
26 Correct 16 ms 3784 KB Output is correct
27 Correct 83 ms 3776 KB Output is correct
28 Correct 121 ms 3704 KB Output is correct
29 Correct 117 ms 3944 KB Output is correct
30 Correct 3 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 9 ms 632 KB Output is correct
33 Correct 2 ms 376 KB Output is correct