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 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 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.
 

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 (2N100000) and T (Ni=1ai<T<231).
In the next line there are N integers a1,a2,a3,...,aN(i,0ai1000).
 

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
原创粉丝点击