CF 1496B

本文介绍了在CF1496B题目中,如何通过巧妙利用map数据结构和max、mex概念,避免暴力模拟,解决大规模k值情况下的时间复杂度问题。通过分析max和mex的关系,作者提供了三种关键场景的解决方案,确保在最短时间找到正确答案。
摘要由CSDN通过智能技术生成

CF(穿越火线) 1496B

B. Max and Mex

这道题如果暴力模拟是会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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值