제출 #613252

#제출 시각아이디문제언어결과실행 시간메모리
613252czhang2718HicCup (FXCUP4_hiccup)C++17
24 / 100
418 ms34876 KiB
// #include "hiccup.h"
using namespace std;
#include "bits/stdc++.h"

const int N=1e6+1;
vector<int> adj[N];
int cnt[N];
int X;
bool bad;

void dfs(int x, int &c){
	c+=cnt[x];
	// if(X==2) cout << x << " c " << c << "\n";
	if(x) c-=X;
	if(x && c<0) bad=1;
	for(int i=adj[x].size()-1; i>=0; i--){
		int k=adj[x][i];
		dfs(k, c);
	}
}

int HicCup(std::string S) {
	int n = S.size();

	S='-'+S;
	stack<int> par;
	par.push(0);
	int prv=-1;
	for(int i=1; i<=n; i++){
		if(S[i]=='!'){
			if(prv==-1) return -1;
			cnt[prv]++;
			continue;
		}
		if(S[i]=='C'){
			if(par.size()<2) return -1;
			int p=par.top(); par.pop();
			prv=p;
			adj[par.top()].push_back(p);
		}
		else{
			par.push(i);
		}
	}
	// for(int i=0; i<=n; i++){
	// 	if(cnt[i]) cout << "cnt[" << i << "] " << cnt[i] << "\n";
	// }
	if(par.size()!=1) return -1;

	auto check=[&](int x){
		X=x;
		bad=0;
		int c=0;
		dfs(0, c);
		return !bad;
	};

	int x=0;
	for(int i=19; i>=0; i--){
		if(x+(1<<i)<n && check(x+(1<<i))) x+=(1<<i);
	}
	// check(2);
	if(!check(x)) return -1;
	return x;
}

// int main(){
// 	string s;
// 	cin >> s;
// 	cout << HicCup(s);
// }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...