CF1896F Tutorial

· · 题解

0 前言

高妙构造题!

Tony 老师还是太强大了。唉,是这样的。

讲的很长,主要是为了说清楚。

1 题目大意

给你一个由 \texttt{01} 构成的序列。你可以做不超过 10 次如下操作:选定一个合法括号序列,对每一对位置为 l,r 的匹配括号,将序列上 [l,r] 的位置取反。

2 题解

本题有多种解法,以下分享类似于 小吴同学wtc 的解法。

事实上,感觉本题只要确定一个比较对的方向,然后想,就可以做出来了。因为限制其实给的很松,正统 wtc 解法上限为 3,我就稍微菜一点,要 4。

首先我们先判断一手无解。显然,如果 a_1 \operatorname{xor} a_{2n} = 1 或者 \bigoplus_{i = 1}^{2n} a_i = 1 那么是无解的。

其他情况都是有解的,会在接下来的解法中说明。

首先忽略 a_1 和 a_{2n},考虑什么情况下是可以做到做一次就可以满足全部数都是 \texttt 0 或者 \texttt 1。

显然的,类似于 \texttt{100001111110011001} 是可以做到一次转换的。具体的,输出序列可以为 \texttt{(()())()()(())(())}。构造方法如下:

  1. 两两匹配所有的 \texttt 1,分别向它们赋值 \texttt ( 和 \texttt)。
  2. 两两匹配所有的 \texttt 0,分别向它们赋值 \texttt ( 和 \texttt)。

容易观察到如果满足上面的条件,构造出来的括号序列一定是合法的。

那么,什么样的序列才能满足这样构造满足条件呢?

设一段两两匹配的 \texttt 1 区间为 [l,r]。那么,我们要求 \forall [l,r] , r - l - 1 \equiv 2 \pmod 0,即中间夹着的 \texttt 0 均为偶数个。显然序列如果是这样的格式,就可以按照上述方法构造满足要求。

接着想怎么让一个任意数列构造成这个样子。我们可以说明任意的之前判断有解的数列都可以变成这种格式。

先 rand 一个数列方便理解(忽略掉了第一个和最后一个):

\texttt{0110101011000101010110100111}

这是一个任意的,满足有解条件的序列。不妨先给他们两两分组。

发现这么一件事情,一个满足要求(即使得存在一次转换使得满足全部数相同)的数列一定是 \texttt{10} 然后接若干段 \texttt{00} 然后接一个 \texttt {01} 或者是 \texttt{11} 这两种东西拼接在一起。

不妨来决定哪个两两构成的小组是要“翻转”的。来演示一下流程。

首先是对于一组内存在两个不同的数,我们按照上面的规则,决定翻转还是不翻转:

\texttt{[01]10101011000101010110100111}

选中的该组为 \texttt{01},前面没有 \texttt{10},于是它就要变为 \texttt{10},要翻转。

\texttt{{\color{purple}01}[10]101011000101010110100111}

(红色表示要翻转的颜色,蓝色表示“前面的”\texttt{10},而紫色就是二者兼备)

选中的改组为 \texttt{10},前面有 \texttt{10},于是他就要变为 \texttt{01},要翻转。

\texttt{{\color{red}0110}{\color{blue}10}[11]000101010110100111}

现在来到了一组内存在两个相同的数的情况,我们仍然是按照上面的规则,决定翻转还是不翻转。

由于选中的该组为 \texttt{11},而前面存在 \texttt{10},那么它是要变为 \texttt{00} 的,要翻转。

\texttt{{\color{red}0110}10{\color{red}11}0001{\color{red}01}01{\color{red}0110}1001[11]}

而对于选中的该组为 \texttt{11},由于前面不存在 \texttt{10},那么它是不需要翻转的。

最终结果如下:

\texttt{{\color{red}0110}10{\color{red}11}0001{\color{red}01}01{\color{red}0110}100111}

那么,我们简单总结一下:我们记录一个标记表示前面存不存在 \texttt{10}。如果遇到一组内存在两个不同的数,那么翻转标记,同时按照要求决定要不要翻转这组数。如果一组内两个数相同,就按照标记的要求修改即可。

那么最后一个关键问题来了,我们如何处理“两个两个翻转的这种操作”?以及,如果你还记得,我们为什么要忽视 a_1, a_{2n} 这两个数?

仍然还是以上面那个数列为例子,这次,我们在两侧加上 a_{1} 和 a_{2n}。

\texttt{{\color{green}1}{\color{red}0110}10{\color{red}11}0001{\color{red}01}01{\color{red}0110}100111{\color{green}1}}

首先我们先在两侧填写 \texttt ( 和 \texttt )。

\texttt{{\color{green}1}{\color{red}0110}10{\color{red}11}0001{\color{red}01}01{\color{red}0110}100111{\color{green}1}} \\ \texttt{({\color{red}??}????????????????????????)}

此时我们还是一组一组的考虑。红色表示我正在考虑的位置。假设这个组是要翻转的。那么,不难发现,填写 \texttt{)(} 就可以符合要求。为什么?因为我们之前已经有了一个左括号(这正是之前忽略 a_1 和 a_{2n} 的原因,为了填写 \texttt{(} 以及 \texttt )),所以此时填写 \texttt{)(} 就可以使得左边的数被翻转。而右边的那个数,由于最右侧有一个括号,所以最后它也是会被翻转的。

不难发现,此后考虑序列的时候仍然存在一个左括号的环境,而且由于左右两侧括号的存在,这样填写一定合法,所以可以推广到所有的需要翻转的组:

