Sort
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 739 Accepted Submission(s): 144
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
Source
Recommend
题意:给你n个数和一个T,求最小k将n个数最终合并成一个数,每一步能选任意选几个数合并,k为合并过程中合并的最大个数,每一步的花费为每一步选择几个数合并后的数的值,总花费必须<=T。
思路:很明显二分k出答案,然后就是怎样判断当前k值是否满足花费<=T的问题,这里我的解法是卡着时间过的800~1000ms之间(感觉不是正解),因为要判断当前k是否可以,那就是要求出当前k的最小花费,最小花费仔细想想就可以知道先把小的数合并是最优的,因为越大的数参与累加的次数越少越划算,然后就是有剩余的问题了,例如6个数,每次取3个再合并再放回去的话是会有剩余1的,所以要先把(剩余数+1)个小的数合并起来,这样才是最优的做法,下面给代码:
#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
typedef long long LL;
using namespace std;
#define inf 0x3f3f3f3f
#define maxn 100005
typedef long long LL;
priority_queue<int>q;
int n,T,a[maxn];
bool solve(int x){
LL ans=0;
int sy=(n-1)%(x-1);
LL re=0;
for(int i=0;i<sy+1;i++){
int now=q.top();
q.pop();
re+=-now;
}
ans+=re;
if(ans>T){
return false;
}
if(re&&!q.empty()){
q.push(-(int)re);
}
while(!q.empty()){
re=0;
for(int i=0;i<x&&!q.empty();i++){
int now=q.top();
q.pop();
re+=-now;
}
ans+=re;
if(ans>T){
return false;
}
if(!q.empty()){
q.push(-(int)re);
}
}
return true;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&T);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
int l=2,r=n;
int ans=n;
while(l<=r){
int mid=l+r>>1;
while(!q.empty()){
q.pop();
}
for(int i=0;i<n;i++){
q.push(-a[i]);
}
if(solve(mid)){
r=mid-1;
ans=mid;
}
else{
l=mid+1;
}
}
printf("%d\n",ans);
}
}