题解:P7915 [CSP-S 2021] 回文

思路分析

当一个数 \(a_i\) 被取出时,与它相等的数 \(a_j\) 可以视为被“冻结”。

考虑这个样例:

4
4 1 2 4 5 3 1 2 3 5

假设我们取出 \(a_1=4\) 作为 \(b_1\),那么 \(a_4=4\) 必须作为 \(b_{2n}\) 被最后取出,所以我们将 \(a_4\) 视为“冻结”,在前 \(n\) 个数中无法取出它们。

那么此时要取第二个数,那么它的对应数必须是 \(a_3=2\) 或 \(a_5=5\),否则 \(a_3\) 或 \(a_5\) 位于两个冻结的数中,它需要在两个冻结的数之前取走,但两边都有被冻结的数,无法取走。

推广:对于某一时刻,被“冻结”的数一定构成一个连续的段。

由于我们只能从序列的两端取数,所以需要检查两段的数是否符合要求。

算法设计

对于某一时刻,被冻结的部分视为 \([l,r]\),当前剩余还未被取走的部分视为 \([x,y]\)。

每次操作优先尝试在左侧取数,左侧无法取数时在考虑右侧。

下面是对样例的模拟:(红色表示已被取走,蓝色表示被冻结)

编号 \(1\) \(2\) \(3\) \(4\) \(5\) \(6\) \(7\) \(8\) \(9\) \(10\)
数值 \(\color{red}4\) \(1\) \(2\) \(\color{blue}4\) \(5\) \(3\) \(1\) \(2\) \(3\) \(5\)

操作序列:L。

编号 \(1\) \(2\) \(3\) \(4\) \(5\) \(6\) \(7\) \(8\) \(9\) \(10\)
数值 \(\color{red}4\) \(1\) \(2\) \(\color{blue}4\) \(\color{blue}5\) \(3\) \(1\) \(2\) \(3\) \(\color{red}5\)

操作序列:LR。

编号 \(1\) \(2\) \(3\) \(4\) \(5\) \(6\) \(7\) \(8\) \(9\) \(10\)
数值 \(\color{red}4\) \(1\) \(2\) \(\color{blue}4\) \(\color{blue}5\) \(\color{blue}3\) \(1\) \(2\) \(\color{red}3\) \(\color{red}5\)

操作序列:LRR。

编号 \(1\) \(2\) \(3\) \(4\) \(5\) \(6\) \(7\) \(8\) \(9\) \(10\)
数值 \(\color{red}4\) \(\color{red}1\) \(2\) \(\color{blue}4\) \(\color{blue}5\) \(\color{blue}3\) \(\color{blue}1\) \(2\) \(\color{red}3\) \(\color{red}5\)

操作序列:LRRL(此时虽然取 \(2\) 也可以,但是左侧优先)

编号 \(1\) \(2\) \(3\) \(4\) \(5\) \(6\) \(7\) \(8\) \(9\) \(10\)
数值 \(\color{red}4\) \(\color{red}1\) \(\color{red}2\) \(\color{blue}4\) \(\color{blue}5\) \(\color{blue}3\) \(\color{blue}1\) \(\color{blue}2\) \(\color{red}3\) \(\color{red}5\)

操作序列:LRRLL,最后补上冻结部分的操作就得到 LRRLLRRRRL。

时间复杂度 \(O(n)\)。

代码实现

第一次不一定在左侧取,要左侧右侧均尝试一次。

#include<bits/stdc++.h>
using namespace std;
string solve(char first, int n, const vector<int>& a, const vector<int>& rev) {
    int x = 1, y = 2 * n;
    int l, r;
    string ans;
    vector<int> st;
    if(first == 'L'){
        int p = rev[x];
        l = r = p;
        st.push_back(x);
        ans.push_back('L');
        ++x;
    } else {
        int p = rev[y];
        l = r = p;
        st.push_back(y);
        ans.push_back('R');
        y--;
    }
    for(int step = 2; step <= n; step++){
        bool ok = false;
        if(x <= y && (x < l || x > r)){
            int p = rev[x];
            if(p == l - 1 || p == r + 1){
                if(p == l - 1) l--;
                else r++;
                st.push_back(x);
                ans.push_back('L');
                x++;
                ok = true;
            }
        }
        if(ok) continue;
        if(x <= y && (y < l || y > r)) {
            int p = rev[y];
            if(p == l - 1 || p == r + 1){
                if(p == l - 1) l--;
                else r++;
                st.push_back(y);
                ans.push_back('R');
                y--;
                ok = true;
            }
        }
        if(!ok) return "";
    }
    while(!st.empty()){
        int t = st.back();
        st.pop_back();
        int p = rev[t];
        if(p == l){
            ans.push_back('L');
            ++l;
        } else if(p == r){
            ans.push_back('R');
            --r;
        } else {
            return "";
        }
    }
    if((int)ans.size() != 2 * n) return "";
    return ans;
}
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int T; cin >> T;
    while(T--){
        int n; cin >> n;
        vector<int> a(2 * n + 1), rev(2 * n + 1), pos(n + 1, 0);
        for(int i = 1; i <= 2 * n; i++){
            cin >> a[i];
            if(pos[a[i]]){
                rev[i] = pos[a[i]];
                rev[pos[a[i]]] = i;
            } else {
                pos[a[i]] = i;
            }
        }
        string ans = solve('L', n, a, rev);
        if(ans.empty()) ans = solve('R', n, a, rev);
        if(ans.empty()) cout << -1 << '\n';
        else cout << ans << '\n';
    }
    return 0;
}
posted @ 2026-10-18 18:50  地上飞的鱼  阅读(21)  评论(0)    收藏  举报