Dreamoon 热衷于参加 Codeforces 竞赛。
一天,他声称自己再参加两场 rated 场后,就会获得过从第 1 1 1 名到第 54 54 54 名的所有名次。这真是不可思议!
基于此,你提出了如下一个问题:
张三参加了 n n n 场 Codeforces 比赛。他第一场的排名为 a 1 a_1 a1,第二场的排名为 a 2 a_2 a2,……,第 n n n 场的排名为 a n a_n an。
给出一个正整数 x x x。请找到最大的 v v v,满足张三在再参加 x x x 场 rated 比赛后,有可能取得 1 1 1 到 v v v 中所有名次。
换句话说,你需要找到最大的 v v v,满足在继续参加 x x x 场 rated 比赛后,有可能对于任意的 1 ≤ i ≤ v 1 \le i \le v 1≤i≤v,存在一场张三排在第 i i i 名的比赛。
举个例子,如果 n = 6 , x = 2 , a = [ 3 , 1 , 1 , 5 , 7 , 10 ] n = 6, x = 2, a = [3, 1, 1, 5, 7, 10] n=6,x=2,a=[3,1,1,5,7,10],那么答案 v = 5 v = 5 v=5。因为如果接下来的两次比赛内,张三分别取得了第 2 2 2 名和第 4 4 4 名,他可以获得过第 1 1 1 名到第 5 5 5 名内的所有名次,所以 v = 5 v = 5 v=5 是可能达成的最大答案。
第一行输入一个整数 t ( 1 ≤ t ≤ 5 ) t ~ (1 \le t \le 5) t (1≤t≤5),表示测试数据组数。
对于每组测试数据,输入两行。
第一行输入两个整数 n , x ( 1 ≤ n , x ≤ 100 ) n, x ~ (1 \le n, x \le 100) n,x (1≤n,x≤100)。
第二行输入 n n n 个正整数 a 1 , a 2 , … , a n ( 1 ≤ a i ≤ 100 ) a_1, a_2, \ldots, a_n ~ (1 \le a_i \le 100) a1,a2,…,an (1≤ai≤100)。
对于每组测试数据,输出一行一个整数 v v v,表示在再参加 x x x 次比赛后,有可能实现对于所有 1 ≤ i ≤ v 1 \le i \le v 1≤i≤v,存在一场比赛张三排在第 i i i 名。
第一组测试数据的说明见【题目描述】。
对于第二组测试数据,张三将会再参加 100 100 100 场比赛,他可以以任意顺序取得第 1 , 2 , … , 99 1, 2, \ldots, 99 1,2,…,99 和 101 101 101 名,就获得过第 1 , 2 , … , 101 1, 2, \ldots, 101 1,2,…,101 名中的所有名词。
Dreamoon is a big fan of the Codeforces contests.
One day, he claimed that he will collect all the places from $ 1 $ to $ 54 $ after two more rated contests. It’s amazing!
Based on this, you come up with the following problem:
There is a person who participated in $ n $ Codeforces rounds. His place in the first round is $ a_1 $ , his place in the second round is $ a_2 $ , …, his place in the $ n $ -th round is $ a_n $ .
You are given a positive non-zero integer $ x $ .
Please, find the largest $ v $ such that this person can collect all the places from $ 1 $ to $ v $ after $ x $ more rated contests.
In other words, you need to find the largest $ v $ , such that it is possible, that after $ x $ more rated contests, for each $ 1 \leq i \leq v $ , there will exist a contest where this person took the $ i $ -th place.
For example, if $ n=6 $ , $ x=2 $ and $ a=[3,1,1,5,7,10] $ then answer is $ v=5 $ , because if on the next two contest he will take places $ 2 $ and $ 4 $ , then he will collect all places from $ 1 $ to $ 5 $ , so it is possible to get $ v=5 $ .
The first line contains an integer $ t $ ( $ 1 \leq t \leq 5 $ ) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains two integers $ n, x $ ( $ 1 \leq n, x \leq 100 $ ). The second line contains $ n $ positive non-zero integers $ a_1, a_2, \ldots, a_n $ ( $ 1 \leq a_i \leq 100 $ ).
For each test case print one line containing the largest $ v $ , such that it is possible that after $ x $ other contests, for each $ 1 \leq i \leq v $ , there will exist a contest where this person took the $ i $ -th place.
5
6 2
3 1 1 5 7 10
1 100
100
11 1
1 1 1 1 1 1 1 1 1 1 1
1 1
1
4 57
80 60 40 20
5
101
2
2
60
The first test case is described in the statement.
In the second test case, the person has one hundred future contests, so he can take place $ 1,2,\ldots,99 $ and place $ 101 $ on them in some order, to collect places $ 1,2,\ldots,101 $ .
#include
#include
int compare(const void* p, const void* q)
{
return *(int*)p - *(int*)q;
}
int find(int* arr, int n,int start,int x)
{
int i = 0, ans=1;
if (arr[0] != 1)
{
x -= 1;
}
while(i<n)
{
if (arr[i] - ans > 1)//如果遇到跳跃1个以上的数字
{
if (x)
{
x -= 1;
ans++;
}
else
{
break;
}
}
else if (arr[i] - ans == 1)
{
i++;
ans++;
}
else
i++;
}
if (x)
ans += x;
return ans;
}
int main(void)
{
int t;
scanf("%d", &t);
while (t--)
{
int n, x, arr[101] = { 0 };
scanf("%d %d", &n, &x);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
qsort(arr, n, sizeof(int), compare);
int ans=find(arr, n, 1,x);
printf("%d\n", ans);
}
return 0;
}
条理的重要性