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 ...
随机推荐
- 图-搜索-BFS-DFS-126. 单词接龙 II
2020-03-19 13:10:35 问题描述: 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序 ...
- 干货|漫画算法:LRU从实现到应用层层剖析(第一讲)
今天为大家分享很出名的LRU算法,第一讲共包括4节. LRU概述 LRU使用 LRU实现 Redis近LRU概述 第一部分:LRU概述 LRU是Least Recently Used的缩写,译为最近最 ...
- java面试基础篇-List
一.ArrayList: 底层为数组实现,线程不安全,查询,修改快,增加删除慢, 数据结构:数组以0为下标依次连续进行存储 数组查询元素:根据下标查询就行 数组增加元素:如果需要给index为10的位 ...
- Ubuntu查看文件格式(后缀名)
在文件目录执行: $ file filename #filename表示要查看的文件名
- [noip模拟]小猫爬山<迭代深搜>
[题目描述]: Freda和rainbow饲养了N只小猫,这天,小猫们要去爬山.经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了(呜咕>_<). Freda和rai ...
- Vue点击当前元素添加class 去掉兄弟的class
<div id="app"> <ul> <li v-for="(todo, index) in todos" v-on:click ...
- PTA数据结构与算法题目集(中文) 7-25
PTA数据结构与算法题目集(中文) 7-25 7-25 朋友圈 (25 分) 某学校有N个学生,形成M个俱乐部.每个俱乐部里的学生有着一定相似的兴趣爱好,形成一个朋友圈.一个学生可以同时属于若干 ...
- ceph概述
ceph概述 基础知识 什么是分布式文件系统 • ...
- Shell:Day03笔记
编程原理:1.编程结束 驱动 硬件默认是不能使用的 CPU控制硬件 不同的厂家硬件设备之间需要进行指令沟通,就需要驱动程序来进行“翻译” 编程语言的分类: 高级语言.超高级语言需要翻 ...
- spring官方为什么放弃spring social项目及替代方案
spring social 1.6之后官方不在维护该项目, spring boot 2.x之后也不在提供spring social的 Autoconfiguration. 原因: https://sp ...