Make Them Odd
time limit per test3 seconds
memory limit per test256 megabytes
input: standard input
output: standard output
There are n positive integers a1,a2,…,an. For the one move you can choose any even value c and divide by two all elements that equal c.
For example, if a=[6,8,12,6,3,12] and you choose c=6, and a is transformed into a=[3,8,12,3,3,12] after the move.
You need to find the minimal number of moves for transforming a to an array of only odd integers (each element shouldn’t be divisible by 2).
Input
The first line of the input contains one integer t(1≤t≤104) — the number of test cases in the input. Then t test cases follow.
The first line of a test case contains n(1≤n≤2⋅105) — the number of integers in the sequence a. The second line contains positive integers a1,a2,…,an(1≤ai≤109).The sum of n for all test cases in the input doesn’t exceed 2⋅105.
Output
For t 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 2).
Example
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
题意:
t组数,每组n个数,每一次操作可以把数组里相同的同时除以2,直到数组里所有的数都变成了奇数
只要用一个数组把每一个偶数的变化过程标记下来,如果循环到某一个数字时当前数字已经被标记就结束当前循环,然后记录一共除了多少次就好。介于数字过大,不用定义一个int数组,所以用map就好了(不过这么简单的题目 emmm 应该不会有人查博客吧)
记录这题是因为这个做法让我想起了以前cc哥哥出过的一道题,想让我们在本地跑5分钟才能得出答案,但因为用了这个做法就得以快速ac。那道题找不到了,就用这道题来代替下,让自己有一个印象~
还是贴一个代码叭:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int t,n;
ll times;
int cmp(int a,int b){
return a>b;
}
int main(){
cin>>t;
while(t--){
cin>>n;
times = 0;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+1+n,cmp);
map<int,int> mp;
for(int i=1;i<=n;i++){
if(a[i]%2==1){
continue;
}else{
while(a[i]%2==0 && mp[a[i]]==0){
mp[a[i]]=1;
a[i]/=2;
times++;
}
}
}
cout<<times<<endl;
}
return 0;
}

给定一个整数数组,你需要通过选择任意偶数值并将其所有出现的元素除以2的操作,将数组转化为仅包含奇数的数组。题目要求找出达到此目标的最少操作次数。提供的输入包括测试用例数量、每个测试用例的数组长度及数组元素。输出为每个测试用例的最少操作次数。
524

被折叠的 条评论
为什么被折叠?



