codeforces B. Make Them Odd -C++stl之set的使用
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).
The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases in the input. Then tt test cases follow.
The first line of a test case contains nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of integers in the sequence aa. The second line contains positive integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).
The sum of nn for all test cases in the input doesn't exceed 2⋅1052⋅105.
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;
}
codeforces B. Make Them Odd -C++stl之set的使用的更多相关文章
- Educational Codeforces Round 24 A 水 B stl C 暴力 D stl模拟 E 二分
A. Diplomas and Certificates time limit per test 1 second memory limit per test 256 megabytes input ...
- codeforces 710C C. Magic Odd Square(构造)
题目链接: C. Magic Odd Square Find an n × n matrix with different numbers from 1 to n2, so the sum in ea ...
- Codeforces Round #413 B T-shirt buying (STL set)
链接:http://codeforces.com/contest/799/problem/B 题意: 给定n件衣服,对于第i(1<i<=n)件衣服,分别有价格pi,前颜色ai,后颜色bi三 ...
- CodeForces 990B Micro-World(思维、STL)
http://codeforces.com/problemset/problem/990/B 题意: 有n个细菌,每个细菌的尺寸为ai,现在有以常数k,如果细菌i的尺寸ai大于细菌j的尺寸aj,并且a ...
- Codeforces 731 C.Socks-并查集+STL(vector+map)
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Educational Codeforces Round 42D. Merge Equals(STL)
D. Merge Equals time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #606(B-D)
Dashboard - Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4) - Codeforces ...
- ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syria, Lattakia, Tishreen University, April, 30, 2018
ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
随机推荐
- 洛谷--P1028 数的计算(递推)
题意:链接:https://www.luogu.org/problem/P1028 先输入一个自然数n (n≤1000) , 然后对此自然数按照如下方法进行处理: 不作任何处理; 在它的左边加上一个自 ...
- 虚拟机出现“The system is running in low-graphics mode”的解决方法
我用虚拟机安装了ubuntu的版本后,又在上面安装了几个插件,第二次打开虚拟机就出现下面的提示: 通过在网上查找教程,找到了以下的解决方法 1.Ctrl+Alt+F1进入控制台 2.输入用户密码 3. ...
- Python之路【第二十四篇】:数据库索引
数据库索引 一.索引简介 索引在mysql中也叫做"键",是存储引擎用于快速找到记录的一种数据结构.索引对于良好的性能非常关键,尤其是当表中的数据量越来越大时,索引对于性能的影响愈 ...
- 介绍一款好用的命令行工具Cmder
一.Cmder的介绍: 在大多数情况下,我们都想复制命令行窗口中的命令行,但是cmd复制粘贴大家都懂得:有没有更好的工具替代呢? 答案是肯定的,今天我将为大家介绍一款工具--Cmder. Cmder可 ...
- 全栈项目|小书架|微信小程序-登录回调及获取点赞列表功能
效果图 这一节介绍,登录回调 以及 喜欢列表 的实现. 登录回调:这里是指在获取登录完成之后,再进行下一步的操作. 比如效果图中我的页面,默认是未登录状态,积分和喜欢列表的数量都没有获取到. 而登录成 ...
- nodeJS微信JSDK 配置
nodeJS微信JSDK 配置 一.微信公众平台申请一个测试公众号 二.配置nodeJS 使用express框架搭建服务器 微信生成签名步骤: 1.获取token 2.通过token,获取jsapi_ ...
- Python人工智能第二篇:人脸检测和图像识别
Python人工智能第二篇:人脸检测和图像识别 人脸检测 详细内容请看技术文档:https://ai.baidu.com/docs#/Face-Python-SDK/top from aip impo ...
- 2. ES6基础-let和const命令
目录 1. let命令 1.1 用法 1. 2 不存在变量提升 1.3 区域绑定 1.4 不允许重复声明 2. const命令 2.1 用法 2.2 与let类似的特性 2.3 const本质 2.4 ...
- 激活windows去掉右下角水印
激活windows去掉右下角水印 //需要隔一段时间执行一次 // 卸载已有的激活产品slmgr.vbs /upk // 重新按照激活产品slmgr /ipk NPPR9-FWDCX-D2C8J-H ...
- Vue项目整体架构记要
此文记录初次学习Vue的一些记要,可做为参考,导图有些生涩,故意不再加以修复完善,以持初心!