368 字
2 分钟
Little-Fairy's-Painting

题面#

The little fairy has a ribbon with 101810^{18} cells and an infinite variety of colors. The first nn cells of the ribbon have already been colored, where the ii-th cell is colored with color aia_i.

Little fairy will color the remaining cells in order from n+1n+1 to 101810^{18}. For the ii-th cell:

  • Little fairy first counts the number of distinct colors currently present on the ribbon, denoted as cic_i.
  • Then she will color the ii-th cell with color cic_i.

What color will the fairy use for the 101810^{18}-th cell?

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t5001 \le t \le 500). The description of the test cases follows.

The first line of each test case contains a single integer nn (1n1001 \le n \le 100) — the number of colored cells.

The second line of each test case contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n (1ai1031 \le a_i \le 10^3) — the colors of the cells.

Output

For each test case, print the color of the last cell.

思路#

设初始出现过的颜色集合为 SSk=Sk=|S|
对于任意一步,若当前不同颜色数为 kk 则新染色为 kk

kSk\in S,则本次不引入新颜色且之后每步仍染 kk 因而答案为 kk
kSk\notin S,则加入新颜色 kk,使集合大小变为 k+1k+1kk+1k\leftarrow k+1

因此,kk 单调递增直到首次满足 kSk\in S 为止
实现上从 k=Sk=|S| 开始循环检查,若不在则插入并 kk+1k\leftarrow k+1 否则输出 kk \

代码#

#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t --) {
int n;
cin >> n;
unordered_set<int> c;
c.reserve(n * 2 + 10);
for (int i = 0; i < n; i ++) {
int x;
cin >> x;
c.insert(x);
}
int k = (int)c.size();
while (true) {
if (c.count(k)) {
cout << k << endl;
break;
}
c.insert(k);
k ++;
}
}
}
Little-Fairy's-Painting
https://github.com/posts/little-fairys-painting/
作者
FZSGBall
发布于
2026-05-23
许可协议
CC BY-NC-SA 4.0