Submission #150027

#TimeUsernameProblemLanguageResultExecution timeMemory
150027Little Piplup (#200)HicCup (FXCUP4_hiccup)C++17
24 / 100
373 ms11424 KiB
#include "hiccup.h"
#include<bits/stdc++.h>
//using namespace std;

int HicCup(std::string S) {
	int N = S.size();
	if ( S[0] != 'H' ) return -1;

	std::vector<int> parse;
	int temp = 0;
	for ( int i = 0 ; i < N ; ++i ) {
		if ( S[i] == 'H' ) {
			if ( temp ) {
				parse.push_back(temp);
				temp = 0;
			}
			parse.push_back(-2);
		} else if ( S[i] == 'C' ) {
			if ( temp ) {
				parse.push_back(temp);
				temp = 0;
			}
			parse.push_back(-1);
		} else {
			++temp;
		}
	}
	if ( temp ) {
		parse.push_back(temp);
	}

	int size = parse.size();

	int up = N+3, down = 0;

	while( down + 1 < up ) {
		int cur = (up + down)/2;
		//std::cout<<up<<" "<<down<<std::endl;

		std::stack<int> boo;

		for ( int i = 0 ; i < size ; ++i ) {
			boo.push(parse[i]);
			//pop if possible
			while( boo.size() > 2 ){
				int c = boo.top();
				boo.pop();
				int b = boo.top();
				boo.pop();
				int a = boo.top();
				boo.pop();
				
				if ( a == -2 && b == -1 && c >= cur ) {

					if ( boo.empty() || boo.top() == -2 || c == cur) {
						//do nothing
					} else {		//push back
						c -= cur;
						if ( boo.empty() || boo.top() < 0 ) {
							boo.push(c);
						} else {
							int temp = boo.top();
							boo.pop();
							boo.push(temp+c);
						}
					}

				} else {
					boo.push(a);
					boo.push(b);
					boo.push(c);
					break;
				}
			}
		}

		if ( boo.empty() || (boo.size() == 1 && boo.top() >= 0) ) {
			down = cur;
		} else {
			up = cur;
		}


	}

	if ( down ) {
		return down;
	} else {
		bool done = true;
		int soo = 0;
		for ( int i = 0 ; i < N ; ++i ) {
			if ( S[i] == 'H' ) ++soo;
			if ( S[i] == 'C' ) --soo;
			if ( soo < 0 ) done = false;
		}
		if ( soo ) done = false;

		if ( done ) return 0;
		else return -1;
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...