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 ...
随机推荐
- day24——面向对象三大特性、鸭子类型、类的约束、super的深度解析
day24 面向对象的三大特性 继承.封装.多态 封装:把一堆东西(代码,数据)放到一个地方(空间),并且可以使用 class Student: def __init__(self, name, se ...
- nginx静态文件不设置缓存
找到nginx安装目录下的nginx.conf文件,再nginx里面添加如下的内容 location / { add_header Cache-Control no-cache; add_header ...
- Kafka Replication: The case for MirrorMaker 2.0
Apache Kafka has become an essential component of enterprise data pipelines and is used for tracking ...
- repodata创建本地YUM仓库
参考一createrepo是linux下的创建仓库的软件包.create是创建的意思,repo是repository的缩写,是仓库的意思. yum(Yellow dog Updater,Modifie ...
- php精度比较函数bccomp
bccomp (PHP 4, PHP 5, PHP 7) bccomp — 比较两个任意精度的数字 说明 int bccomp ( string $left_operand , string $rig ...
- Laravel入门及实践,快速上手ThinkSNS+二次开发
温馨提示: l 本文纯干货,文字和代码居多,且适合零基础Laravel学习者: l 本文会新建一个名为 blog 的 Laravel 程序,这是一个非常简单的博客. l 欢迎随时关注ThinkSNS ...
- Assignment 2: UDP Pinger[课后作业]
Computer Networking : A Top-Down Approach 的课后作业. 要求: 基于UDP协议,实现一个Pinger工具. 服务端代码已经提供了,自己实现客户端的代码. 完整 ...
- 纯css更改图片颜色的技巧
tips: JPG.PNG.GIF 都可以,但是有一个前提要求,就是黑色纯色,背景白色 .pic1 { background-image: url($img), linear-gradient ...
- Flask 进阶
OOP 面向对象反射 # __call__方法 # class Foo(object): # def __call__(self, *args, **kwargs): # return "i ...
- 多代理切换 MultiProxy
配置代理选项 添加代理列表 ie配置代理 开始使用