Submission #40015110


Source Code Expand

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
template<class T,class U> inline bool chmin(T&x,U y){if(x>y){x=y;return true;}return false;}
template<class T,class U> inline bool chmax(T&x,U y){if(x<y){x=y;return true;}return false;}
template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<std::vector<T>> &r){
    if(r.size() == 0){
        out << "||";
        return out;
    }
    for(const auto&v : r) out << v << endl;
    out << endl;
    return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &r){
    if(r.size() == 0){
        out << "[]";
        return out;
    }
    out << "[";
    for(int i{}; i < (int)r.size() - 1; ++i) out << r[i] << ",";
    out << r.back() << "]";
    return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::set<T> &r){
    if(r.size() == 0){
        out << "{}";
        return out;
    }
    out << "{";
    auto e = r.end(); --e;
    for(auto it = r.begin(); it != e; ++it) out << *it << ",";
    out << *e << "}";
    return out;
}

template <class T, class U>
std::ostream &operator<<(std::ostream &out, const std::map<T, U> &r){
    if(r.size() == 0){
        out << "{}";
        return out;
    }
    out << "{";
    auto e = r.end(); --e;
    for(auto it = r.begin(); it != e; ++it) out << it -> first << ":" << it -> second << ",";
    out << e -> first << ":" << e -> second << "}";
    return out;
}
 
template <class T, class U>
std::ostream &operator<<(std::ostream &out, const std::pair<T, U> &r){
    out << "(" << r.first << "," << r.second << ")";
    return out;
}

void solve(){
    int n;
    cin >> n;
    string s;
    cin >> s;
    using P = pair<int, int>;
    vector<P> st;
    vector<int> sm(4*n+1);
    for (int i = 0; i < 2*n; i++)
    {
        int l, r;
        cin >> l  >> r;
        --l, --r;
        if(s[l] == s[r])
        {
            if(s[l] == '(')
            {
                sm[l+1]++;
            }
            else{
                sm[r+1]--;
            }
        }
        else{
            if(s[l] == '(')
            {
                sm[l+1]++;
            }
            else
            {
                sm[r+1]++;
            }
            st.emplace_back(l, r);
        }
    }
    for (int i = 0; i < 4*n; i++)
    {
        sm[i+1] += sm[i];
    }
    
    //cerr << sm << endl;
    if(sm[4*n] < 0 or sm[4*n] % 2 == 1)
    {
        cout << "No" << '\n';
        return;
    }
    int x = sm[4*n] / 2;
    if(x > st.size())
    {
        cout << "No" << '\n';
        return;
    }
    for (int i = 0; i <= 2*n; i++)
    {
        sm[i] -= x;
    }
    for (int i = 2*n + 1; i <= 4*n; ++i)
    {
        sm[i] -= 2*x;
    }
    sort(begin(st), end(st), [](P l, P r){
        return l.second > r.second;
    });
    int id = 0;
    priority_queue<int> pq;
    int tmp = 0;
    vector<int> p(4*n + 1);
    for (int i = 4 * n; i >= 2*n + 1; --i)
    {
        while(id < st.size() and st[id].second == i)
        {
            pq.push(st[id].first);
            ++id;
        }
        while(sm[i] + tmp < 0)
        {
            if(pq.empty())
            {
                cout << "No" << '\n';
                return;
            }
            int L = pq.top();
            pq.pop();
            p[L]++;
            tmp++;
            --x;
            if(x < 0)
            {
                cout << "No" << '\n';
                return;
            }
        }
        sm[i] += tmp;
    }
    while(id < st.size())
    {
        pq.push(st[id].first);
        ++id;
    }
    while(x)
    {
        int L = pq.top();
        pq.pop();
        p[L]++;
        tmp++;
        --x;
    }
    for (int i = 2 * n; i >= 0; --i)
    {
        p[i] += p[i+1];
    }
    for (int i = 0; i <= 4*n; i++)
    {
        if(p[i] + sm[i] < 0)
        {
            cout << "No" << '\n';
            return;
        }
    }
    cout << "Yes" << '\n';
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    unsigned t;
    cin >> t;
    for(unsigned i{}; i < t; ++i){
        solve();
    }
}

Submission Info

Submission Time
Task I - Which must be deleted?
User Motsu_xe
Language C++ (GCC 9.2.1)
Score 100
Code Size 4349 Byte
Status AC
Exec Time 9 ms
Memory 3788 KiB

Compile Error

./Main.cpp: In function ‘void solve()’:
./Main.cpp:110:10: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::pair<int, int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
  110 |     if(x > st.size())
      |        ~~^~~~~~~~~~~
