// #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];
for(int i=adj[x].size()-1; i>=0; i--){
int k=adj[x][i];
dfs(k, c);
}
// cout << x << " have " << c << "\n";
c-=X;
if(x && c<0) bad=1;
}
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]++;
// cout << "cnt[" << prv << "]++\n";
continue;
}
if(S[i]=='C'){
if(par.size()<2) return -1;
int p=par.top(); par.pop();
adj[par.top()].push_back(p);
// cout << "par " << p << " = " << par.top() << "\n";
prv=p;
}
else{
par.push(i);
}
}
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);
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
23776 KB |
Output is correct |
2 |
Correct |
19 ms |
23776 KB |
Output is correct |
3 |
Correct |
18 ms |
23808 KB |
Output is correct |
4 |
Correct |
26 ms |
24172 KB |
Output is correct |
5 |
Correct |
420 ms |
34848 KB |
Output is correct |
6 |
Correct |
27 ms |
27624 KB |
Output is correct |
7 |
Correct |
22 ms |
27612 KB |
Output is correct |
8 |
Correct |
464 ms |
34856 KB |
Output is correct |
9 |
Correct |
344 ms |
34872 KB |
Output is correct |
10 |
Correct |
25 ms |
27604 KB |
Output is correct |
11 |
Incorrect |
17 ms |
23748 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
23776 KB |
Output is correct |
2 |
Correct |
19 ms |
23776 KB |
Output is correct |
3 |
Correct |
18 ms |
23808 KB |
Output is correct |
4 |
Correct |
26 ms |
24172 KB |
Output is correct |
5 |
Correct |
420 ms |
34848 KB |
Output is correct |
6 |
Correct |
27 ms |
27624 KB |
Output is correct |
7 |
Correct |
22 ms |
27612 KB |
Output is correct |
8 |
Correct |
464 ms |
34856 KB |
Output is correct |
9 |
Correct |
344 ms |
34872 KB |
Output is correct |
10 |
Correct |
25 ms |
27604 KB |
Output is correct |
11 |
Incorrect |
17 ms |
23748 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |