cf#582div3 D——暴力

【题目描述】

The only difference between easy and hard versions is the number of elements in the array.You are given an array a consisting of n integers. In one move you can choose any ai and divide it by 2 rounding down (in other words, in one move you can set ai:=⌊ai2⌋).You can perform such an operation any (possibly, zero) number of times with any ai.Your task is to calculate the minimum possible number of operations required to obtain at least k equal numbers in the array.Don't forget that it is possible to have ai=0 after some operations, thus the answer always exists.InputThe first line of the input contains two integers n and k (1≤k≤n≤50) — the number of elements in the array and the number of equal numbers required.The second line of the input contains n integers a1,a2,…,an (1≤ai≤2⋅105), where ai is the i-th element of a.OutputPrint one integer — the minimum possible number of operations required to obtain at least k equal numbers in the array.ExamplesInput
5 3
1 2 2 4 5
Output
1
Input
5 3
1 2 3 4 5
Output
2
Input
5 3
1 2 3 3 3
Output
0

【题目分析】
敢想不敢做系列,总觉得是一个贪心或者什么的,但是没有想到什么好的策略。暴力吧又不敢写,105的数据暴力,emmm。
在网上查了一下题解,发现还真是一个暴力,只不过这种暴力的思想我还是没有想到,主要是人家用到vector开二维数组,我觉得肯定会爆空间就没想到用二维数组。
用一个二维数组vis[i][j]vis[i][j]vis[i][j]j变成i需要多少步,这样我们找到含有k个以上数字可以变成他的vis[i]然后找需要步数最小的。不明白可以看代码,很暴力的做法。
我把看的题解的代码稍微改动了一下,稍微进行了优化,首先,我将整个数组进行排序,然后按照这个顺序建立的二维数组肯定是有序的,对于每个i,肯定是j越小所用步数越小的在前面,这样就不用每个都进行排序了(竟然排序都能过)。还有就是加上了一个剪枝,如果当前步数已经大于前面找到的答案直接退出。
还是要敢想敢做啊,不要被复杂度蒙蔽了双眼什么都不敢做。就算TLE了也比什么都做不出来强。(当然如果是DP然后还想暴力那就emmm了)
【AC代码】

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<set>
#include<map>
#include<vector>using namespace std;typedef long long ll;
const int INF=0x3f3f3f3f;
const int MAXN=2e5+5;
vector<int> vis[MAXN];
int a[MAXN];
int n,k,ans;int main()
{int t,step,tmp;scanf("%d%d",&n,&k);for(int i=0;i<n;i++){scanf("%d",&a[i]);}sort(a,a+n);for(int i=0;i<n;i++){t=a[i];step=0;vis[t].push_back(step++);while(t){t>>=1;//先除2,这样可以将0也计算进去vis[t].push_back(step++);}}ans=INT_MAX;for(int i=0;i<MAXN;i++){if(vis[i].size()<k) continue;tmp=0;for(int j=0;j<k;j++){tmp+=vis[i][j];if(tmp>ans) break;}if(tmp<ans) ans=tmp;}printf("%d",ans);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/384023.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C语言 可变参数

http://www.cnblogs.com/zhanggaofeng/p/6434554.html //可变参数 #include <stdio.h> #include <stdlib.h> #include <string.h> //引用头文件 #include <stdarg.h>/* va_list用于声明一个变量&#xff0c;我们知道函数的可变参数列表其实就是一个字符…

UVa1585

【题目描述】 传送门 【题目分析】 氵 【AC代码】 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #include<cstdlib> #include<set> #include<map> #include<vector>u…

c语言经典算法——查找一个整数数组中第二大数

https://www.cnblogs.com/dootoo/p/4473958.html 题目&#xff1a; 实现一个函数&#xff0c;查找一个整数数组中第二大数。 算法思想&#xff1a; 设置两个变量max1和max2&#xff0c;用来保存最大数和第二大数&#xff0c;然后将数组剩余的数依次与这两个数比较&#xff0c;如…

进程间关系和守护进程

一. 进程组/作业/会话 1.进程组 每一个进程除了有一个进程ID之外, 还属于一个进程组. 进程是一个或多个进程的集合. 通常, 它们与同一个作业向关联, 可以接收来自同一个终端下的各种命令,信号. 每一个进程组都有唯一的进程组 ID. 每一个进程组都可以有一个组长进程. 组长进程的…

UVa1586

【题目描述】 传送门 【题目分析】 氵 【AC代码】 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #include<cstdlib> #include<set> #include<map> #include<vector> …

猴子偷桃问题

http://blog.csdn.net/snow_5288/article/details/52561882 问题描述&#xff1a; /*有一群猴子&#xff0c;去摘了一堆桃子*/ /*商量之后决定每天吃剩余桃子的一半*/ /*当每天大家吃完桃子之后&#xff0c;有个贪心的小猴都会偷偷再吃一个桃子*/ /*按照这样的方式猴子们每天都…

UVa1225

【题目描述】 传送门 【题目分析】 做题做多了慢慢都忘记暴力了&#xff0c;想要快速算出来&#xff0c;找到规律&#xff0c;但是找来找去好复杂的都没有找到&#xff0c;然后写了一个不能再暴力的写法&#xff0c;就过了。。。 我还是觉得如果数据范围变成1e9那种级别的还…

linux 线程学习之条件变量

http://blog.csdn.net/hemmanhui/article/details/4417433 互斥锁&#xff1a;用来上锁。 条件变量&#xff1a;用来等待&#xff0c;当条件变量用来自动阻塞一个线程&#xff0c;直到某特殊情况发生为止。通常条件变量和互斥锁同时使用。 函数介绍&#xff1a; 1&#xff0e;…

UVa455

【题目描述】 传送门 【题目分析】 就是一个简单的暴力&#xff0c;只是需要注意输出格式比较毒瘤。 【AC代码】 #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<cmath> #i…

网络相关基础概念

一. 相关基础概念 1.计算机网络的特点 (1)连通性:计算机网络使得上网的用户都能够彼此相连, 好像用户的计算机可以直接相连     (2)资源共享:资源共享可以是信息共享, 软件共享, 硬件共享等等. 由于网络的存在, 使得用户感觉资源就在自己身边 2. 网络 网络是由若干结点和…

linux线程同步(2)-条件变量

https://www.cnblogs.com/yuuyuu/p/5140875.html linux线程同步(2)-条件变量 一.概述 上一篇&#xff0c;介绍了互斥量。条件变量与互斥量不同&#xff0c;互斥量是防止多线程同时访问共享的互斥变量来保护临界区。条件变量…

UVa227

【题目描述】 传送门 【题目分析】 题目的意思很简单&#xff0c;只是输入输出很毒瘤&#xff0c;我一开始用的fgets然后用scanf(" ")吃掉所有的空格和换行&#xff0c;可是这样有可能将迷宫的空格吃掉&#xff08;例如这个空格恰好在第一行第一列&#xff09;。 …

点对点数据链路层

数据链路层的主要功能将数据转换为相应的比特流使用的信道主要有点对点的信道方式(一对一的方式), 以及广播的信道方式 一. 点对点信道的数据链路层 1. 数据链路和数据帧 链路就是从一个结点连接到相邻结点的一段物理线路(有线或者无线), 期间不准有任何的交换结点, 因此两台…

UVa232

[题目描述] 传送门 [题目分析] 简单的模拟,注意细节 [AC代码] #include<cstdio> #include<cstring> #include<algorithm> #include<climits> #include<cctype> #include<queue> #include<set>using namespace std;typedef long…

linux线程同步(1)-互斥量

http://www.cnblogs.com/yuuyuu/p/5140251.html 一.概述 互斥量是线程同步的一种机制&#xff0c;用来保护多线程的共享资源。同一时刻&#xff0c;只允许一个线程对临界区进行访问。 互斥量的工作流程&#xff1a;创建一个…

UVa1368

[题目描述] 传送门 [题目分析] 乍一看好像有点复杂,稍微思考一下只需要找到每个位置中最多的碱基.如果相等的话优先输出字典序小的. [AC代码] #include<cstdio> #include<cstring> #include<algorithm> #include<climits> #include<cctype>…

linux线程同步(3)-读写锁

http://www.cnblogs.com/yuuyuu/p/5143881.html 一.概述 读写锁与互斥量的功能类似&#xff0c;对临界区的共享资源进行保护&#xff01;互斥量一次只让一个线程进入临界区&#xff0c;读写锁比它有更高的并行性。读写锁有…

树的相关笔试面试题

1. 树的创建 已知一个先序遍历数的结果用数组表示, 其中空节点用 null_node 表示, 要求创建出这棵树. 同样采用递归的思想, 先定义一个指针, 指向数组中的第一个元素, 然后给数组的第一个结点创建相应的结点, 然后指针后移, 递归创建根节点的左子树, 递归创建根节点的右子树, …

UVa202

[题目描述] 传送门 [题目分析] 就是一个模拟,不过稍微有点小复杂,而且输出格式有点小毒瘤. 不过只是RE了两发,PE了一发就过了,还是很开心. 需要注意数组要开很大,可能循环节出现在很后. 每个输出样例应该输出一个空行,最后面也应该有,不然会PE [AC代码] #include<cst…

linux线程同步(5)-屏障

http://www.cnblogs.com/yuuyuu/p/5152560.html 一.概述 barrier(屏障)与互斥量&#xff0c;读写锁&#xff0c;自旋锁不同&#xff0c;它不是用来保护临界区的。相反&#xff0c;它跟条件变量一样&#xff0c;是用来协同多…