2016 年青岛网络赛---Sort(k叉哈夫曼)
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=5884
Alice will give Bob N sorted sequences, and the i-th sequence includes ai elements. Bob need to merge all of these sequences. He can write a program, which can merge no more than k 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 T cost. So Bob wants to know the smallest k to make the program complete in time.
For each test case, the first line consists two integers N (2≤N≤100000) and T (∑Ni=1ai<T<231).
In the next line there are N integers a1,a2,a3,...,aN(∀i,0≤ai≤1000).
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <cmath>
#include <string.h>
using namespace std;
int N;
long long T;
long long a[]; int calc(int k)
{
queue<long long>q;
int pos=;
long long sum=;
if((N-)%(k-)!=&&N>k) ///如果不能k个k个合并到底,则先合并筹不足k个的;
{
pos=(N-)%(k-)+;
for(int i=;i<pos;i++) sum+=a[i];
q.push(sum);
}
while()
{
long long sum2=;
for(int i=; i<k; i++)
{
if(!q.empty())
{
if(pos<N&&q.front()>a[pos])
{
sum2+=a[pos];
sum+=a[pos];
pos++;
}
else
{
sum2+=q.front();
sum+=q.front();
q.pop();
}
}
else if(pos<N)
{
sum2+=a[pos];
sum+=a[pos];
pos++;
}
else goto endw;
}
if(sum>T) return ;
if(pos<N||!q.empty())
q.push(sum2);
}
endw:;
if(sum<=T) return ;
else return ;
} int main()
{
int to;
scanf("%d",&to);
while(to--)
{
scanf("%d%lld",&N,&T);
for(int i=; i<N; i++)
scanf("%lld",&a[i]);
sort(a,a+N);
int l=,r=N,mid;
while(l<=r)
{
mid=(l+r)>>;
int f=calc(mid);
if(f==) l=mid+;
else r=mid-;
}
printf("%d\n",l);
}
return ;
}
2016 年青岛网络赛---Sort(k叉哈夫曼)的更多相关文章
- hdu5884 Sort(二分+k叉哈夫曼树)
题目链接:hdu5884 Sort 题意:n个有序序列的归并排序.每次可以选择不超过k个序列进行合并,合并代价为这些序列的长度和.总的合并代价不能超过T, 问k最小是多少. 题解:先二分k,然后在k给 ...
- 两个队列+k叉哈夫曼树 HDU 5884
// 两个队列+k叉哈夫曼树 HDU 5884 // camp题解: // 题意:nn个有序序列的归并排序.每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的合并代价不能超过TT, ...
- 【CF884D】Boxes And Balls k叉哈夫曼树
题目大意:给定一个大小为 N 的集合,每次可以从中挑出 2 个或 3 个数进行合并,合并的代价是几个数的权值和,求将这些数合并成 1 个的最小代价是多少. 引理:K 叉哈夫曼树需要保证 \((n-1) ...
- UOJ#130 【NOI2015】荷马史诗 K叉哈夫曼树
[NOI2015]荷马史诗 链接:http://uoj.ac/problem/130 因为不能有前缀关系,所以单词均为叶子节点,就是K叉哈夫曼树.第一问直接求解,第二问即第二关键字为树的高度. #in ...
- AcWing:149. 荷马史诗(哈夫曼编码 + k叉哈夫曼树)
追逐影子的人,自己就是影子. ——荷马 达达最近迷上了文学. 她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>. 但是由<奥德赛>和<伊 ...
- HDU 5884 Sort (二分+k叉哈夫曼树)
题意:n 个有序序列的归并排序.每次可以选择不超过 k 个序列进行合并,合并代价为这些序列的长度和.总的合并代价不能超过T, 问 k最小是多少. 析:首先二分一下这个 k .然后在给定 k 的情况下, ...
- BZOJ 4198: [Noi2015]荷马史诗 哈夫曼树 k叉哈夫曼树
https://www.lydsy.com/JudgeOnline/problem.php?id=4198 https://blog.csdn.net/chn_jz/article/details/7 ...
- bzoj 4198 [ Noi 2015 ] 荷马史诗 —— 哈夫曼编码(k叉哈夫曼树)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4198 第一次写哈夫曼树!看了很多博客. 哈夫曼树 & 哈夫曼编码:https://w ...
- P2168 [NOI2015]荷马史诗 k叉哈夫曼树
思路:哈夫曼编码 提交:1次(参考题解) 题解:类似合并果子$QwQ$ 取出前$k$小(注意如果叶子结点不满的话要补全),合并起来再扔回堆里去. #include<cstdio> #inc ...
随机推荐
- atitit 业务 触发器原理. 与事件原理 docx
atitit 业务 触发器原理. 与事件原理 docx 1.1. 呵呵,你需要需要一个业务 触发器..1 1.2. 触发器/事件/中断的原理1 1.3. Io 硬件中断的原理( 中断的低层有cpu轮询 ...
- Atitit.会员卡(包括银行卡)api的设计
Atitit.会员卡(包括银行卡)api的设计 1. 银行卡的本质是一种商业机构会员卡1 2. 会员卡号结构组成1 2.1. ●前六位是:发行者标识代码 Issuer Identification N ...
- SSH框架详解
1.什么是ssh? SSH对应 struts spring hibernate struts 采用MVC模式,主要是作用于用户交互 spring 采用IOC和AOP~作用比较抽象,是用于项目的松耦合 ...
- js的基本语句和语法
字符串赋值:把字符串用双引号或单引号引起来,在(js.php)中:二.类型转换;parseint():转整数.parsefloat转小数:强制转换三.运算符表达式1数序运算:加减乘除 %取余:2逻 ...
- iOS开发——高级技术OC篇&运行时(Runtime)机制
运行时(Runtime)机制 本文将会以笔者个人的小小研究为例总结一下关于iOS开发中运行时的使用和常用方法的介绍,关于跟多运行时相关技术请查看笔者之前写的运行时高级用法及相关语法或者查看响应官方文档 ...
- java webservice 总结(学会读别人的webservice并且通过代理模式访问)
公司做的系统之间的交互用到了webservice做交互,现在对webservice做一个总结. 1.配置已有的webservice webservice主要包括 xml/json:作为传输数据的格式 ...
- 浅谈attr()和prop()
刚开始学JQ的时候 ,看到attr()和prop()这两个属性的时候感觉很迷茫,而且配合官方给出的推荐使用图: prop()可以做到的attr()完全都可以,而且做不到的attr()也可以做到.何用? ...
- C#委托(delegate)
C#中委托(delegate)是一种安全地封装方法的类型,委托是面向对象的.类型安全的. 使用委托的步骤: 1.声明委托 public delegate void DelegateHandler(st ...
- Oracle 11g系列:数据库
1.创建Oracle数据库 创建Oracle数据库的最常用工具为Database Configuration Assistant(数据库配置助手),依次选择[开始]|[所有程序]|[Oracle-Or ...
- 了解HTML表单之input元素的30个元素属性
目录 传统属性 name type accept alt checked disabled readonly maxlength size src value 新增属性 autocomplete au ...