map.1

map

map容器是一个键值对key-value的映射,
其内部实现是一棵以key为关键码的红黑树。
Map的key和value可以是任意类型,其中key必须定义小于号运算符。
	声明
		map<key_type, value_type> name;
		例如:
		map<long, long, bool> vis;
		map<string, int> hash;
		map<pair<int, int>, vector<int>> test;

与map类似的是pair
pair包含两个数值,与容器一样,pair也是一种模板类型。
但是又与之前介绍的容器不同;

pair<string,string> p1;
pair<int ,string> p2;
pair<int ,double> p3;
pair<string,vector<int>> p4;
#include<iostream>

using namespace std;

int main()
{
	pair<int ,int >p;
	p.first=1;
	p.second=2;
	cout<<p.first<<endl<<p.second<<endl;
	return 0;
}
size/empty/clear/begin/end均与set类似。

insert/erase
	与set类似,但其参数均是pair<key_type, value_type>。
find
	h.find(x) 在变量名为h的map中查找key为x的二元组。
	[]操作符
	h[key] 返回key映射的value的引用,时间复杂度为O(log n)。
	[]操作符是map最吸引人的地方。
	我们可以很方便地通过h[key]来得到key对应的value,
	还可以对h[key]进行赋值操作,改变key对应的value。

Max and Mex
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a multiset S initially consisting of n distinct non-negative integers. A multiset is a set, that can contain some elements multiple times.

You will perform the following operation k times:

Add the element ⌈a+b2⌉ (rounded up) into S, where a=mex(S) and b=max(S). If this number is already in the set, it is added again.
Here max of a multiset denotes the maximum integer in the multiset, and mex of a multiset denotes the smallest non-negative integer that is not present in the multiset. For example:

mex({1,4,0,2})=3;
mex({2,5,1})=0.
Your task is to calculate the number of distinct elements in S after k operations will be done.

Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n, k (1≤n≤105, 0≤k≤109) — the initial size of the multiset S and how many operations you need to perform.

The second line of each test case contains n distinct integers a1,a2,…,an (0≤ai≤109) — the numbers in the initial multiset.

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output
For each test case, print the number of distinct elements in S after k operations will be done.
题意:
给定一个仅包含非负整数的数组,其中没有重复元素,进行K次操作,每次操作为选出max和mex将[(max+mex)/2]加入到数组中,其中max为数组中最大的元素,mex为数组中不存在的非负最小整数,如数组a包含0,1,3,4,则mex为2。问进行完K次操作后,数组中有多少个不同元素。

说点题外话
在做题的时候,我原本使用的是比较常规的方法,但是在测试3的时候,经常空间超限
我在搜题解的过程中,发现别人用的是stl中的map
我在学习stl的时候,像map,vector,set,stack,queue这些,我都是只了解了一下名字。因此,我又去复习了一下,在做题的时候我才发现,这些东西真的很好用
题解:
k=0时, 输入多少种,就有多少种 结果为n
k!=0时,
我们要先找到出mex
如果mx>mex
对于新元素y=(mx+mex+1)/2有两种情况
y是不会影响到mex的,且不会影响mx

  1. y=已存在 结果为n
  2. y=未知(x) 之后无论操作多少次 y与x恒等 结果为 n+1

如果mx<=mex
(四舍五入) 每一次都会产生一个新的元素 结果为 n+k

#include<iostream>
#include<map>

using namespace std;

int main()
{
	map<int ,bool> mp;
	int t,n,k;
	cin>>t;
	while(t--)
	{
		cin>>n>>k;
		mp.clear();
		int a[n+1],mx=0,mex=0;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			mx=max(mx,a[i]);
			mp[a[i]]=1;
		}
		if(k==0)
		{
			cout<<n<<endl;
			continue;	
		}
		for(int i=0;i<=mx;i++)
		{
			if(mp[i]!=1) 
			{
				mex=i;
				break;
			}else mex=i+1;
		}
		if(mx>mex)
		{
			if(mp[(mx+mex+1)/2]==1) cout<<n<<endl;
			else cout<<n+1<<endl;
		}else{
			cout<<n+k<<endl;
		}
	}
	return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值