提出 #70488316


ソースコード 拡げる

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = std::forward<U>(y), true); }

template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = std::forward<U>(y), true); }

template <class T> void mkuni(std::vector<T>& v) {
    std::ranges::sort(v);
    auto result = std::ranges::unique(v);
    v.erase(result.begin(), result.end());
}

template <class T> int lwb(const std::vector<T>& v, const T& x) {
    return std::distance(v.begin(), std::ranges::lower_bound(v, x));
}

using namespace std;

using ll = long long;

void solve() {
    int N;
    cin >> N;
    vector<int> A(N), B(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }
    for (int i = 0; i < N; i++) {
        cin >> B[i];
    }

    int one = accumulate(A.begin(), A.end(), 0);
    if (N - one < one) {
        for (int i = 0; i < N; i++) {
            A[i] ^= 1;
            B[i] ^= 1;
        }
    }
    vector<int> p, q;
    for (int i = 0; i < N; i++) {
        if (A[i] == 1) {
            p.emplace_back(i);
        }
        if (B[i] == 1) {
            q.emplace_back(i);
        }
    }
    if (p.size() != q.size() or accumulate(p.begin(), p.end(), 0) != accumulate(q.begin(), q.end(), 0)) {
        cout << "No\n";
        return;
    }

    vector<tuple<int, int, int, int>> ans;
    int M = p.size();
    queue<int> neg, pos;
    for (int i = 0; i < M; i++) {
        if (p[i] > q[i]) {
            pos.emplace(i);
        }
    }
    for (int i = M - 1; i >= 0; i--) {
        if (p[i] < q[i]) {
            neg.emplace(i);
        }
    }
    auto op = [&](int i, int j, int len) {
        int a = p[i], b = p[i] + len;
        int c = p[j] - len + 1, d = p[j] + 1;
        if (a > c) {
            swap(a, c);
            swap(b, d);
        }
        ans.emplace_back(a, b, c, d);
        p[i] += len - 1;
        p[j] -= len - 1;
    };
    while (not neg.empty()) {
        auto i = neg.front();
        auto j = pos.front();
        auto I = abs(p[i] - q[i]);
        auto J = abs(p[j] - q[j]);
        auto len = min(I, J) + 1;
        op(i, j, len);
        if (p[i] == q[i]) {
            neg.pop();
        }
        if (p[j] == q[j]) {
            pos.pop();
        }
    }
    cout << "Yes\n";
    cout << ans.size() << "\n";
    for (auto [a, b, c, d] : ans) {
        cout << a + 1 << " " << b << " " << c + 1 << " " << d << "\n";
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int T;
    cin >> T;
    for (; T--;) {
        solve();
    }
    return 0;
}

提出情報

提出日時
問題 B - Swap if Equal Length and Sum
ユーザ rniya
言語 C++ 23 (gcc 12.2)
得点 800
コード長 2805 Byte
結果 AC
実行時間 31 ms
メモリ 7436 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 800 / 800
結果
AC × 1
AC × 47
セット名 テストケース
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
ケース名 結果 実行時間 メモリ
random_1.txt AC 18 ms 4212 KiB
random_10.txt AC 26 ms 4772 KiB
random_11.txt AC 13 ms 4252 KiB
random_12.txt AC 26 ms 5680 KiB
random_13.txt AC 31 ms 7436 KiB
random_14.txt AC 21 ms 4904 KiB
random_15.txt AC 17 ms 4656 KiB
random_16.txt AC 22 ms 4972 KiB
random_17.txt AC 20 ms 4184 KiB
random_18.txt AC 29 ms 5548 KiB
random_19.txt AC 20 ms 4576 KiB
random_2.txt AC 16 ms 5592 KiB
random_20.txt AC 25 ms 4840 KiB
random_21.txt AC 19 ms 4804 KiB
random_22.txt AC 16 ms 5088 KiB
random_23.txt AC 30 ms 5856 KiB
random_24.txt AC 22 ms 4584 KiB
random_25.txt AC 23 ms 4560 KiB
random_26.txt AC 29 ms 5372 KiB
random_27.txt AC 17 ms 5356 KiB
random_28.txt AC 21 ms 4044 KiB
random_29.txt AC 27 ms 5772 KiB
random_3.txt AC 16 ms 4240 KiB
random_30.txt AC 18 ms 4568 KiB
random_4.txt AC 20 ms 5220 KiB
random_5.txt AC 16 ms 4664 KiB
random_6.txt AC 16 ms 5348 KiB
random_7.txt AC 23 ms 6192 KiB
random_8.txt AC 16 ms 5140 KiB
random_9.txt AC 26 ms 5632 KiB
sample.txt AC 1 ms 3372 KiB
small_1.txt AC 21 ms 3440 KiB
small_10.txt AC 22 ms 3352 KiB
small_11.txt AC 22 ms 3388 KiB
small_12.txt AC 22 ms 3296 KiB
small_13.txt AC 22 ms 3420 KiB
small_14.txt AC 21 ms 3384 KiB
small_15.txt AC 22 ms 3352 KiB
small_16.txt AC 4 ms 3224 KiB
small_2.txt AC 20 ms 3428 KiB
small_3.txt AC 20 ms 3376 KiB
small_4.txt AC 21 ms 3428 KiB
small_5.txt AC 22 ms 3292 KiB
small_6.txt AC 21 ms 3400 KiB
small_7.txt AC 21 ms 3424 KiB
small_8.txt AC 21 ms 3380 KiB
small_9.txt AC 21 ms 3240 KiB