Sort

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description
Recently, Bob has just learnt a naive sorting algorithm: merge sort. Now, Bob receives a task from Alice.
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.
 
Input
The first line of input contains an integer t0, the number of test cases. t0 test cases follow.
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).
 
Output
For each test cases, output the smallest k.
 
Sample Input
1
5 25
1 2 3 4 5
 
Sample Output
3
 
Source
 

由于多叉哈夫曼最后可能不能得到k个再合并成一个,可以先将多的部分取余,或者加0;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
const int N=1e5+,M=1e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
ll n,m;
ll a[N];
int check(int x)
{
queue<ll>q;
queue<ll>d;
int yy=(n-)%(x-);
if(yy!=)
{
for(int i=;i<x--yy;i++)
q.push();
}
for(ll i=;i<=n;i++)
q.push(a[i]);
ll ans=;
while(!q.empty()||!d.empty())
{
ll sum=;
for(int i=;i<x;i++)
{
if(q.empty()&&d.empty())
break;
if(q.empty())
{
sum+=d.front();
d.pop();
}
else if(d.empty())
{
sum+=q.front();
q.pop();
}
else
{
int u=q.front();
int v=d.front();
if(u<v)
{
sum+=u;
q.pop();
}
else
{
sum+=v;
d.pop();
}
}
}
ans+=sum;
if(q.empty()&&d.empty())
break;
d.push(sum);
}
if(ans>m)
return ;
return ;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld",&n,&m);
for(ll i=;i<=n;i++)
scanf("%lld",&a[i]);
sort(a+,a+n+);
int st=,en=n;
while(st<en)
{
int mid=(st+en)/;
if(check(mid))
en=mid;
else
st=mid+;
}
printf("%d\n",st);
}
return ;
}

最新文章

  1. 69个经典Spring面试题和答案
  2. ElasticSearch-5.0安装head插件
  3. SystemClock.sleep和Thread.sleep的区别(转)
  4. 总结一下工作中遇到的NPOI以及在ASP.NET MVC中的使用
  5. PHP函数补完 - var_export
  6. Struts2框架的运行流程
  7. UVA11624(bfs)
  8. c读取文本文档
  9. idea14使用maven创建web工程
  10. Using Run-Time Dynamic Linking(使用运行时动态链接库)
  11. TOYS - POJ 2318(计算几何,叉积判断)
  12. vc++深入跟踪MFC程序的执行流程
  13. PostScript学习:另一种缩写为PS的技术
  14. 【SEO】搜索引擎优化的陷阱和作弊
  15. 99%的人理解错 HTTP 中 GET 与 POST 的区别
  16. Django细节小记
  17. Java 并发集合的实现原理
  18. http笔记汇总
  19. Docker 简单查看name和ip
  20. BZOJ 4827 循环卷积

热门文章

  1. [Spring Data MongoDB]学习笔记--注册一个Mongo实例
  2. Oracle 逻辑体系
  3. 博文分类索引--Python
  4. Android系统移植与调试之------->如何使用PhotoShop转换24位的bmp图片为16位bmp图片
  5. Js用户引导插件bootstrap-tour
  6. PyNN:神经网络模拟器的通用接口
  7. ACM解题之回文序列
  8. Python 2 一些实用模块的使用
  9. Loadrunner脚本篇——从文件中读取内容并参数化
  10. Python学习进程(1)Python简介