题解:AT_arc223_e [ARC223E] Yin-Yang Two Bits Insertion

· · 题解

感觉场切这个题最难的部分在于你要快速做完 A~D 然后还有足够的时间来开这个题。

注意到两个东西:a_i \oplus a_{i+1} = (1-a_i) \oplus (1-a_{i+1}),a_i \oplus (1-a_i) = 1。

我们自然联想到把序列转换成 a_i \leftarrow a_i \oplus a_{i-1},发现这个操作相当于选择一个位置 1 < i \le n,然后在 a_i 左侧和右侧都插入一个 1。

那我们对 A,B 序列做同样的转换后有最基本的要求 a_1 = b_1,而且两个序列中 0 的个数应当相同。

那么我们判完 i=1 后把第一个位置去掉,容易得到剩下的 0 的个数也是相同的。我们可以用 0 把两个序列划分成若干个全 1 段,每次操作相当于选两个相邻的连续段让它们长度各加 1,或者选中一个长度非零段让它长度加 2。

考虑 dp 解决。设 f_{i,j,0/1} 为已经处理了前 i 段,第 i 段还缺 j 个,长度是否非零。

发现当一个段的长度非零了就可以不停地 +2,所以可以把状态优化成 f_{i,0/1/2,0/1},表示第 i 段不缺/缺奇数/缺偶数,然后就可以线性递推求解了。式子应该不难推,可以看代码。

不难吧!场上怎么没有想着做这个题呢 /ll

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pii;
#define fi first
#define se second

const int N = 2e5+10;

int n,m;
int a[N],b[N];
int a1[N],b1[N];
int tota,totb;
bool f[N][3][2];

inline int get(int x) {return x?(x&1)?1:2:0;}

bool check()
{
    f[0][0][0] = 1;
    for(int i=1;i<=tota;i++)
    {
        for(int _:{0,1,2}) for(int __:{0,1}) f[i][_][__] = 0;
        if(a1[i] > b1[i]) return 0;
        int now = b1[i] - a1[i];
        f[i][get(now)][(a1[i]>0)?1:0] |= (f[i-1][0][0] | f[i-1][0][1] | f[i-1][2][1]);
        if(now >= 1) f[i][get(now-1)][1] |= (f[i-1][1][0] | f[i-1][1][1]);
        if(now >= 2) f[i][get(now-2)][1] |= (f[i-1][2][0] | f[i-1][2][1]);
    }
    return f[tota][0][0] | f[tota][0][1] | f[tota][2][1];
}
void solve()
{
    tota = totb = 0;
    cin >> n >> m;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i];
    }
    for(int i=1;i<=m;i++)
    {
        cin >> b[i];
    }
    for(int i=n;i>=1;i--) a[i] ^= a[i-1];for(int i=m;i>=1;i--) b[i] ^= b[i-1];
    if(a[1] != b[1])
    {
        cout << "No\n";
        return;
    }
    int len=0;
    for(int i=2;i<=n;i++)
    {
        if(!a[i])
        {
            a1[++tota] = len;
            len=0;
        }
        else len++;
    }
    a1[++tota] = len;
    len=0;
    for(int i=2;i<=m;i++)
    {
        if(!b[i])
        {
            b1[++totb] = len;
            len=0;
        }
        else len++;
    }
    b1[++totb] = len;
    if(tota != totb || !check()) cout << "No\n";
    else cout << "Yes\n";
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T;
    cin >> T;
    while(T--)
    {
        solve();
    }
    return 0;
}