\texttt{{\color{green}1}{\color{red}0110}10{\color{red}11}0001{\color{red}01}01{\color{red}0110}100111{\color{green}1}} \\ \texttt{()()({\color{red}??}????????????????????)}

那么,对于不需要翻转的组,我们怎么做呢?考虑填写 \texttt{()}。检验合理性和上述两个条件。首先,由于之前已经有一个左括号环境,那么此时这一个组就相当于翻转了两次,就相当于没有翻转,是合理的。然后是两个条件,首先左括号环境还在,没问题。而且这样子填写也肯定还是合法的。

那么,问题就解决了,按照上述方法全部填写完毕并操作。

\texttt{{\color{green}1}{\color{red}0110}10{\color{red}11}0001{\color{red}01}01{\color{red}0110}100111{\color{green}1}} \\ \texttt{()()(())(()())(())()(()()())} \\ \texttt{0100110000001100110011001110}

然后再做一下我们之前提到的操作。之前忽略了 a_1 和 a_{2n},仿照上述方法,填写 \texttt ( 和 \texttt{)}。我们就得到了如下结果:

\texttt{0100110000001100110011001110} \\ \texttt{((())(()()())(())(())(())())} \\ \texttt{1111111111111111111111111111}

最后一个问题,如何让所有数变成 \texttt{0}?

这个随便做吧,只要用 \texttt{(()()()...()())} 来翻转上述情况(也有可能不需要),然后用 \texttt{()()()...()()} 把整个序列变成 \texttt 0 即可。

操作次数最多 4 次。时间复杂度明显是 O(n) 的。

2 代码

很丑很丑。

#include<bits/stdc++.h>
using namespace std;

const int N = 4e5 + 405;

int t, n, a[N];
bool isflp[N];
int opr[11][N], oprsum = 0;

int main() {
    scanf("%d", &t);
    for(; t; --t) {
        oprsum = 0;
        scanf("%d", &n);
        char str = getchar();
        int len = 0;
        while(str < 33 || str > 126) str = getchar();
        while(str >= 33 && str <= 126) {
            a[++len] = str - '0';
            str = getchar();
        }
        if(a[1] != a[2 * n]) {
            printf("-1\n");
            continue;
        }
        int xorsum = 0;
        for(int i = 1; i <= 2 * n; i++) {
            xorsum ^= a[i];
        }
        if(xorsum) {
            printf("-1\n");
            continue;
        }
        bool tag = 0;
        for(int i = 2; i < 2 * n; i += 2) {
            if(tag == 0) {
                if(a[i] == 0 && a[i + 1] == 0) isflp[i >> 1ll] = 1;
                if(a[i] == 0 && a[i + 1] == 1) isflp[i >> 1ll] = 1, tag = 1;
                if(a[i] == 1 && a[i + 1] == 0) isflp[i >> 1ll] = 0, tag = 1;
                if(a[i] == 1 && a[i + 1] == 1) isflp[i >> 1ll] = 0;
            }
            else if(tag == 1) {
                if(a[i] == 0 && a[i + 1] == 0) isflp[i >> 1ll] = 0;
                if(a[i] == 0 && a[i + 1] == 1) isflp[i >> 1ll] = 0, tag = 0;
                if(a[i] == 1 && a[i + 1] == 0) isflp[i >> 1ll] = 1, tag = 0;
                if(a[i] == 1 && a[i + 1] == 1) isflp[i >> 1ll] = 1;
            }
        }
        oprsum++;
        a[1] ^= 1, a[2 * n] ^= 1;
        opr[oprsum][1] = 1, opr[oprsum][2 * n] = 2;
        for(int i = 2; i < 2 * n; i += 2) {
            if(isflp[i >> 1ll]) opr[oprsum][i] = 2, opr[oprsum][i + 1] = 1, a[i] ^= 1, a[i + 1] ^= 1;
            else opr[oprsum][i] = 1, opr[oprsum][i + 1] = 2;
        }
        oprsum++;
        a[1] ^= 1, a[2 * n] ^= 1;
        opr[oprsum][1] = 1, opr[oprsum][2 * n] = 2;
        tag = 0;
        bool tag2 = 0;
        for(int i = 2; i < 2 * n; i++) {
            if(tag == 0) {
                if(a[i] == 1) opr[oprsum][i] = 1, tag = 1, tag2 = 0;
            }
            else if(tag == 1) {
                if(a[i] == 1) opr[oprsum][i] = 2, tag = 0;
                else if(a[i] == 0) {
                    if(tag2 == 0)
                        opr[oprsum][i] = 1, tag2 = 1;
                    else if(tag2 == 1)
                        opr[oprsum][i] = 2, tag2 = 0;
                }
            }
        }
        oprsum++;
        if(a[1] == 1) {
            for(int i = 1; i <= 2 * n; i += 2)
                opr[oprsum][i] = 1, opr[oprsum][i + 1] = 2;
        }
        else {
            opr[oprsum][1] = 1, opr[oprsum][2 * n] = 2;
            for(int i = 2; i < 2 * n; i += 2)
                opr[oprsum][i] = 1, opr[oprsum][i + 1] = 2;
            oprsum++;
            for(int i = 1; i <= 2 * n; i += 2)
                opr[oprsum][i] = 1, opr[oprsum][i + 1] = 2;
        }
        printf("%d\n", oprsum);
        for(int i = 1; i <= oprsum; i++) {
            for(int j = 1; j <= 2 * n; j++) {
                printf("%c", opr[i][j] == 1 ? '(' : ')');
            }
            printf("\n");
        }
    }
    return 0;
}