./Main.cpp:132:18: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::pair<int, int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
  132 |         while(id < st.size() and st[id].second == i)
      |               ~~~^~~~~~~~~~~
./Main.cpp:157:14: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::pair<int, int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
  157 |     while(id < st.size())
      |           ~~~^~~~~~~~~~~

Judge Result

Set Name sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 1
AC × 54
Set Name Test Cases
sample 00_sample_01
All 00_sample_01, 01_small_01, 01_small_02, 01_small_03, 01_small_04, 01_small_05, 02_rand_01, 02_rand_02, 02_rand_03, 02_rand_04, 02_rand_05, 02_rand_06, 02_rand_07, 02_rand_08, 02_rand_09, 02_rand_10, 02_rand_11, 02_rand_12, 02_rand_13, 02_rand_14, 02_rand_15, 02_rand_16, 02_rand_17, 02_rand_18, 02_rand_19, 02_rand_20, 02_rand_21, 02_rand_22, 02_rand_23, 02_rand_24, 02_rand_25, 02_rand_26, 02_rand_27, 02_rand_28, 02_rand_29, 02_rand_30, 03_killer_01, 03_killer_02, 03_killer_03, 03_killer_04, 03_killer_05, 03_killer_06, 03_killer_07, 03_killer_08, 03_killer_09, 03_killer_10, 03_killer_11, 03_killer_12, 03_killer_13, 03_killer_14, 03_killer_15, 04_handmade_01, 04_handmade_02, 04_handmade_03
Case Name Status Exec Time Memory
00_sample_01 AC 8 ms 3564 KiB
01_small_01 AC 2 ms 3508 KiB
01_small_02 AC 2 ms 3508 KiB
01_small_03 AC 5 ms 3584 KiB
01_small_04 AC 4 ms 3656 KiB
01_small_05 AC 2 ms 3608 KiB
02_rand_01 AC 4 ms 3516 KiB
02_rand_02 AC 4 ms 3564 KiB
02_rand_03 AC 3 ms 3576 KiB
02_rand_04 AC 3 ms 3580 KiB
02_rand_05 AC 4 ms 3508 KiB
02_rand_06 AC 5 ms 3616 KiB
02_rand_07 AC 4 ms 3572 KiB
02_rand_08 AC 6 ms 3780 KiB
02_rand_09 AC 5 ms 3572 KiB
02_rand_10 AC 5 ms 3636 KiB
02_rand_11 AC 4 ms 3776 KiB
02_rand_12 AC 4 ms 3632 KiB
02_rand_13 AC 4 ms 3724 KiB
02_rand_14 AC 4 ms 3732 KiB
02_rand_15 AC 5 ms 3620 KiB
02_rand_16 AC 5 ms 3624 KiB
02_rand_17 AC 4 ms 3788 KiB
02_rand_18 AC 5 ms 3728 KiB
02_rand_19 AC 4 ms 3788 KiB
02_rand_20 AC 5 ms 3640 KiB
02_rand_21 AC 4 ms 3712 KiB
02_rand_22 AC 5 ms 3696 KiB
02_rand_23 AC 6 ms 3728 KiB
02_rand_24 AC 5 ms 3576 KiB
02_rand_25 AC 4 ms 3688 KiB
02_rand_26 AC 5 ms 3784 KiB
02_rand_27 AC 4 ms 3736 KiB
02_rand_28 AC 9 ms 3728 KiB
02_rand_29 AC 4 ms 3724 KiB
02_rand_30 AC 7 ms 3728 KiB
03_killer_01 AC 4 ms 3496 KiB
03_killer_02 AC 4 ms 3564 KiB
03_killer_03 AC 5 ms 3508 KiB
03_killer_04 AC 4 ms 3608 KiB
03_killer_05 AC 9 ms 3584 KiB
03_killer_06 AC 4 ms 3712 KiB
03_killer_07 AC 6 ms 3632 KiB
03_killer_08 AC 5 ms 3632 KiB
03_killer_09 AC 4 ms 3620 KiB
03_killer_10 AC 4 ms 3636 KiB
03_killer_11 AC 5 ms 3656 KiB
03_killer_12 AC 5 ms 3660 KiB
03_killer_13 AC 8 ms 3656 KiB
03_killer_14 AC 5 ms 3672 KiB
03_killer_15 AC 4 ms 3684 KiB
04_handmade_01 AC 4 ms 3640 KiB
04_handmade_02 AC 4 ms 3704 KiB
04_handmade_03 AC 2 ms 3652 KiB