HDU 5884 - Sort

54 篇文章 0 订阅
17 篇文章 0 订阅
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


题意:给出 n 个有序序列,要求把所有的序列都合并起来,每次可以选择 k 个序列进行合并,而每次合并的代价就是序列长度的和,且总代价不得超过 T ,问 k 最小是多少。

1.二分法找 k

2.对于每个 k 进行一次检查看能不能满足题目要求,每个 k 都要合并 n-1 个数,一共 k-1 次,所以 (n-1) % (k-1) 如果不是0,就将余出来的这个数先单独合并一次,然后再每次合并 k-1 个数。建立两个队列,将数字丢进队列1,每次从队列1取数字,合并完的数就丢进队列2.

#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;

long long T;
long long a[200000 + 5];
int n;
queue<int>Q1, Q2;

bool solve(int k)
{
    while (!Q1.empty())
        Q1.pop();
    while (!Q2.empty())
        Q2.pop();
    for (int i = 1; i <= n; ++i)
        Q1.push(a[i]);

    int num = 0;
    long long sum = 0;
    long long ans = 0;

    if ((n-1) % (k-1) != 0)
    {
        num = (n-1) % (k-1) + 1;
        for (int i = 1; i <= num; ++i)
        {
            sum += Q1.front();
            Q1.pop();
        }
        Q2.push(sum);
        ans += sum;
    }

    while (!Q1.empty())
    {
        sum = 0;
        for (int i = 1; i <= k; ++i)
        {
            if (!Q1.empty() && !Q2.empty())
            {
                if (Q1.front() <= Q2.front())
                {
                    sum += Q1.front();
                    Q1.pop();
                }
                else
                {
                    sum += Q2.front();
                    Q2.pop();
                }
            }
            else if (Q1.empty())
            {
                sum += Q2.front();
                Q2.pop();
            }
            else if (Q2.empty())
            {
                sum += Q1.front();
                Q1.pop();
            }
        }
        ans += sum;
        Q2.push(sum);
    }
    if (ans > T)
        return false;

    sum = 0;
    num = 0;
    while (!Q2.empty())
    {
        sum += Q2.front();
        Q2.pop();
        num++;
        if (num == k)
        {
            Q2.push(sum);
            ans += sum;
            sum = num = 0;
            if (Q2.size() == 1)
                break;
        }
    }
    if (ans > T)
        return false;
    return true;
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d %lld", &n, &T);
        for (int i = 1; i <= n; ++i)
            scanf("%lld", &a[i]);
        sort(a + 1, a + n + 1);

        int left = 2;
        int right = n;
        int mid;
        int ans = 1;

        while (left <= right)
        {
            mid = (left + right) / 2;
            if (solve(mid))
            {
                right = mid - 1;
                ans = mid;
            }
            else
                left = mid + 1;
        }
        printf("%d\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值