Submission #937148

#TimeUsernameProblemLanguageResultExecution timeMemory
937148089487Superpozicija (COCI22_superpozicija)C++14
110 / 110
24 ms7776 KiB
#pragma GCC optimize("O3,unroll-loops") #pragma GCC target("sse4,abm,avx,popcnt") #include<bits/stdc++.h> #define int long long #define quick ios::sync_with_stdio(0);cin.tie(0); #define rep(x,a,b) for(int x=a;x<=b;x++) #define repd(x,a,b) for(int x=a;x>=b;x--) #define F first #define S second #define eb emplace_back #define mp make_pair #define mt make_tuple #define all(x) x.begin(),x.end() #define sz(x) (int)(x.size()) #define lowbit(x) (x&-x) using namespace std; typedef pair<int,int> pii; void debug(){ cout<<"\n"; } template<class T,class ...U> void debug(T a,U ...b){ cout<<a<<" ",debug(b...); } const int N=2e5+7; const int INF=1e18L; pii p[N]; int to[N]; int dp[N]; int ch[N]; void solve(){ int n; cin>>n; fill(to,to+(n<<1),-1); fill(dp,dp+(n<<1),0LL); string s; cin>>s; set<pii> st; int cnt=0; int szv=0; rep(i,0,n-1){ cin>>p[i].F>>p[i].S; --p[i].F,--p[i].S; if(s[p[i].F]!=s[p[i].S]) { szv++; if(s[p[i].F]==')') dp[p[i].F]--,ch[i]=0; else { dp[p[i].S]--; ch[i]=1; } to[p[i].F]=i; } else{ if(s[p[i].F]=='(') dp[p[i].F]++,cnt++,ch[i]=0; else { dp[p[i].S]--; ch[i]=1; cnt--; } } } int total=-cnt+szv; if(total%2){ cout<<"-1\n"; return ; } int a=total/2; int b=a+cnt; if(a<0||b<0) { cout<<"-1\n"; return ; } int sum=0; priority_queue<pii,vector<pii>,greater<pii> > pq; rep(i,0,(n<<1)-1){ if(i) dp[i]+=dp[i-1]; if(to[i]!=-1) pq.push(mp(p[to[i]].S,to[i])); //cout<<dp[i]<<" \n"[i==n*2-1]; while(dp[i]<0&&a){ if(!sz(pq)) { cout<<"-1\n"; return ; } a--; pii k=pq.top(); //debug("use",k.F,k.S,"swich",p[k.S].F,p[k.S].S); dp[i]+=1; dp[max(k.F,i)]+=1; ch[k.S]^=1; pq.pop(); } if(dp[i]<0) { cout<<"-1\n"; return ; } //cout<<dp[i]<<" \n"[i==2*n-1]; } rep(i,0,n-1){ cout<<ch[i]<<" \n"[i==n-1]; } } signed main(){ quick int t; cin>>t; while(t--){ solve(); } }

Compilation message (stderr)

Main.cpp: In function 'void solve()':
Main.cpp:73:9: warning: unused variable 'sum' [-Wunused-variable]
   73 |     int sum=0;
      |         ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...