HDU 5884 k叉的哈夫曼 O(n)构造
来源:互联网 发布:latex 算法伪代码 编辑:程序博客网 时间:2024/06/11 20:55
Sort
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1832 Accepted Submission(s): 458
Problem Description
Recently, Bob has just learnt a naive sorting algorithm: merge sort. Now, Bob receives a task from Alice.
Alice will give BobN 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 thank 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 thanT cost. So Bob wants to know the smallest k to make the program complete in time.
Alice will give Bob
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 integersN (2≤N≤100000) and T (∑Ni=1ai<T<231) .
In the next line there areN integers a1,a2,a3,...,aN(∀i,0≤ai≤1000) .
For each test case, the first line consists two integers
In the next line there are
Output
For each test cases, output the smallest k .
Sample Input
15 251 2 3 4 5
Sample Output
3
Source
2016 ACM/ICPC Asia Regional Qingdao Online
Recommend
wange2014 | We have carefully selected several similar problems for you: 5932 5931 5930 5928 5925
维护两个队列
#include <map>#include <set>#include <stack>#include <queue>#include <cmath>#include <ctime>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf (-((LL)1<<40))#define lson k<<1, L, (L + R)>>1#define rson k<<1|1, ((L + R)>>1) + 1, R#define mem0(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define mem(a, b) memset(a, b, sizeof(a))#define FIN freopen("in.txt", "r", stdin)#define FOUT freopen("out.txt", "w", stdout)#define rep(i, a, b) for(int i = a; i <= b; i ++)#define dec(i, a, b) for(int i = a; i >= b; i --)typedef long long ll;ll a[100005];int n,m;int work(int x){ queue<ll>q,d; int yy=(n-1)%(x-1); if(yy!=0) { for(int i=0; i<x-1-yy; i++) q.push(0); } for(int i=1; i<=n; i++) q.push(a[i]); ll ans=0; while(!q.empty()|| !d.empty()) { ll sum=0; for(int i=0; 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 0; return 1;}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) scanf("%lld",&a[i]); sort(a+1,a+1+n); int l=2,r=n; while(l<r) { int mid=(l+r)>>1; if(!work(mid)) l=mid+1; else r=mid; } printf("%d\n",l); } return 0;}
0 0
- HDU 5884 k叉的哈夫曼 O(n)构造
- K叉哈夫曼树构造方法 O(N)
- 寻找第K小的数(O(n))
- soj3102 O(n)求第k小的数
- 快排求第K大的数O(n)
- k倍区间 O(N)
- 寻找最小的k个数 时间复杂度 O(n*k)
- HDU 3884(水过O(N ^ 2)的复杂度)
- 求前k个最小的数---类似快排思想的O(n)
- O(N)的时间寻找最大的K个数
- O(N)的时间寻找最大的K个数
- 找第K小的数(O(N))(运用随机思想)
- 在O(n)时间内找到数组中任意第K小的数
- o(n)时间寻找第k小的数
- O(n)时间求最接近中位数的k个数
- O(n)时间求最接近中位数的k个数
- 在O(n)时间内找出最小的k个元素
- O(n)查找第k小(大)的数
- 二分法查找
- halcon学习笔记(8)——QT显示halcon处理的图像和结果
- App Extension 与 App 之间的数据共享
- 如何安装配置SVN
- codeforces 722E
- HDU 5884 k叉的哈夫曼 O(n)构造
- uva 1354 Mobile Computing
- 常用正则表达式汇总
- python2 中文 乱码问题
- 经验教训
- 第七周项目3-负数把正数赶出队列
- Quartus ii中使用testbench文件
- AndroidStudio常用快捷键整理
- 数据结构之拓扑排序