Submission #13093435


Source Code Expand

#include <bits/stdc++.h>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,b,a) for(ll i=b;i>=a;i--)
#define fori(a) for(auto i : a )
#define all(a) begin(a), end(a)
#define set(a,b) memset(a,b,sizeof(a))
#define sz(a) a.size()
#define pi 3.14159
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front //deque
#define mp make_pair
#define pq priority_queue
#define mod 1000000007
#define f first
#define s second
#define pii pair< ll, ll >
#define vi vector<ll>
#define vpii vector<pii>
#define debug(v) for(auto i:v) cout<<i<<" ";
#define tc ll t; cin >> t; while(t--)

using namespace std;
string repeat(string s, ll n) {
    string s1 = "";
    for (ll i=0; i<n;i++)
        s1+=s;
    return s1;
}
string getString(char x) {
    string s(1, x);
    return s;
}

void optimizeIO(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
ll gcd(ll a, ll b){
    if (a == 0)  return b;
    return gcd(b % a, a);
}
void solve(){
  ll n;
  cin>>n;
  vector<string> s(n);
  string g;
  rep(i,0,n) {cin>>g; s[i]=g;}
  vector<pii> v1(n,{0,0});
  vector<ll> op,clos;
  vpii op_clos;
  rep(i,0,n){
    stack<ll> st;
    g=s[i];
    rep(j,0,(ll)g.length()){
      if(st.empty()){
        if(g[j]=='(') st.push(1);
        else st.push(-1);
      }
      else{
        if(g[j]=='('){
          if(st.top()<0) st.push(1);
          else{
            ll x=st.top();
            st.pop();
            st.push(x+1);
          }
        }
        else{
          ll x=st.top();
          x--;
          st.pop();
          if(x!=0) st.push(x);
        }
      }

    }
    ll mn=0,mx=0;
    while(!st.empty()){
      if(st.top()<0) mn=st.top();
      if(st.top()>0) mx=st.top();
      st.pop();
    }
    v1[i]={mn,mx};
    if(mn==0 && mx>0){
      op.push_back(i);
    }
    else if(mx==0 && mn<0){
      clos.pb(i);
    }
    else if(mx>0 && mn<0){
      op_clos.pb({mn,mx});
    }
  }

  ll op1=0,flag=0;
  rep(i,0,(ll)op.size()){
    op1+=v1[op[i]].s;
  }
  auto comp=[](pii &x,pii &y){
    return (x.f+x.s)>(y.f+y.s);
  };
  sort(all(op_clos),comp);
  rep(i,0,(ll)op_clos.size()){
    pii x=op_clos[i];
    if(op1<abs(x.f)) {flag=1; break;}
    else{
      op1+=x.f;
      op1+=x.s;
    }
  }
  // cout<<flag<<endl;
  rep(i,0,(ll)clos.size()){
    pii x=v1[clos[i]];
    if(op1<abs(x.f)){flag=1; break;}
    else op1+=x.f;
  }
  // cout<<op1<<endl;
  if(op1!=0) flag=1;
  if(flag) cout<<"No\n";
  else cout<<"Yes\n";
}
int main(){
    optimizeIO();

    solve();

}

Submission Info

Submission Time
Task F - Bracket Sequencing
User vineetjai
Language C++ (GCC 9.2.1)
Score 600
Code Size 2656 Byte
Status AC
Exec Time 139 ms
Memory 58084 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 4
AC × 32
Set Name Test Cases
Sample sample_01, sample_02, sample_03, sample_04
All random_01, random_02, random_03, random_04, random_05, random_06, random_07, random_08, random_09, random_10, random_11, random_12, random_13, random_21, random_22, random_23, random_31, random_32, random_33, random_41, random_42, random_43, random_51, random_52, random_53, random_61, random_62, random_63, sample_01, sample_02, sample_03, sample_04
Case Name Status Exec Time Memory
random_01 AC 58 ms 18732 KiB
random_02 AC 120 ms 48876 KiB
random_03 AC 77 ms 25136 KiB
random_04 AC 136 ms 58076 KiB
random_05 AC 139 ms 58084 KiB
random_06 AC 61 ms 23784 KiB
random_07 AC 28 ms 6612 KiB
random_08 AC 16 ms 4860 KiB
random_09 AC 16 ms 4616 KiB
random_10 AC 64 ms 23796 KiB
random_11 AC 2 ms 3604 KiB
random_12 AC 2 ms 3676 KiB
random_13 AC 2 ms 3524 KiB
random_21 AC 2 ms 3472 KiB
random_22 AC 2 ms 3520 KiB
random_23 AC 2 ms 3644 KiB
random_31 AC 2 ms 3648 KiB
random_32 AC 4 ms 3632 KiB
random_33 AC 2 ms 3676 KiB
random_41 AC 2 ms 3524 KiB
random_42 AC 2 ms 3672 KiB
random_43 AC 2 ms 3648 KiB
random_51 AC 3 ms 3676 KiB
random_52 AC 2 ms 3612 KiB
random_53 AC 2 ms 3676 KiB
random_61 AC 40 ms 13260 KiB
random_62 AC 54 ms 18376 KiB
random_63 AC 76 ms 25012 KiB
sample_01 AC 2 ms 3652 KiB
sample_02 AC 2 ms 3504 KiB
sample_03 AC 2 ms 3496 KiB
sample_04 AC 2 ms 3504 KiB