zoukankan      html  css  js  c++  java
  • HDU 5884 Sort(二分答案+计算WPL的技巧)

    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 (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
    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
    ", k);
        }
        return 0;
    }
  • 相关阅读:
    Add a column in table control
    ALV
    ABAP Object Differences
    Field Symbols, Casting Decimal Places
    fROM PPV report
    python全局变量
    管理商品demo
    Mac系统在Pycharm中切换解释器
    python中 元组
    python中字符串格式化
  • 原文地址:https://www.cnblogs.com/Blackops/p/6382637.html
Copyright © 2011-2022 走看看