codeforces Equalizing by Division (easy version)
standard output
The only difference between easy and hard versions is the number of elements in the array.
You are given an array aa consisting of nn integers. In one move you can choose any aiai and divide it by 22 rounding down (in other words, in one move you can set ai:=⌊ai2⌋ai:=⌊ai2⌋).
You can perform such an operation any (possibly, zero) number of times with any aiai.
Your task is to calculate the minimum possible number of operations required to obtain at least kk equal numbers in the array.
Don't forget that it is possible to have ai=0ai=0 after some operations, thus the answer always exists.
The first line of the input contains two integers nn and kk (1≤k≤n≤501≤k≤n≤50) — the number of elements in the array and the number of equal numbers required.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅1051≤ai≤2⋅105), where aiai is the ii-th element of aa.
Print one integer — the minimum possible number of operations required to obtain at least kk equal numbers in the array.
5 3
1 2 2 4 5
1
5 3
1 2 3 4 5
2
5 3
1 2 3 3 3
0
题解:开一个数组cnt,保存某一个数出现的次数。然后循环除2,同时再开一个数组num,用来保存当初的数字,需要除多少次2才能变成当前的值。同时判断当前数字出现的次数,
#include<bits/stdc++.h>
using namespace std;
const int N=2E5+;
const int inf=1e9+;
int arr[N];
int num[N];
int cnt[N];
int main(){
int n,k;
cin>>n>>k;
for(int i=;i<=n;i++) scanf("%d",&arr[i]);
sort(arr+,arr++n);
int ans=inf;
for(int i=;i<=n;i++){
int temp=;
int x=arr[i];
while(x){
cnt[x]++;//判断arr[i]出现的次数;
num[x]+=temp;//判断arr[i]编程现在的arr[i]需要操作几次;
if(cnt[x]==k) {
ans=min(ans,num[x]);
}
temp++;
x/=;
}
}
printf("%d",ans);
return ;
}
codeforces Equalizing by Division (easy version)的更多相关文章
- D2. Equalizing by Division (hard version)
D2. Equalizing by Division (hard version) 涉及下标运算一定要注意下标是否越界!!! 思路,暴力判断以每个数字为到达态最小花费 #include<bits ...
- Codeforces 1118F1 Tree Cutting (Easy Version) (简单树形DP)
<题目链接> 题目大意: 给定一棵树,树上的点有0,1,2三中情况,0代表该点无色.现在需要你将这棵树割掉一些边,使得割掉每条边分割成的两部分均最多只含有一种颜色的点,即分割后的两部分不能 ...
- Codeforces 1296E1 - String Coloring (easy version)
题目大意: 给定一段长度为n的字符串s 你需要给每个字符进行涂色,然后相邻的不同色的字符可以进行交换 需要保证涂色后能通过相邻交换把这个字符串按照字典序排序(a~z) 你只有两种颜色可以用来涂 问是否 ...
- Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】
任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...
- Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】
任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...
- Codeforces Round #521 (Div. 3) F1. Pictures with Kittens (easy version)
F1. Pictures with Kittens (easy version) 题目链接:https://codeforces.com/contest/1077/problem/F1 题意: 给出n ...
- Codeforces 1077F1 Pictures with Kittens (easy version)(DP)
题目链接:Pictures with Kittens (easy version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:$dp[i][j ...
- Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version) 水题
B1. Character Swap (Easy Version) This problem is different from the hard version. In this version U ...
- Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)
D1. RGB Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inp ...
随机推荐
- 题解 SP2916 【GSS5 - Can you answer these queries V】
前言 最近沉迷于数据结构,感觉数据结构很有意思. 正文 分析 先来分类讨论一下 1. \(x2<y1\) 如果 \(y1<x2\) 的话,答案 \(=\max \limits_{ y1 \ ...
- Hive内外表的区分方法及内外部差异
Hive内外部区分方法 查看hive元数据:进入mysql中hive元数据库,查看TBLS表,查看对应的表名和表类型: 在hive-cli界面:desc extended tablename,查看Ta ...
- JavaScript的URLSearchParams方法
URLSearchParams 接口定义了一些实用的方法来处理 URL 的查询字符串. 一个实现了 URLSearchParams 的对象可以直接用在 for…of 结构中,不需要使用 entries ...
- mybatis 入门基础
一.Mybatis介绍 MyBatis是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架.MyBatis几乎消除了所有的JDBC代码,也基本不需要手工去设置参数和获取检索结果.MyBatis ...
- 深入理解NIO(三)—— NIO原理及部分源码的解析
深入理解NIO(三)—— NIO原理及部分源码的解析 欢迎回到淦™的源码看爆系列 在看完前面两个系列之后,相信大家对NIO也有了一定的理解,接下来我们就来深入源码去解读它,我这里的是OpenJDK-8 ...
- ubuntu查看并杀死自己之前运行的进程解决办法RuntimeError: CUDA error: out of memory
问题描述:在跑深度学习算法的时候,发现服务器上只有自己在使用GPU,但使用GPU总是会报RuntimeError: CUDA error: out of memory,这是因为自己之前运行的进程还存在 ...
- 模块 heapq_堆排序
_heapq_堆排序 该模块提供了堆排序算法的实现.堆是二叉树,最大堆中父节点大于或等于两个子节点,最小堆父节点小于或等于两个子节点. 创建堆 heapq有两种方式创建堆, 一种是使用一个空列表,然后 ...
- 一位萌新Google冲浪的开始
这一切的开始可能都来源于对 百度 各方面的不满吧(确实不咋滴) 于是开始对Google感冒,上必应https://cn.bing.com/去搜了下“国内如何上Google”,上面也是众说纷纭,莫衷一是 ...
- phpwind 安装下一步空白解决方案
系统版本 centos 翻阅网上大部分都是php版本问题,让降级就行了,试了之后根本不行 其实再安装一个插件即可成功 如下: yum install -y php-mysql
- Codeforces Round #631 (Div. 2) D. Dreamoon Likes Sequences (bitmasks +dp )
https://codeforces.com/contest/1330/problem/D 题目大意:给出一个限制 d 与模数 m ,求出可以构造出的满足条件的数组 a 的个数,需要满足以下条件: ...