HDU 5884 Sort 2016青岛网赛

Sort
Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 982    Accepted Submission(s): 207


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
2016 ACM/ICPC Asia Regional Qingdao Online 

一看就知道是huffman编码题 想都没想直接开搞….
不难发现 如果k’是满足cost<=T的最小的k 那对任意k>=k’ 也能满足cost<=T
这就可以用二分法查找最小的k 复杂度为O(n(logn)*(logn))
比赛的时候 用了priority_queue 无限TLE 后来换了手写的二叉堆700+ms(也可以用2个双向队列 然后归并在一起)
然而 无限WA……
赛后才发现 如果n个串 每次合k个串 最后剩下不足k个串的话 应该先将那不足k个的几个串合并起来 才能达到最优解………
显然 n个串 每次合并一次后剩下n-(k-1)个
如果最后一次也合并k个 那n-t*(k-1)=1 //t为非负整数
so: (n-1)%(k-1)==0
最后不足k个
要提前合并的个数为 (n-1)%(k-1)+1


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
#include<bitset>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007

const int N=100000+5;
ll a[N];

bool check(int k,int n,ll t){
    deque<int>p(a,a+n),q;
    ll cost=0;
    if((n-1)%(k-1)){
        int x=(n-1)%(k-1)+1;
        for(int i=0;i<x;++i){
            cost+=p[0];
            p.pop_front();
        }
        q.push_back(cost);
    }
    while(p.size()+q.size()>1){
        ll c=0;
        for(int i=0;i<k;++i){
            if(!p.empty()&&(q.empty()||p[0]<=q[0])){
                c+=p[0];
                p.pop_front();
            }
            else
                if(!q.empty()&&(p.empty()||q[0]<=p[0])){
                    c+=q[0];
                    q.pop_front();
                }
        }
        cost+=c;
        q.push_back(c);
    }
    return cost<=t;
}

int binarySearch(int n,ll t){
    int j=n,i=2;
    while(i<j){
        int mid=(i+j)/2;
        bool check_res=check(mid,n,t);
        if(check_res)
            j=mid;
        else
            i=mid+1;
    }
    return i;
}

int main()
{

    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    int tc,n;
    ll t;
    scanf("%d",&tc);
    for(int ii=0;ii<tc;++ii){
        scanf("%d%lld",&n,&t);
        for(int i=0;i<n;++i)
            scanf("%lld",a+i);
        sort(a,a+n);
        printf("%d\n",binarySearch(n,t));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值