Submission #70484863


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll ILL=2167167167167167167;
const int INF=2100000000;
#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> int UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,T b){if(b<a){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,T b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
bool yneos(bool a,bool upp=false){if(a){cout<<(upp?"YES\n":"Yes\n");}else{cout<<(upp?"NO\n":"No\n");}return a;}
template<class T> void vec_out(vector<T> &p,int ty=0){
    if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<",";}cout<<'"'<<p[i]<<'"';}cout<<"}\n";}
    else{if(ty==1){cout<<p.size()<<"\n";}for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}}
template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T vec_sum(vector<T> &a){T ans=T(0);for(auto &x:a) ans+=x;return ans;}
int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;}
template<class T> T square(T a){return a * a;}



void solve();
// POP'N ROLL MUSIC / TOMOO
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    cin >> t;
    rep(i, 0, t) solve();
}

void solve(){
    int N;
    cin >> N;
    vector<int> A(N), B(N);
    rep(i, 0, N) cin >> A[i];
    rep(i, 0, N) cin >> B[i];
    {
        ll diff = 0;
        rep(i, 0, N) if (A[i]) diff += i;
        rep(i, 0, N) if (B[i]) diff -= i;
        if (diff){
            cout << "No\n";
            return;
        }
        if (vec_sum(A) != vec_sum(B)){
            cout << "No\n";
            return;
        }
    }
    cout << "Yes\n";
    if (vec_sum(A) >= (N + 1) / 2){
        rep(i, 0, N) A[i] ^= 1;
        rep(i, 0, N) B[i] ^= 1;
    }
    vector<int> p, q;
    rep(i, 0, N) if (A[i]) p.push_back(i);
    rep(i, 0, N) if (B[i]) q.push_back(i);
    int M = p.size();
    vector<pair<int, int>> L, R;
    rep(i, 0, M){
        if (p[i] < q[i]) R.push_back({p[i], q[i]});
        else L.push_back({p[i], q[i]});
    }
    reverse(all(L));
    vector<tuple<int, int, int, int>> ans;
    auto op = [&](int a, int b, int c, int d) -> void {
        if (b > c) swap(a, c), swap(b, d);
        // cout << a << " " << b << " " << c << " " << d << endl;
        assert(b <= c);
        assert(b - a == d - c);
        int dif = 0;
        rep(i, a, b) dif += A[i];
        rep(i, c, d) dif -= A[i];
        // cout << dif << endl;
        assert(dif == 0);
        rep(i, a, b) swap(A[i], A[i + c - a]);
        ans.push_back({a + 1, b, c + 1, d});
        // vec_out(A);
    };
    rep(i, 0, M - 1){
        if (L.empty() || R.empty()) break;
        auto tmpL = L.back();
        auto tmpR = R.back();
        // cout << tmpL.first << " " << tmpL.second << " " << tmpR.first << " " << tmpR.second << endl;
        if (tmpL.first - tmpL.second < tmpR.second - tmpR.first){
            int len = tmpL.first - tmpL.second;
            op(tmpL.second, tmpL.second + len + 1, tmpR.first, tmpR.first + len + 1);
            R.back().first += len;
            L.pop_back();
        }
        else{
            int len = tmpR.second - tmpR.first;
            op(tmpL.first - len, tmpL.first + 1, tmpR.first, tmpR.second + 1);
            L.back().first -= len;
            R.pop_back();
        }
    }
    cout << ans.size() << "\n";
    for (auto [a, b, c, d] : ans){
        cout << a << " " << b << " " << c << " " << d << "\n";
    }
    assert(A == B);
}


Submission Info

Submission Time
Task B - Swap if Equal Length and Sum
User potato167
Language C++ 17 (gcc 12.2)
Score 800
Code Size 4164 Byte
Status AC
Exec Time 36 ms
Memory 7928 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 1
AC × 47
Set Name Test Cases
Sample sample.txt
All random_1.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_2.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_3.txt, random_30.txt, random_4.txt, random_5.txt, random_6.txt, random_7.txt, random_8.txt, random_9.txt, sample.txt, small_1.txt, small_10.txt, small_11.txt, small_12.txt, small_13.txt, small_14.txt, small_15.txt, small_16.txt, small_2.txt, small_3.txt, small_4.txt, small_5.txt, small_6.txt, small_7.txt, small_8.txt, small_9.txt
Case Name Status Exec Time Memory
random_1.txt AC 21 ms 4348 KiB
random_10.txt AC 30 ms 4804 KiB
random_11.txt AC 14 ms 4648 KiB
random_12.txt AC 31 ms 6108 KiB
random_13.txt AC 35 ms 7928 KiB
random_14.txt AC 23 ms 5272 KiB
random_15.txt AC 20 ms 4592 KiB
random_16.txt AC 28 ms 5432 KiB
random_17.txt AC 22 ms 4064 KiB
random_18.txt AC 36 ms 5728 KiB
random_19.txt AC 22 ms 4976 KiB
random_2.txt AC 15 ms 4700 KiB
random_20.txt AC 30 ms 5064 KiB
random_21.txt AC 22 ms 4856 KiB
random_22.txt AC 18 ms 5304 KiB
random_23.txt AC 35 ms 6320 KiB
random_24.txt AC 23 ms 4800 KiB
random_25.txt AC 24 ms 5104 KiB
random_26.txt AC 34 ms 6008 KiB
random_27.txt AC 17 ms 4344 KiB
random_28.txt AC 23 ms 4152 KiB
random_29.txt AC 33 ms 6232 KiB
random_3.txt AC 19 ms 4484 KiB
random_30.txt AC 20 ms 4624 KiB
random_4.txt AC 24 ms 5316 KiB
random_5.txt AC 16 ms 4112 KiB
random_6.txt AC 16 ms 4268 KiB
random_7.txt AC 29 ms 6624 KiB
random_8.txt AC 15 ms 4340 KiB
random_9.txt AC 32 ms 6168 KiB
sample.txt AC 1 ms 3504 KiB
small_1.txt AC 17 ms 3508 KiB
small_10.txt AC 18 ms 3508 KiB
small_11.txt AC 18 ms 3432 KiB
small_12.txt AC 18 ms 3512 KiB
small_13.txt AC 18 ms 3352 KiB
small_14.txt AC 18 ms 3648 KiB
small_15.txt AC 19 ms 3500 KiB
small_16.txt AC 3 ms 3516 KiB
small_2.txt AC 17 ms 3456 KiB
small_3.txt AC 17 ms 3452 KiB
small_4.txt AC 18 ms 3512 KiB
small_5.txt AC 18 ms 3436 KiB
small_6.txt AC 18 ms 3448 KiB
small_7.txt AC 18 ms 3492 KiB
small_8.txt AC 18 ms 3516 KiB
small_9.txt AC 18 ms 3508 KiB