CF1927B 题解

· · 题解

思路

这就是一个贪心题,若 a_i=0,则把 s_i 设为一个目前没有出现过的字符;否则就把 s_i 设为任意一个在此之前已经出现了 a_i-1 次的字符。但是数据范围太大,所以从 a_{i-1} 开始倒着枚举当然是不行的,因此我们可以使用 n 个队列,q_i 里存的是所有在此之前已经出现了 i 次的字符。每次添加元素的时候(即当 a_i\ne0 时),我们就可以取出 q_{a_i-1} 的队头充当 s_i,并把它弹了再加到 q_i 里。

代码

# include <bits/stdc++.h>
using namespace std;
int t, n, x;
queue <char> q[200005];
char tot, ans;
int main () {
    ios::sync_with_stdio (0);
    cin.tie (0);
    cout.tie (0);
    cin >> t;
    while (t --) {
        cin >> n;
        tot = 'a'; //tot 是指当前第一个没出现过的字符
        for (int i = 0; i < n; ++ i) {
            while (! q[i].empty ()) //多组数据要清空
                q[i].pop ();
            cin >> x;
            if (x)
                cout << (ans = q[x - 1].front ()), q[x - 1].pop (), q[x].push (ans); //在 q[x-1] 里找元素
            else
                cout << (ans = tot), q[0].push (tot), ++ tot;
        }
        cout << '\n';
    }
    return 0;
}