QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#302242 | #4824. Bracket-and-bar Sequences | Loging# | 0 | 0ms | 3920kb | C++20 | 2.0kb | 2024-01-10 17:59:06 | 2024-01-10 17:59:07 |
answer
#include <bits/stdc++.h>
#define ll long long
#define int long long
using namespace std;
int T,n;
ll now,f[100],g[100];
char s[100];
inline ll calc(int l,int r){
// cout<<l<<" "<<r<<endl;
if(l>r)return 1;
if(l==r)return 1;
if(l==r-2)return 1;
int res=0,id=0;
for(int i=l;i<=r;i++){
if(s[i]=='(')res++;
if(s[i]==')')res--;
if(!res){
id=i;
break;
}
}
int m=(r-l-2)/3;
int p=(id-l-2)/3;
int q=(r-id)/3;
ll sum=0;
for(int i=0;i<p;i++){
if(i==0){
sum+=f[m];
}else{
sum+=2*f[i]*f[m-i];
}
}
if(id==0||s[l+1]=='|'){
return sum+(calc(l+2,id-1)-1)*f[q]+calc(id+1,r);
}else{
return sum+f[p]*f[q]+(calc(l+1,id-2)-1)*f[q]+calc(id+1,r);
}
}
inline void calc2(int l,int r,int n,ll now){
if(l>=r)return;
// cout<<l<<" "<<r<<" "<<n<<" "<<now<<endl;
int p=0;
for(int i=0;i<n;i++){
ll res=0;
if(i==0){
res=f[n-1];
}else{
res=2*f[i]*f[n-1-i];
}
if(now<=res){
p=i;
break;
}else{
now-=res;
}
}
int id=l+p*3+2;
int q=n-p-1;
// cout<<p<<" "<<q<<" "<<now<<endl;
if(p==0||now<=f[p]*f[q]){
s[l]='(';
s[l+1]='|';
s[id]=')';
ll k=((now-1)/f[q])+1;
ll kk=(now-1)%f[q]+1;
calc2(l+2,id-1,p,k);
calc2(id+1,r,q,kk);
}else{
s[l]='(';
s[id-1]='|';
s[id]=')';
now-=f[p]*f[q];
ll k=((now-1)/f[q])+1;
ll kk=(now-1)%f[q]+1;
calc2(l+1,id-2,p,k);
calc2(id+1,r,q,kk);
}
}
signed main(){
f[0]=1;
f[1]=g[1]=1;
for(int i=2;i<=25;i++){
f[i]=0;
for(int j=0;j<=i-1;j++){
if(j==0){
f[i]+=f[i-1];
}else{
f[i]+=2*f[j]*f[i-1-j];
}
}
// cout<<i<<" "<<f[i]<<endl;
g[i]=g[i-1]+f[i];
}
scanf("%s",s+1);
if(s[1]=='e'){
scanf("%lld",&T);
while(T--){
scanf("%lld",&n);
scanf("%s",s+1);
now=calc(1,3*n);
printf("%lld\n",now);
}
}else{
scanf("%lld",&T);
while(T--){
scanf("%lld",&n);
scanf("%lld",&now);
calc2(1,3*n,n,now);
for(int i=1;i<=3*n;i++)putchar(s[i]);puts("");
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3904kb
input:
encode 3 1 (|) 4 ((((|)|)|)|) 5 (|(|))((|(|))|)
output:
1 45 55
input:
decode 3 1 1 4 45 5 55
output:
(|) ((((|)|)|)|) (|(|))((|(|))|)
result:
ok 3 lines
Test #2:
score: 100
Accepted
time: 0ms
memory: 3920kb
input:
encode 1 1 (|)
output:
1
input:
decode 1 1 1
output:
(|)
result:
ok single line: '(|)'
Test #3:
score: 100
Accepted
time: 0ms
memory: 3856kb
input:
encode 3 2 ((|)|) 1 (|) 2 (|(|))
output:
3 1 2
input:
decode 3 2 3 1 1 2 2
output:
((|)|) (|) (|(|))
result:
ok 3 lines
Test #4:
score: 0
Stage 1: Program answer Time Limit Exceeded
input:
encode 1000 3 (|)(|)(|) 3 (|)(|(|)) 3 (|)((|)|) 3 (|(|))(|) 3 (|(|)(|)) 3 (|(|(|))) 3 (|((|)|)) 3 ((|)|)(|) 3 ((|)|(|)) 3 ((|)(|)|) 3 ((|(|))|) 3 (((|)|)|) 4 (|)(|)(|)(|) 4 (|)(|)(|(|)) 4 (|)(|)((|)|) 4 (|)(|(|))(|) 4 (|)(|(|)(|)) 4 (|)(|(|(|))) 4 (|)(|((|)|)) 4 (|)((|)|)(|) 4 (|)((|)|(|)) 4 (|)((|)...