大意就是给一个序列,可能有重复数字,有x次机会为这个序列填上一个数字,问最终从里面获得的1~v连续子序列的v最大是多少。
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 a1, his place in the second round is a2, …, his place in the n-th round is an.
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≤i≤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≤t≤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≤n,x≤100). The second line contains n positive non-zero integers a1,a2,…,an (1≤ai≤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≤i≤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
其实读懂这道题的题意,基本都能写出来,A题一般都简单,这里说下我对这道题的分析:
1.我首先想到map,记录出现的值。
2.用一个while循环,直到找到第m次的“空位”
3.结果用了减二,因为while循环,先判断再运行再判断这样就会多加2
4.起始即为1,题目要求从1开始。
#include
#include
#include
#include
using namespace std;
const int M=1e3+10;
int t,n,m,ans,k;
int a[M];
int main()
{
scanf("%d",&t);
while(t--)
{
map<int,int>mp;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
mp[a[i]]++;
}
k=0;
ans=1;
while(k<=m)
{
if(!mp[ans])
k++;
ans++;
}
printf("%d\n",ans-2);
}
}
最近想提高下自己博客的阅读量,就会开始尽量把以后所有写过的CodeForces,发到博客上,因为题会新一点,也希望能帮到你们,我会开一个新分类。就酱!