CF1927B 题解

思路

这就是一个贪心题,若 ai=0a_i=0,则把 sis_i 设为一个目前没有出现过的字符;否则就把 sis_i 设为任意一个在此之前已经出现了 ai1a_i-1 次的字符。但是数据范围太大,所以从 ai1a_{i-1} 开始倒着枚举当然是不行的,因此我们可以使用 nn 个队列,qiq_i 里存的是所有在此之前已经出现了 ii 次的字符。每次添加元素的时候(即当 ai0a_i\ne0 时),我们就可以取出 qai1q_{a_i-1} 的队头充当 sis_i,并把它弹了再加到 qiq_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;
}
posted @ 2024-02-09 19:55  Vitamin_B  阅读(9)  评论(0)    收藏  举报  来源