跳到正文

题解

CF1927B 题解

Vitamin_B约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年2月9日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 2 分钟
题目
CF1927B

版本

  1. v1当前

思路

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

代码

C++
# 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;
}
原文