【HDU - 5884】Sort(k叉哈夫曼树,优化tricks,两个队列代替优先队列)

题干:

Recently, Bob has just learnt a naive sorting algorithm: merge sort. Now, Bob receives a task from Alice. 
Alice will give Bob NN sorted sequences, and the ii-th sequence includes aiai elements. Bob need to merge all of these sequences. He can write a program, which can merge no more than kk sequences in one time. The cost of a merging operation is the sum of the length of these sequences. Unfortunately, Alice allows this program to use no more than TT cost. So Bob wants to know the smallest kk to make the program complete in time.

Input

The first line of input contains an integer t0t0, the number of test cases. t0t0 test cases follow. 
For each test case, the first line consists two integers N (2≤N≤100000)N (2≤N≤100000) and T (∑Ni=1ai<T<231)T (∑i=1Nai<T<231). 
In the next line there are NN integers a1,a2,a3,...,aN(∀i,0≤ai≤1000)a1,a2,a3,...,aN(∀i,0≤ai≤1000).

Output

For each test cases, output the smallest kk.

Sample Input

1
5 25
1 2 3 4 5

Sample Output

3

题目大意:

有n个数,每个数有个值,现在你可以选择每次K个数合并,合并的消耗为这K个数的权值和,问在合并为只有1个数的时候,总消耗不超过T的情况下,最小的K是多少

解题报告:

首先能想到的做法,二分一个K,然后check的时候按照标准k叉哈夫曼树的做法,搞个优先队列,别忘补0(通过n%(k-1)==1这个判断条件不断n++),但是这个做法是nlognlogn的。现在考虑优化:

首先如果是2叉哈夫曼树:

可以设两个数组,一个存放排序后的原数据,一个存放每次相加后的值。对于取之也有几种分析:

1.全部来源于a,此时q为空或者q的队头的元素比a的前两个元素都小.

2.全部来源于b,此时a为空或者a的队头的元素比b的前两个元素都小.

3.a和b中各取一个.

对于k叉:

就一个一个的取,一共取k次,每次取的时候就在两个数组中选一个小的就行了

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<vector>
#define mod (1000000007)
using namespace std;
typedef long long ll;
int a[200010];
int c[200010];
int b[200010];
int T;
int cal(int n,int k) {int i=1;int b1=0,cnt=0;int x=0;int ans=0;int add=0,y=k;if(k!=2) {while((n+add)%(k-1)!=1) add++;}for(int j=n; j>=1; j--) c[j+add]=a[j];for(int j=add; j>=1; j--) c[j]=0;n+=add;while(true) {int x=0;int ad=0;while((i<=n||b1<cnt)&&x<k) {if(i>n) {ad+=b[b1];ans+=b[b1];b1++;} else if(b1>=cnt) {ad+=c[i];ans+=c[i];i++;} else {if(c[i]<b[b1]) {ad+=c[i];ans+=c[i];i++;} else {ad+=b[b1];ans+=b[b1];b1++;}}x++;}if(i>n&&b1>=cnt) {break;}b[cnt++]=ad;}return ans;
}
int main() {int t;cin>>t;while(t--) {int n;scanf("%d%d",&n,&T);for(int i=1; i<=n; i++) scanf("%d",&a[i]);sort(a+1,a+1+n);int l=2,r=n;int ans;while(l<=r) {int mid=(l+r)/2;if(cal(n,mid)<=T) r=mid-1, ans=mid;else l=mid+1;}printf("%d\n",ans);}return 0;
}

 

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

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

相关文章

金额输入框校验和自动校正、支持指定任意位数小数decimal、支持只能输入整数、支持是否允许输入负数等功能

应用场景&#xff1a;开发前端交互页面时&#xff0c;经常遇到金额输入框、指定小数位数的数字输入框&#xff0c;单一的正则表达式无法满足大部分的业务校验需求&#xff0c;下面总结一个实用巧妙而又灵活的把普通输入框变成自动校正输入框的解决方案&#xff1a; 数字&#…

网络编程懒人入门(七):深入浅出,全面理解HTTP协议

转自即时通讯网&#xff1a;http://www.52im.net/ 本文引用了自简书作者“涤生_Woo”的文章&#xff0c;内容有删减&#xff0c;感谢原作者的分享。 1、前言 HTTP&#xff08;全称超文本传输协议&#xff0c;英文全称HyperText Transfer Protocol&#xff09;是互联网上应用…

【HDU - 5889】Barricade(最短路+网络流,最小割)

题干&#xff1a; The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as NN towns and MM roads, and each road has the same length and connects two towns. The town numbered 11 is where generals cast…

SM4国密对称算法源码解析

最近在研究国密算法&#xff0c;主要分为&#xff1a;SM2、SM3、SM4。其中SM2为非对称加密算法&#xff0c;SM3为哈希摘要算法&#xff0c;SM4为对称加密算法。 1.在SM4算法源文件中主要有以下几个函数&#xff1a; void sm4_setkey_enc( sm4_context *ctx, unsigned char ke…

Mac系统容易忽视但很实用的命令整理

001.终端指定用哪个软件打开指定的文件 # open -a 实用哪个软件(软件名有空格需转义) 文件名路径 open -a /Applications/Google\ Chrome.app xx.html # 【tips:】由于先输入"open -a"后tab键自动补全容易卡死终端&#xff0c;所以推荐先输入"/App..."&a…

【HDU 4394】Digital Square(bfs,数位搜索,思维,数学)

