QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#616583 | #4212. Brackets | ucup-team3659 | RE | 0ms | 0kb | C++17 | 1.3kb | 2024-10-06 08:36:37 | 2024-10-06 08:36:38 |
answer
#include<bits/stdc++.h>
using namespace std;
const int N=2e6+10;
int n,p[N],tr[N],lst[N],to[N],sb[N];
inline void No(){cout<<"(",exit(0);}
inline void add(int x,int y){for(;x<N;x+=x&-x)tr[x]+=y;}
inline int query(int x){int sum=0;for(;x;x-=x&-x)sum+=tr[x];return sum;}
int main()
{
freopen("fate.in","r",stdin);
freopen("fate.out","w",stdout);
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
if(n&1)No();
for(int i=1;i<=n*2;i++)
{
cin>>p[i];
if(lst[p[i]])to[lst[p[i]]]=i;
lst[p[i]]=i;
}
int sbsb=0;
if(n<=2000)
for(int i=1;i<=n*2;i++)
{
if(!to[i])continue;
add(i,1),add(to[i],1);
bool flag=1;
for(int i=1;i<=n*2;i++)if(query(n*2)-query(i-1)>(n*2-i+1)/2)flag=0;
if(!flag)add(i,-1),add(to[i],-1);
else sb[i]=sb[to[i]]=1,sbsb++;//,cout<<"#"<<i<<" "<<to[i]<<endl;
if(sbsb==n/2)break;
}
else
for(int i=1;i<=n*2;i++)
{
if(!to[i])continue;
add(i,1),add(to[i],1);
if((query(n*2)-query(i-1)>(n*2-i+1)/2)||(query(n*2)-query(to[i]-1)>(n*2-to[i]+1)/2))add(i,-1),add(to[i],-1);
else sb[i]=sb[to[i]]=1,sbsb++;//,cout<<"#"<<i<<" "<<to[i]<<endl;
if(sbsb==n/2)break;
}
int nw=0;
for(int i=1;i<=n*2;i++)
{
if(sb[i])nw++;
else
{
if(!nw)No();
nw--;
}
}
if(nw)No();
for(int i=1;i<=n*2;i++)cout<<((sb[i])?'(':')');
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
2 1 2 1 2