Submission #13118554


Source Code Expand

/*!enjoy karo yaar!*/

//CF,CC,AtC,SPOJ: hp1999
//HE: hemant269
//HR: hemant2132

// ref: https://codeforces.com/blog/entry/77148?#comment-619728

#include<bits/stdc++.h>
using namespace std;

#define     int             long long int
#define     fast()          ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define     all(x)          begin(x),end(x)
#define     rz(x)           resize(x)
#define     asn(x,y)        assign(x,y)
#define     mem(a,b)        memset(a,b,sizeof(a))
#define     sz(x)           ((int)(x.size()))
#define     eb              emplace_back
#define     pb              push_back
#define     pf              push_front
#define     pob             pop_back
#define     pof             pop_front
#define     ins             insert
#define     vi              vector<int>
#define     pii             pair<int,int>
#define     mii             map<int,int>
#define     F               first
#define     S               second
#define     makep           make_pair
#define     maket           make_tuple
#define     remax(a,b)      a=max(a,b)
#define     remin(a,b)      a=min(a,b)
#define     bitcount(x)     __builtin_popcountll(x)
#define     iceil(n,x)      (((n)+(x)-1)/(x))
#define     gcd(a,b)        (__gcd((a),(b)))
#define     lcm(a,b)        ((a)*((b)/gcd((a),(b))))
#define     dbug(x)         cout<<#x<<": "<<(x)<<"\n"
#define     flush           fflush(stdout)
#define     show(x)         for(auto zz:x)cout<<zz<<" ";cout<<"\n";
#define     show2(x)        for(auto zz:x)cout<<zz.F<<" "<<zz.S<<"\n";

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

const ld pi=acos(-1);
const ll inf=1e18,M=1e9+7;
const int N=1;

void solve()
{
    int n;
    cin>>n;

    vector<pii> pve,nve;

    string s;
    int cnt,len;
    pii p;
    for(int i=0;i<n;++i)
    {
        cin>>s;
        len=sz(s);

        cnt=0;
        for(int j=0;j<len;++j)
        {
            if(s[j]=='(')
                ++cnt;
            else if(cnt)
                --cnt;
        }
        p.S=cnt;        // unbalanced "("

        cnt=0;
        for(int j=len-1;j>=0;--j)
        {
            if(s[j]==')')
                ++cnt;
            else if(cnt)
                --cnt;
        }
        p.F=cnt;        // unbalanced ")"

        if(p.S-p.F>=0)
            pve.pb(p);
        else
            nve.pb(p);
    }

    sort(all(pve),[](pii a,pii b){return a.F<b.F;});
    sort(all(nve),[](pii a,pii b){return a.S>b.S;});

    vector<pii> a;
    a.reserve(n);
    a.insert(end(a),all(pve));
    a.insert(end(a),all(nve));

    int sum=0;
    for(auto c:a)
    {
        sum-=c.F;

        if(sum<0)
        {
            cout<<"No";
            return;
        }

        sum+=c.S;
    }

    if(sum)
        cout<<"No";
    else
        cout<<"Yes";
}

int32_t main()
{
    fast();

    int t=1;
    //cin>>t;
    for(int z=1;z<=t;++z)
    {
        solve();
        //cout<<"\n";
    }

    return 0;
}

Submission Info

Submission Time
Task F - Bracket Sequencing
User hp1999
Language C++ (GCC 9.2.1)
Score 600
Code Size 3083 Byte
Status AC
Exec Time 100 ms
Memory 34608 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 42 ms 11396 KiB
random_02 AC 87 ms 29052 KiB
random_03 AC 55 ms 14992 KiB
random_04 AC 100 ms 34608 KiB
random_05 AC 97 ms 34488 KiB
random_06 AC 49 ms 16984 KiB
random_07 AC 18 ms 4676 KiB
random_08 AC 18 ms 3688 KiB
random_09 AC 18 ms 3660 KiB
random_10 AC 52 ms 16920 KiB
random_11 AC 2 ms 3452 KiB
random_12 AC 2 ms 3564 KiB
random_13 AC 2 ms 3576 KiB
random_21 AC 2 ms 3560 KiB
random_22 AC 2 ms 3564 KiB
random_23 AC 2 ms 3452 KiB
random_31 AC 2 ms 3540 KiB
random_32 AC 2 ms 3416 KiB
random_33 AC 2 ms 3480 KiB
random_41 AC 2 ms 3568 KiB
random_42 AC 2 ms 3560 KiB
random_43 AC 2 ms 3568 KiB
random_51 AC 2 ms 3516 KiB
random_52 AC 2 ms 3564 KiB
random_53 AC 2 ms 3572 KiB
random_61 AC 32 ms 8136 KiB
random_62 AC 36 ms 10468 KiB
random_63 AC 50 ms 15132 KiB
sample_01 AC 2 ms 3548 KiB
sample_02 AC 2 ms 3512 KiB
sample_03 AC 2 ms 3564 KiB
sample_04 AC 2 ms 3468 KiB