CF(穿越火线) 1496B
这道题如果暴力模拟是会tle的,因为k非常大的(亲身经历)
但是我们可以发现max和mex大小关系不一样会造成结果不一致,很明显,
我们用map来存储一下下 因为map会自排序
先 排除一下k==0的情况,
case1: 如果mex > max 那么每一次结果都将稳步上升
m
e
x
=
m
a
x
+
1
mex = max+1
mex=max+1;
m
a
x
=
r
o
u
n
d
(
(
m
a
x
+
m
e
x
)
/
2
)
max = round((max+mex) / 2)
max=round((max+mex)/2)
那么下一次 n也会增加
case2: 如果max > mex 但是 r o u n d ( ( m a x + m e x ) / 2 ) round((max+mex) / 2) round((max+mex)/2) 得到的值是一个已有位置的值,那么下次mex和max将不会更新,然后他就卡bug了,跟k没关系 总数是n
case3: 如果max > mex 但是 r o u n d ( ( m a x + m e x ) / 2 ) round((max+mex) / 2) round((max+mex)/2)得到的值是一个未有位置的值,那么总数将增加1 但是 mex + max 除以2的结果可能是mex 吗 很显然 不可能的 所以 下一次的mex 和max 也不会变 所以总数是n+1
#include<iostream>
#include<map>
#include<cmath>
#define int long long
using namespace std;
signed main() {
int t;cin>>t;
while(t--){
int n,k;cin>>n>>k;
map<int,int> mp;
int _max(0),_mex(0);
for(int i=0;i<n;i++){
int x;cin>>x;mp[x]++;
}
if(k==0) {
cout<<n<<endl;
continue;
}
map<int,int>:: iterator it = mp.end();
_max = ((--it)->first);
for(int i=0;i<=n;i++) {
if(mp.count(i)==0) {
_mex=i;break;
}
}
if(_max<_mex){
cout<<n+k<<endl;
} else {
if(mp.count(round((_max+_mex)/2.0))){
cout<<n<<endl;
} else {
cout<<n+1<<endl;
}
}
}
return 0;
}