codeforces B. Make Them Odd -C++stl之set的使用

B. Make Them Odd

There are nn positive integers a1,a2,,ana1,a2,…,an. For the one move you can choose any even value cc and divide by two all elements that equal cc.

For example, if a=[6,8,12,6,3,12]a=[6,8,12,6,3,12] and you choose c=6c=6, and aa is transformed into a=[3,8,12,3,3,12]a=[3,8,12,3,3,12] after the move.

You need to find the minimal number of moves for transforming aa to an array of only odd integers (each element shouldn't be divisible by 22).

Input

The first line of the input contains one integer tt (1t1041≤t≤104) — the number of test cases in the input. Then tt test cases follow.

The first line of a test case contains nn (1n21051≤n≤2⋅105) — the number of integers in the sequence aa. The second line contains positive integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109).

The sum of nn for all test cases in the input doesn't exceed 21052⋅105.

Output

For tt test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by 22).

Example

4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7

output

4
10
4
0

  这是用c++的STL暴力过的,用set容器,他的特点就是各个元素是唯一的,相比于map,set还支持自定义排序,默认是int升序排列,字符是按字典序排列的

set的详细使用可以看:https://blog.csdn.net/byn12345/article/details/79523516

暴力不用多说直接看代码(带有注释):

//https://blog.csdn.net/sinat_37158899/article/details/79328104 
#include<bits/stdc++.h>
using namespace std;
int a[200005];
bool cmp (int a,int b)
{
	return a>b;
}
struct cmp2
{
	bool operator () (const int a,const int b)
	{
		return a>b;//降序 
	}
};
set<int,cmp2> ss;//默认从小到大,将字符串按字母序进行排序。 
int main(void)
{
	int t,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		sort(a+1,a+n+1,cmp);
		
		int top=0;int flag=0;
		ss.clear();
		
		for(int i=1;i<=n;i++){
			if(a[i]!=a[i-1]&&a[i]%2==0)
			ss.insert(a[i]),flag=1;
		}
		long long int ans=0;
		set<int>::iterator it;
		//printf("svdsvf\n");
		while(flag)
		{
			flag=0;
			//for(it=ss.begin();it!=ss.end();it++) printf("%d ",*it);
			//printf("\n");
			for(it=ss.begin();it!=ss.end();it++){
				if(*it%2==0)
				{
					flag=1;
					ans++;
					int san=*it;
					ss.erase(san);
					san/=2;
					//printf("没有失效:%d\n",*it);//set的it也会失效 ,vector的会 
					//注意不要使用过期的iterator 
					if(san%2==0)
					ss.insert(san);
					break;
				}
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/xuanmaiboy/p/12041611.html
今日推荐