Submission #70488235


Source Code Expand

#include <bits/stdc++.h>


using namespace std;


pair <int,int> getFingerprint(vector <int> &v) {
    int sum = 0, inv = 0;
    for (int i = 0; i < (int) v.size(); i++) {
        inv += v[i] * (i - sum);
        sum += v[i];
    }

    return {sum, inv};
}

vector <int> getPos(vector <int> &v) {
    vector <int> p;
    for (int i = 0; i < (int) v.size(); i++) {
        if (v[i]) {
            p.push_back(i);
        }
    }

    return p;
}

vector <vector<int>> solve(int n, vector <int> &vSource, vector <int> &vTarget) {
    int sum = 0;
    for (int &v : vSource) sum += v;

    if (sum > n - sum) {
        for (int i = 0; i < n; i++) {
            vSource[i] = 1 - vSource[i];
            vTarget[i] = 1 - vTarget[i];
        }
    }

    vector <int> pSource = getPos(vSource), pTarget = getPos(vTarget);

    deque <int> ids;
    vector <vector<int>> ans;

    for (int i = 0; i < (int) pSource.size(); i++) {
        if (pSource[i] == pTarget[i]) {
            continue;
        }

        if (!ids.empty()) {
            int j = ids.back();
            if (pSource[j] < pTarget[j] && pSource[i] > pTarget[i]) {
                int l = min(pTarget[j] - pSource[j], pSource[i] - pTarget[i]);

                ans.push_back({pSource[j], pSource[j] + l, pSource[i] - l, pSource[i]});

                pSource[j] += l;
                pSource[i] -= l;

                if (pSource[j] == pTarget[j]) {
                    ids.pop_back();
                    i--;
                    continue;
                }
            }
        }

        if (pSource[i] != pTarget[i]) {
            ids.push_back(i);
        }
    }

    while (!ids.empty()) {
        int j = ids[0], i = ids.back();
        int l = min(pSource[j] - pTarget[j], pTarget[i] - pSource[i]);

        ans.push_back({pSource[j] - l, pSource[j], pSource[i], pSource[i] + l});

        pSource[j] -= l;
        pSource[i] += l;

        if (pSource[j] == pTarget[j]) {
            ids.pop_front();
        }

        if (pSource[i] == pTarget[i]) {
            ids.pop_back();
        }
    }

    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);

    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n;

        vector <int> a(n), b(n);

        for (int &ai : a) {
            cin >> ai;
        }

        for (int &bi : b) {
            cin >> bi;
        }

        if (getFingerprint(a) != getFingerprint(b)) {
            cout << "No\n";
        } else {
            cout << "Yes\n";

            auto ans = solve(n, a, b);
            cout << ans.size() << '\n';

            for (auto &v : ans) {
                for (int x : v) {
                    cout << x + 1 << ' ';
                }

                cout << '\n';
            }
        }
    }

    return 0;
}

Submission Info

Submission Time
Task B - Swap if Equal Length and Sum
User krismaz
Language C++ 20 (gcc 12.2)
Score 800
Code Size 2923 Byte
Status AC
Exec Time 44 ms
Memory 10308 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 22 ms 4464 KiB
random_10.txt AC 28 ms 5388 KiB
random_11.txt AC 17 ms 4492 KiB
random_12.txt AC 29 ms 7228 KiB
random_13.txt AC 35 ms 10308 KiB
random_14.txt AC 24 ms 5488 KiB
random_15.txt AC 21 ms 4492 KiB
random_16.txt AC 26 ms 5976 KiB
random_17.txt AC 23 ms 4232 KiB
random_18.txt AC 32 ms 6948 KiB
random_19.txt AC 23 ms 5452 KiB
random_2.txt AC 18 ms 4636 KiB
random_20.txt AC 28 ms 5332 KiB
random_21.txt AC 22 ms 5128 KiB
random_22.txt AC 20 ms 5300 KiB
random_23.txt AC 33 ms 7600 KiB
random_24.txt AC 24 ms 5012 KiB
random_25.txt AC 24 ms 5012 KiB
random_26.txt AC 32 ms 7064 KiB
random_27.txt AC 18 ms 4280 KiB
random_28.txt AC 23 ms 4284 KiB
random_29.txt AC 31 ms 7528 KiB
random_3.txt AC 19 ms 4472 KiB
random_30.txt AC 21 ms 4816 KiB
random_4.txt AC 23 ms 5556 KiB
random_5.txt AC 18 ms 4104 KiB
random_6.txt AC 18 ms 4212 KiB
random_7.txt AC 26 ms 7540 KiB
random_8.txt AC 18 ms 4272 KiB
random_9.txt AC 30 ms 7100 KiB
sample.txt AC 1 ms 3216 KiB
small_1.txt AC 44 ms 3216 KiB
small_10.txt AC 42 ms 3376 KiB
small_11.txt AC 42 ms 3300 KiB
small_12.txt AC 42 ms 3412 KiB
small_13.txt AC 43 ms 3428 KiB
small_14.txt AC 42 ms 3232 KiB
small_15.txt AC 42 ms 3424 KiB
small_16.txt AC 7 ms 3372 KiB
small_2.txt AC 43 ms 3424 KiB
small_3.txt AC 42 ms 3424 KiB
small_4.txt AC 42 ms 3240 KiB
small_5.txt AC 42 ms 3420 KiB
small_6.txt AC 42 ms 3416 KiB
small_7.txt AC 42 ms 3536 KiB
small_8.txt AC 42 ms 3240 KiB
small_9.txt AC 42 ms 3436 KiB