[AGC074B] Swap if Equal Length and Sum

· · 题解

或许更好的阅读体验。

很神秘的构造,感觉完全想不到。

思路:

首先注意一下不变量,显然 0/1 的数量不变;更深层次的,考虑 1 的所有下标和,显然每次操作左右会分别平移抵消掉,于是下标和也不变。

根据不变量可以轻松判定无解情况;我们需要证明满足不变量的都有解,可以给出一种构造:

于是最多操作 n 次就可以得到解,但是题目限制是 \frac{n}{2},想到 \min(cnt_0, cnt_1) \le \frac{n}{2},所以如果 cnt_1 > \frac{n}{2},那么就去匹配 0 即可。

时间复杂度为 O(n)。

完整代码:

#include<bits/stdc++.h>
#define ls(k) k << 1
#define rs(k) k << 1 | 1
#define fi first
#define se second
#define popcnt(x) __builtin_popcount(x)
#define open(s1, s2) freopen(s1, "r", stdin), freopen(s2, "w", stdout);
using namespace std;
typedef __int128 __;
typedef long double lb;
typedef double db;
typedef unsigned long long ull;
typedef long long ll;
const int N = 2.5e5 + 10;
inline ll read(){
    ll x = 0, f = 1;
    char c = getchar();
    while(c < '0' || c > '9'){
        if(c == '-')
          f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9'){
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return x * f;
}
inline void write(ll x){
    if(x < 0){
        putchar('-');
        x = -x;
    }
    if(x > 9)
      write(x / 10);
    putchar(x % 10 + '0');
}
int T, n, ca, cb;
int a[N], b[N], ida[N], idb[N];
vector<int> VL, VR;
inline void solve(){
    ll s1 = 0, s2 = 0;
    n = read();
    for(int i = 1; i <= n; ++i)
      a[i] = read(), s1 += a[i] * i;
    for(int i = 1; i <= n; ++i)
      b[i] = read(), s2 += b[i] * i;
    if(s1 != s2){
        puts("No");
        return ;
    }
    s1 = s2 = 0;
    for(int i = 1; i <= n; ++i)
      s1 += a[i], s2 += b[i];
    if(s1 != s2){
        puts("No");
        return ;
    }
    if(s1 > (n >> 1)){
        for(int i = 1; i <= n; ++i)
          a[i] ^= 1, b[i] ^= 1;
        s1 = n - s1;
    }
    ca = cb = 0;
    for(int i = 1; i <= n; ++i){
        if(a[i])
          ida[++ca] = i; 
        if(b[i])
          idb[++cb] = i;
    }
    VL.clear(), VR.clear();
    for(int i = 1; i <= s1; ++i){
        // cerr << ida[i] << " to " << idb[i] << '\n';
        if(ida[i] > idb[i])
          VL.push_back(i);
        else if(ida[i] < idb[i])
          VR.push_back(i);
    }
    reverse(VR.begin(), VR.end());
    vector<pair<pair<int, int>, pair<int, int>>> ans;
    int h1 = 0, h2 = 0;
    while(h1 < (int)VL.size() && h2 < (int)VR.size()){
        int x = VL[h1], y = VR[h2];
        if(ida[x] - idb[x] > idb[y] - ida[y]){
            // cerr << "???1\n";
            // cerr << x << ' ' << y << '\n';
            ans.push_back({{ida[x] - (idb[y] - ida[y]), ida[x]}, {ida[y], idb[y]}});
            ++h2;
            ida[x] -= (idb[y] - ida[y]);
        }
        else if(ida[x] - idb[x] < idb[y] - ida[y]){
            // cerr << "???2\n";
            ans.push_back({{idb[x], ida[x]}, {ida[y], ida[y] + (ida[x] - idb[x])}});
            ++h1;
            ida[y] += (ida[x] - idb[x]);
        }
        else{
            // cerr << "???3\n";
            ans.push_back({{idb[x], ida[x]}, {ida[y], idb[y]}});
            ++h1, ++h2;
        }
    }
    assert(h1 == (int)VL.size() && h2 == (int)VR.size());
    puts("Yes");
    write(ans.size());
    putchar('\n');
    for(auto t : ans){
        if(t.fi.fi > t.se.fi)
          swap(t.fi, t.se);
        write(t.fi.fi);
        putchar(' ');
        write(t.fi.se);
        putchar(' ');
        write(t.se.fi);
        putchar(' ');
        write(t.se.se);
        putchar('\n'); 
    }
}
int main(){
    T = read();
    while(T--)
      solve();
    return 0;
}