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 ...
随机推荐
- php mysql的储存过程 实现批量更新
UPDATE newhouse_clicks SET clicks = CASE id WHEN 1 THEN 1000 WHEN 2 THEN 2000 WHEN 3 THEN 3000 END W ...
- hadoop2.x大数据视频教程(十二天学会)
- php GD 和图像处理函数, 用 STHUPO.TTF 字体向图像写入文本
php GD 和图像处理函数, 用 STHUPO.TTF 字体向图像写入文本 注意: 01) imagettftext() 这个函数不能使用相对路径, 要想使用相对路径要先使用 puten ...
- An Illustrated Proof of the CAP Theorem
An Illustrated Proof of the CAP Theorem The CAP Theorem is a fundamental theorem in distributed syst ...
- C# vb .net实现装饰边框效果滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的装饰边框效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...
- Golang中的RegExp正则表达式用法指南
------------------------------------------------------------ Golang中的正则表达式 ------------------------- ...
- InputStream和OutputStream及相关知识汇总
https://www.jianshu.com/p/e5bc7ea5f948 最近帮学姐写爬虫的时候遇到奇怪的问题,同样的程序在Mac上可以正常运行而在Windows上返回结果错误,最后经排查发现是L ...
- 启动OpenOffice服务
下载安装 安装OpenOffice 4.1.6:下载路径:http://www.openoffice.org/zh-cn/download/ 启动 用以下命令启动OpenOffice服务,注意ip,如 ...
- C语言判断字符串是否是 hex string的代码
把写内容过程中经常用到的一些内容段备份一下,如下内容内容是关于C语言判断字符串是否是 hex string的内容. { static unsigned int hex2bin[256]={0}; me ...
- linux安全加固项目
分享一个Linux加固脚本项目,可快速对服务器进行安全加固,顺便做下备忘,安全人员必须熟悉运维相关的知识! 支持的操作系统平台: Amazon 2013.03 Amazon 2013.09 Amazo ...