题干&#xff1a; Given an integer N,you should come up with the minimum nonnegative integer M.M meets the follow condition: M 2%10 xN (x0,1,2,3....) Input The first line has an integer T( T< 1000), the number of test cases. For each case, each line…

SM4算法原理

前面的文章介绍了SM4算法的C语言实现&#xff0c;源码可见文章&#xff1a;SM4国密对称算法源码解析_10点43的博客-CSDN博客_sm4代码。 本文将会介绍SM4算法原理&#xff0c;这部分可能会比较枯燥&#xff0c;但数学要求也不是太高。 目录 1.概述 2. 参数产生 3. 轮函数 4…

webpack打包前端项目入门

前言&#xff1a;在开发过程中&#xff0c;利用webpack可以帮我们自动把ES6语法编译成低版本浏览器能解析的JavaScript代码。下面给出webpack打包前端项目入门案例。 [终端&#xff1a;进入项目所在目录] 1.初始化项目依赖&#xff1a; npm init -y 2.安装webpack-cli脚手架…

网络编程懒人入门(八):手把手教你写基于TCP的Socket长连接

转自即时通讯网&#xff1a;http://www.52im.net/ 本文原作者&#xff1a;“水晶虾饺”&#xff0c;原文由“玉刚说”写作平台提供写作赞助&#xff0c;原文版权归“玉刚说”微信公众号所有&#xff0c;即时通讯网收录时有改动。 1、引言 好多小白初次接触即时通讯&#xff…

【HihoCoder - 1502】最大子矩阵(二维前缀和,尺取)

题干&#xff1a; 给定一个NxM的矩阵A和一个整数K&#xff0c;小Hi希望你能求出其中最大&#xff08;元素数目最多&#xff09;的子矩阵&#xff0c;并且该子矩阵中所有元素的和不超过K。 Input 第一行包含三个整数N、M和K。 以下N行每行包含M个整数&#xff0c;表示A。 对…

Idea Spring Boot配置文件.yaml或.properties不能自动提示的有效解决办法

SpringBoot项目的配置文件.yaml/.yml/.properties文件编写的时候没有自动提示&#xff0c;网上的解决办法五花八门&#xff0c;不一定适合具体个人的IDE环境&#xff0c;下面总结一套能解决绝大部分情况的方案&#xff1a; 先给出能自动识别的图样&#xff1a; 步骤1&#xff…

[通俗易懂]深入理解TCP协议(上):理论基础

转自即时通讯网&#xff1a;http://www.52im.net/ 前言 TCP是一个巨复杂的协议&#xff0c;因为他要解决很多问题&#xff0c;而这些问题又带出了很多子问题和阴暗面。所以学习TCP本身是个比较痛苦的过程&#xff0c;但对于学习的过程却能让人有很多收获。关于TCP这个协议的细…

【POJ - 3616】Milking Time (贪心+dp)

题干&#xff1a; Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤ 1,000,000) hours (conveniently labeled 0..N-1) so that she produces as much milk as possible. …

Thymeleaf模板引擎处理日期输入框回显问题type=“date“类型的坑 和 单选按钮、复选框的回显

type"date"类型的日期输入框的默认格式为"yyyy/MM/dd"&#xff0c;但是如果使用Thymeleaf的日期格式化工具类的时候使用"yyyy/MM/dd"就无法回显数据&#xff0c;必需使用类似于"yyyy-MM-dd"这种格式才能回显。 <!--emp.birth为Dat…

SM3密码杂凑算法源码解析

1.在SM3算法源文件中主要有以下几个函数&#xff1a; void sm3_starts( sm3_context *ctx ); void sm3_update( sm3_context *ctx, unsigned char *input, int ilen ); void sm3_finish( sm3_context *ctx, unsigned char output[32] ); void sm3( unsigned char *input, int …

【HDU - 5418】Victor and World(tsp旅行商问题,状压dp,floyd最短路,图论)

题干&#xff1a; After trying hard for many years, Victor has finally received a pilot license. To have a celebration, he intends to buy himself an airplane and fly around the world. There are nn countries on the earth, which are numbered from 11 to nn. T…

Anaconda中软件库更新

今天在Anaconda运行Visualization of MLP weights on MNIST源码时出现了如图错误&#xff1a; 提示无法导入fetch_openml&#xff0c;查了一下是对应的sklearn软件包版本过低&#xff0c;为0.17版。需要更新到0.20版。 1.打开Anaconda Prompt命令行 输入 conda list 命令 查看…

Mac安装mysql8.x最简洁的步骤,避免采坑

1.下载mysql8的.dmg安装包&#xff08;官网下载需要Oracle账号&#xff0c;推荐网上搜索一个8系列的版本即可&#xff09; 2.双击.dmg安装包&#xff0c;不断点击下一步。但是需要注意以下两点&#xff1a; &#xff08;1&#xff09;密码认证方式都选第二个&#xff08;不是…

【HDU - 4784】Dinner Coming Soon(记忆化搜索bfs,dp)

题干&#xff1a; Coach Pang loves his boyfriend Uncle Yang very much. Today is Uncle Yang’s birthday, Coach Pang wants to have a romantic candlelit dinner at Uncle Yang’s house and he has to arrive there in T minutes.   There are N houses in their cit…

Linux操作系统CentOS7安装

最近在学习Linux&#xff0c;今天记录下如何安装CentOS7操作系统。 1. 下载虚拟机软件 虚拟机选择的是VMware Workstation软件&#xff0c;可以访问这个链接下载&#xff1a;https://coding.net/u/aminglinux/p/resource/git/blob/master/README.md 2. 安装虚拟机 按照提示&…