HDU 5884 青岛网络赛(二分加维护队列)

Sort

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 689    Accepted Submission(s): 134


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
 
题目大意:
n
n
个有序序列的归并排序.每次可以选择不超过 kk 个序列进行合并,合并代价为这些序列的长度和.总的合并代价不能超过 TT , 问 kk 最小是多少。
思路:先对所有的数排序,二分查找k,之后再将合并之后的数字存入一个队列之中。每次取数组和队列中较小的数。这样就可以,不过有个需要注意
如果(n-1) \%(k-1) \neq 0(n1)%(k1)0,要把最小的(n-1)\%(k-1)+1(n1)%(k1)+1个数先合并一下
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
long long num[111111];
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        long long n,T;
        queue<long long> q;
        scanf("%lld%lld",&n,&T);
        for (int i = 0 ; i < n ; i++ )
        {
            scanf("%lld",&num[i]);
        }
        sort(num,num+n);
        while (!q.empty())
        {
            q.pop();
        }
        int l = 2 , r = n;
        while (l <r)
        {
            long long sum = 0;
            int mid = (l + r) / 2;
            //printf("%d\n",mid);
            long long temp = 0;
            int all = 0;
            if ((n - 1) % (mid - 1) != 0)
            {
                for (int i = 0 ; i < ((n - 1) % (mid - 1)) + 1 ; i++ )
                {
                    temp += num[i];
                }
                all = ((n - 1) % (mid - 1)) + 1;
                sum += temp;
                q.push(temp);
            }
            temp = 0;
            int cou = 0;
            while (!q.empty() || all < n)
            {
                if (q.empty())
                {
                    sum += num[all];
                    temp += num[all];
                    all++;
                    cou++;
                }
                else
                {
                    if ( all < n)
                    {
                        if (q.front() >= num[all])
                        {
                            sum += num[all];
                            temp += num[all];
                            all++; 
                            cou++;
                        }
                        else 
                        {
                            sum += q.front();
                            temp += q.front();
                            q.pop();
                            cou++;
                        }
                    }
                    else 
                    {
                        sum += q.front();
                        temp += q.front();
                        q.pop();
                        cou++;
                    }
                }
                if ( cou == mid )
                {
                    if ( all < n || !q.empty())
                        q.push(temp);
                    else 
                        break;
                    temp = 0;
                    cou = 0;
                }
                //printf("%lld %d\n",sum , all);
            }
            while (!q.empty())
            {
                q.pop();
            }
            if (sum > T)
            {
                l = mid + 1;
            }
            else 
            {
                r = mid;
            }
        }
        printf("%d\n",l);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值