题解:P12055 [THUPC 2025 决赛] 一个 01 串,n 次三目运算符,最后值为 1

· · 题解

题解

大分讨。我们先考虑什么情况下有解?因为每次操作是三个数为一组,所以我们可以把基本情况在草稿纸上列出来(在此不列出)。我们尝试总结一点神秘结论,比如若最后为一,只有 101 是无解的,其余均有解。还有就是连着两个一跟着什么东西好像也有解,但是 10010 这样也有解但是这又该怎么归类呢?

首先对于奇数位置为一且后面位置有偶数位置有一时一定有解。对于这种我们可以刻画成 ??1??1? 的结构,现在我们尝试对此化简。看到一中间偶数个位置,考虑我们先随便操作,到最后还剩下两个位置的时候我们可以进行分类讨论。若现在为形如 1xy1 的结构,我们看 x=0/1 的做法:

因此我们解决了中间间隔为偶数的情况,现在考虑形如 ??11? 的做法。如果只有后面三个数我们肯定是直接做就对了,但是实际前面还有一段,所以我们希望维持最后这个结构。我们还是讨论一下:

于是我们就解决完奇数位置为一且后面位置有偶数位置有一的情况。现在考虑最后一个位置为一的情况。因为我们已经做了前面一种情况所以不会出现一个奇数位置为一后面又有偶数位置为一。我们还是只看最后五个位置,这样不失一般性。现在开始大力讨论:

然后你就发现貌似是讨论完了,对于长度为三的情况就暴力特判即可。

大概讲一下代码,首先你先特判长度为三的序列,直接分讨,然后注意每次合并你就当成维护并查集即可。把整个流程画出来你发现其实就是一个三叉树的形式,并且如果你从根开始 dfs 就行了。但是你这样做就不知道每次合并后相邻的位置的情况,所以你改装一下这棵树,加入链表的特征,让他能维护前后,每次合并同时维护链表即可。

代码

/*
 * @Author: Nekopedia 
 * @Date: 2025-06-27 11:12:19 
 * @Last Modified by: Nekopedia
 * @Last Modified time: 2025-06-27 12:16:59
 */
#include <bits/stdc++.h>
#define ll long long
#define gc getchar
#define pc putchar
using namespace std;
const int N = 5e5 + 5, S = 1 << 25;
inline ll rd(){
    ll x = 0, f = 1; char c = gc();
    while(! isdigit(c)){if(c == '-')f = - f; c = gc();}
    while(isdigit(c)){x = (x << 3) + (x << 1) + (c ^ 48); c = gc();}
    return x * f;
}
inline void wt(ll x){
    if(x < 0)pc('-'), x = - x; static short st[20], top(0);
    do st[++top] = x % 10, x /= 10; while(x);
    while(top)pc(st[top--] ^ 48);
}

int n, tot, p1, p2, st, ed;
struct node{int son[3], x, las, nxt; inline void clr(){son[0] = son[1] = son[2] = 0;}}a[N];
inline void cmb(int x, int y, int z){
    a[++tot] = {x, y, z};
    a[tot].x = a[x].x ? a[y].x : a[z].x;
    a[tot].las = a[x].las; a[tot].nxt = a[z].nxt;
    a[a[x].las].nxt = tot; a[a[z].nxt].las = tot;
    if(x == st)st = tot; if(z == ed)ed = tot;
}
void dfs(int x){
    if(! a[x].son[0])return wt(a[x].x), void();
    pc('('); dfs(a[x].son[0]); pc('?');
    dfs(a[x].son[1]); pc(':'); dfs(a[x].son[2]); pc(')');
}

inline void special_solve(){
    if(! a[2].x and ! a[3].x)return puts("No"), void();
    if(a[1].x != a[2].x and a[2].x != a[3].x)return puts("No"), void();
    puts("Yes");
    if(! a[1].x)return printf("0?%d:1\n", a[2].x), void();
    printf("1?1:%d\n", a[3].x);
}
inline void case1_solve(){
    puts("Yes");
    for(int i = p1 + 3; i < p2; i += 2){
        int x1 = a[i].las, x2 = a[x1].las;
        cmb(x2, x1, i);
    }
    int x = a[p1].nxt, y = a[p2].las;
    if(x != p2){
        if(a[x].x)cmb(p1, x, y), p1 = tot;
        else cmb(x, y, p2), p2 = tot;
    }
    for(int i = a[p2].nxt; i != ed; i = tot){
        int j = a[i].nxt, k = a[j].nxt;
        cmb(i, j, k);
    }
    if(p1 == st){
        cmb(p1, p2, a[p2].nxt);
        return dfs(tot), pc('\n'), void();
    }
    for(int i = st; a[a[i].nxt].nxt != p1; i = tot){
        int j = a[i].nxt, k = a[j].nxt;
        cmb(i, j, k);
    }
    if(a[st].x)cmb(a[st].nxt, p1, p2), cmb(st, tot, ed);
    else cmb(st, a[st].nxt, p1), cmb(tot, p2, ed);
    dfs(tot); pc('\n');
}
inline void case2_solve(){
    puts("Yes");
    for(int i = st; a[a[i].nxt].nxt != a[a[ed].las].las; i = tot){
        int j = a[i].nxt, k = a[j].nxt;
        cmb(i, j, k);
    }
    if(a[st].x)cmb(st, a[st].nxt, a[a[st].nxt].nxt), cmb(tot, a[ed].las, ed);
    else cmb(a[st].nxt, a[a[st].nxt].nxt, a[ed].las), cmb(st, tot, ed);
    dfs(tot); pc('\n');
}

signed main(){
    for(int T = rd(); T--; p1 = p2 = 0){
        tot = n = rd() << 1 | 1; for(int i = 1; i <= n; ++i){
            char c; cin >> c; a[i].x = c ^ 48;
            a[i].las = i - 1, a[i].nxt = i + 1;
        }
        st = 1, ed = n;
        if(n == 3){special_solve(); goto lyr;}
        for(int i = 1; i <= n; ++i){
            if(i & 1 and a[i].x and ! p1)p1 = i;
            if(i + 1 & 1 and a[i].x and p2 < p1)p2 = i;
        }
        if(p1 and p1 < p2)case1_solve();
        else if(a[n].x)case2_solve();
        else puts("No");
        lyr : for(int i = 1; i <= tot; ++i)a[i].x = a[i].las = a[i].nxt = 0, a[i].clr();
    }
    return 0;
}