Sort
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 912 Accepted Submission(s): 190
Total Submission(s): 912 Accepted Submission(s): 190
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.
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) .
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
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5884
题意:
nnn
个有序序列的归并排序.每次可以选择不超过
kkk
个序列进行合并,合并代价为这些序列的长度和.总的合并代价不能超过
TTT
, 问
kkk
最小是多少。
.
题解:首先二分一下这个
kkk
。然后在给定
kkk
的情况下,这个代价其实就是
kk
叉的哈夫曼树问题。因此直接套用哈夫曼树的堆做法即可。复杂度
O(nlog2n)O(n \log^2 n)
,这样优化一下读入是可以卡过去的。
然后主代码手表示,利用合并的单调性,可以去掉优先队列得到
O(nlogn)O(n \log n)O(nlogn)
的做法:先对所有数排序,另外一个队列维护合并后的值,取值时从两个序列前端取小的即可。
昂:如果
(n-1) \%(k-1) \neq 0(n−1)%(k−1)≠0
,要把最小的
(n−1)%(k−1)+1(n-1)\%(k-1)+1
个数先合并一下。
我是用优先队列做的,比赛的时候怎么也是TLE,加一个输入外挂就好。
这里解释一下为什么是 把最小的
(n−1)%(k−1)+1(n-1)\%(k-1)+1
个数先合并一下。
由哈夫曼树的定义不难得到是把优先队列里的k个数取出来进行合并再加进优先队列。
也就是说每一步操作实际对总数的影响是少了k-1个数,最后剩下一个数。
所以若(n-1)%(k-1)==0,则直接进行k的哈夫曼树。
若不是,择优即对最小的 n−1)%(k−1)+1个数进行合并,剩下的就进行k的哈夫曼树;
代码:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<string>
#include<algorithm>
#include <iostream>
using namespace std;
const int maxn=100000+4;
typedef long long ll;
priority_queue<ll, vector<ll>, greater<ll> >p;
ll num[maxn];
int Scan()
{
int res = 0, ch, flag = 0;
if((ch = getchar()) == '-') //判断正负
flag = 1;
else if(ch >= '0' && ch <= '9') //得到完整的数
res = ch - '0';
while((ch = getchar()) >= '0' && ch <= '9' )
res = res * 10 + ch - '0';
return flag ? -res : res;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
ll n,m;
scanf("%I64d%I64d",&n,&m);
ll l=2,r=n;
for(int i=0;i<n;i++)
{
num[i]=Scan();
}
sort(num,num+n);
ll res=0;
while(l<r)
{
while(!p.empty())
p.pop();
for(int i=0;i<n;i++)
p.push(num[i]);
res=0;
ll mm=(l+r)/2;
ll nn=n;
int cc=(n-1)%(mm-1);
if(cc!=0)
cc++;
if(cc!=0)
{
ll ans=0;
while(cc)
{
ans+=p.top();
p.pop();
cc--;
}
p.push(ans);
res+=ans;
}
int v=(n-1)/(mm-1);
while(v)
{
v--;
ll ans=0,ee=mm;
while(ee)
{
ans+=p.top();
p.pop();
ee--;
}
p.push(ans);
res+=ans;
}
//printf("%I64d %I64d\n",mm,res-1);
if(res-1>m)
l=mm+1;
else
r=mm;
}
printf("%I64d\n",l);
}
return 0;
}