Sort

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2377    Accepted Submission(s): 610

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
题目链接:HDU 5884

这题容易想到二分答案K来做,但是仅仅是这样用手写的堆的也是超时的,加了输入外挂也没卡过去(T_T),显然这样的复杂度是NLogN * LogN ,数据一大就T了

然后膜了一下别人的方法:由于每一次拿k-1个数和1个数合并,因此会剩下(n-1)%(k-1)个数,因此用(k-1)-(n-1)%(k-1)个0与(n-1)%(k-1)个剩下的数凑成k-1个数,这样一来最后就一定一次性完全合并,因此先向qa输入(k-1)-(n-1)%(k-1)个0,再把先把原数组排序再放到qa中,qa用来保存未经过合并的数字,qb用来维护合并过程中的数字,这样K叉哈夫曼树的WPL计算方法就成了每一次从这两个队列中取出队头最小的一个元素,取K个,这样就是一次合并,那么合并的复杂度就成了O(N),总复杂度变成了N * LogN 这里算是学到了一个计算WPL的技巧。

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
struct Que
{
LL arr[N << 1];
int l, r;
void init()
{
l = r = 0;
}
void push(const LL &val)
{
arr[r++] = val;
}
void pop()
{
++l;
}
inline LL front()
{
return arr[l];
}
inline bool empty()
{
return l == r;
}
};
Que qa, qb;
LL arr[N];
int n;
LL T; bool check(const int &k)
{
qa.init();
qb.init();
int res = (n - 1) % (k - 1);
if (res)
{
res = (k - 1) - res;
while (res--)
qa.push(0LL);
}
for (int i = 1; i <= n; ++i)
qa.push(arr[i]); LL sum = 0LL;
while ((!qa.empty()) || (!qb.empty()))
{
LL temp = 0LL;
for (int i = 0; i < k; ++i)
{
if (qa.empty() && qb.empty())
break;
if (!qa.empty() && !qb.empty())
{
if (qa.front() < qb.front())
{
temp += qa.front();
qa.pop();
}
else
{
temp += qb.front();
qb.pop();
}
}
else if (!qa.empty())
{
temp += qa.front();
qa.pop();
}
else if (!qb.empty())
{
temp += qb.front();
qb.pop();
}
}
sum += temp;
if (qa.empty() && qb.empty())
break;
qb.push(temp);
}
return sum <= T;
}
int main(void)
{
int tcase, i;
scanf("%d", &tcase);
while (tcase--)
{
scanf("%d%I64d", &n, &T);
for (i = 1; i <= n; ++i)
scanf("%I64d", arr + i);
sort(arr + 1, arr + 1 + n);
int L = 2, R = n;
int k = 1;
while (L <= R)
{
int mid = (L + R) >> 1;
if (check(mid))
{
k = mid;
R = mid - 1;
}
else
L = mid + 1;
}
printf("%d\n", k);
}
return 0;
}

HDU 5884 Sort(二分答案+计算WPL的技巧)的更多相关文章

  1. HDU 5884 Sort (二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 nn个有序序列的归并排序.每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的 ...

  2. HDU 5884 Sort (二分+k叉哈夫曼树)

    题意:n 个有序序列的归并排序.每次可以选择不超过 k 个序列进行合并,合并代价为这些序列的长度和.总的合并代价不能超过T, 问 k最小是多少. 析:首先二分一下这个 k .然后在给定 k 的情况下, ...

  3. HDU - 5884 Sort (二分答案+贪心)

    有n个数字,你需要把这n个数字合成一个数字,每次只能把k个数字合并成一个,花费为这k个数字的和. 给一个最大花费,问不超过这个最大花费的情况下,k的最小值. Sample Input 1 5 25 1 ...

  4. Sort HDU - 5884(优先队列+二分)

    Sort Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. U - Inviting Friends HDU - 3244(二分答案 + 完全背包)

    U - Inviting Friends HDU - 3244 You want to hold a birthday party, inviting as many friends as possi ...

  6. HDU 5884 Sort(2016年青岛网络赛 G 二分+贪心+小优化)

    好题 题意:给你n<=100000个数,每个数范围[0,1000],然后给你一个最大的代价T,每次最多合并k个数成为一个数,代价为k个数的总和.问最后合成1个数的总代价不大于T的最小k 题解:我 ...

  7. HDU 5884 Sort(二分+优先队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=5884 题意:有个屌丝设计了一个程序,每次可以将k个数组进行合并,代价为这k个数组总的长度之和.现在另外一个屌丝要 ...

  8. 【最优K叉树】hdu 5884 Sort

    http://acm.hdu.edu.cn/showproblem.php?pid=5884 参考:https://www.cnblogs.com/jhz033/p/5879452.html [题意] ...

  9. noiac64 sort (二分答案)

    首先如果L=1,那就可以直接用一个优先队列来做 但它并不是1 所以要换个做法 假设我们已经知道第L的数是x,第R的数是y 那其实就只需要找到[x+1,y+1]这一段,然后再加上一定数量的x和y就是答案 ...

随机推荐

  1. 对ListBox控件中的数据进行排序

    实现效果: 知识运用: ListBox控件的Sorted属性 //ListBox控件中的数据项是否按字母顺序排序 public bool Sorted{get;set;} 实现代码: private ...

  2. python_26_dictionary

    #key-value 字典无下标 所以乱序,key值尽量不要取中文 info={ 'stu1101':'Liu Guannan', 'stu1102':'Wang Ruipu', 'stu1103': ...

  3. 支持向量机: Maximum Margin Classifier

    支持向量机即 Support Vector Machine,简称 SVM .我最开始听说这头机器的名号的时候,一种神秘感就油然而生,似乎把 Support 这么一个具体的动作和 Vector 这么一个 ...

  4. 人脸验证算法Joint Bayesian详解及实现(Python版)

    人脸验证算法Joint Bayesian详解及实现(Python版) Tags: JointBayesian DeepLearning Python 本博客仅为作者记录笔记之用,不免有很多细节不对之处 ...

  5. Win8如何默认以管理员运行程序

    在Win7的时候,关闭UAC,使用自己的用户名,所有程序都是默认以管理员身份运行的. 但是在Win8,关闭UAC,程序不是默认以管理员身份运行的. 在论坛看到的解决方法是:1.用Administrat ...

  6. Bootstrap 弹出框(Popover)插件

    Bootstrap 弹出框(Popover)插件与Bootstrap 提示工具(Tooltip)插件类似,提供了一个扩展的视图,用户只需要把鼠标指针悬停到元素上面即可.弹出框的内容完全由Bootstr ...

  7. 数据库引擎InnoDB和myisam的区别和联系

    1.ENGINE=InnoDB 数据库存储引擎,DEFAULT 默认,CHARSET=utf8 数据库字符编码 2.数据库的存储引擎, mysql中engine=innodb和engine=myisa ...

  8. nuxt.js express模板项目虚拟目录部署问题汇总

    声明环境 反向代理:nginx或者iis的ARR 模板项目:nuxt-express 部署环境:windows 经过了一段时间在windows环境部署项目来看,关于虚拟目录的问题汇总如下, 发布场景假 ...

  9. [基础学习]MySQL常用语句命令总结

    前言 相信平时大家在开发时都会使用MySQL数据库,它是目前比较火的一款数据库工具,对于大多数企业的业务来说,MySQL可以很完美地支持了. 很多时候我们都是借助mysql可视化工具操作mysql,虽 ...

  10. 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8

    a,b,c,d,e,f,g=1,2,3,4,5,8,9 m = a > b and c < d or c > e n = b > a or g < f x = m and ...