CF2205C.Simons and Posting Blogs
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This life is like a TV show; we're swept along as the plotlines go.
— SHUN, TRAP
There are n blogs. The i-th blog mentioned li users in order as an array ai=[ai,1,ai,2,…,ai,li].
You are going to post all n blogs. Let us maintain a sequence Q that describes the list of users you have recently mentioned. You need to perform the following operation exactly n times:
- Choose an unposted blog i (1≤i≤n), then post it. This will cause the following operations for each 1≤j≤li in order:
- If ai,j already exists in Q, then move ai,j to the beginning of Q.
- Otherwise, insert ai,j at the beginning of Q.
Find the lexicographically smallest∗ Q after all n operations.
∗An array x is lexicographically smaller than an array y if and only if one of the following holds:
- x is a prefix of y, but x=y; or
- in the first position where x and y differ, the array x has a smaller element than the corresponding element in y.
人生就像一档电视节目;我们随剧情发展而被裹挟前行。
—— SHUN,《TRAP》链接
共有 n 篇博客。第 i 篇博客按顺序提到了 li 个用户,记为数组 ai=[ai,1,ai,2,…,ai,li]。
你将依次发布全部 n 篇博客。我们维护一个序列 Q,用于记录你最近提及过的用户列表。你需要恰好执行 n 次如下操作:
- 任选一篇尚未发布的博客 i(其中 1≤i≤n),然后将其发布。该操作将按顺序对每个 1≤j≤li 执行以下子操作:
- 若 ai,j 已存在于 Q 中,则将 ai,j 移至 Q 的开头;
- 否则,将 ai,j 插入至 Q 的开头。
求所有 n 次操作完成后,字典序最小的∗ 序列 Q。
∗ 数组 x 字典序小于数组 y,当且仅当满足以下任一条件:
- x 是 y 的真前缀(即 x 是 y 的前缀但 x=y);或
- 在 x 与 y 首次出现差异的位置上,x 中对应位置的元素严格小于 y 中对应位置的元素。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000). The description of the test cases follows.
The first line contains a single integer n (1≤n≤3000) — the number of blogs.
Then n lines follow, the i-th line starting with an integer li (1≤li≤3000), describing the number of users mentioned in the i-th blog, which is followed by li integers ai,1,ai,2,…,ai,li (1≤ai,j≤106) — the list of users mentioned in the i-th blog.
It is guaranteed that the sum of n over all test cases does not exceed 3000.
Denote i=1∑nli as L. It is guaranteed that the sum of L over all test cases does not exceed 3000.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤1000)。随后是各测试用例的描述。
第一行包含一个整数 n(1≤n≤3000)—— 博客的数量。
接下来是 n 行,其中第 i 行以一个整数 li(1≤li≤3000)开头,表示第 i 个博客中提及的用户数量;随后是 li 个整数 ai,1,ai,2,…,ai,li(1≤ai,j≤106)—— 第 i 个博客中提及的用户列表。
保证所有测试用例的 n 之和不超过 3000。
记 i=1∑nli 为 L。保证所有测试用例的 L 之和不超过 3000。
输出格式
Denote m as the number of users mentioned in at least one blog. For each test case, output m integers Q1,Q2,…,Qm — the lexicographically smallest Q.
记 m 为在至少一篇博客中被提及的用户数量。对于每个测试用例,输出 m 个整数 Q1,Q2,…,Qm —— 字典序最小的 Q。
输入输出样例
输入#1
5 3 5 1 2 3 4 6 3 2 5 1 4 1 9 2 3 2 2 1 6 1 6 1 3 6 1 1 5 4 2 3 3 4 5 1 2 4 3 1 2 4 1 3 3 3 1 5 4 3 2 2 2 5 4 2 3 1 4 5 2 5 5 6 5 5 3 4 7 5 5 8 3 6 4 3 1 1 5 4 2 1 1
输出#1
1 5 2 3 9 6 4 6 1 1 6 1 3 2 4 1 4 3 2 5 6 7
说明/提示
In the first test case, you can post the blogs as follows:
- Post the first blog, and Q will become [6,4,3,2,1].
- Post the third blog, and Q will become [3,2,9,1,6,4].
- Post the second blog, and Q will become [1,5,2,3,9,6,4].
There is another method to post blogs:
- Post the third blog, and Q will become [3,2,9,1].
- Post the first blog, and Q will become [6,4,3,2,1,9].
- Post the second blog, and Q will become [1,5,2,6,4,3,9].
We can see that [1,5,2,3,9,6,4] is lexicographically smaller than the other one. If we do not post the second blog at the end, the first element of the array will not be 1, so [1,5,2,3,9,6,4] is the lexicographically smallest array Q.
In the second test case, you can post the blogs as follows:
- Post the first blog, and Q will become [6,1].
- Post the second blog, and Q will keep itself [6,1].
In the third test case, you have to post the only blog, and Q will become [1,6].
在第一个测试用例中,你可以按如下方式发布博客:
- 发布第一篇博客,此时 Q 变为 [6,4,3,2,1]。
- 发布第三篇博客,此时 Q 变为 [3,2,9,1,6,4]。
- 发布第二篇博客,此时 Q 变为 [1,5,2,3,9,6,4]。
还存在另一种发布博客的方法:
- 发布第三篇博客,此时 Q 变为 [3,2,9,1]。
- 发布第一篇博客,此时 Q 变为 [6,4,3,2,1,9]。
- 发布第二篇博客,此时 Q 变为 [1,5,2,6,4,3,9]。
我们可以看出,[1,5,2,3,9,6,4] 在字典序上小于另一个结果。如果我们不将第二篇博客放在最后发布,则数组的第一个元素将不为 1,因此 [1,5,2,3,9,6,4] 是字典序最小的数组 Q。
在第二个测试用例中,你可以按如下方式发布博客:
- 发布第一篇博客,此时 Q 变为 [6,1]。
- 发布第二篇博客,此时 Q 保持不变,仍为 [6,1]。
在第三个测试用例中,你必须发布唯一的一篇博客,此时 Q 将变为 [1,6]。
输入解题思路,AI测评打分。不知道怎么写?