Sort 二分 + (队列 + sort 代替优先队列)

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

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

题意:t组数据,每次输入N,T,然后输入N个数,进行合并操作,将其中k个数合并为一个数后,代价为这k个数的和,并将这个和放入剩余的数列中,一直合并下去……最后合并为一个数,要求总的代价不超过T,求最小的k值;

思路:k叉哈夫曼树,很明显k值在2~N之间,而且k越大总的代价越小,那么利用这个性质我们可以对k值进行二分查找,我开始时想的用优先队列做,但超时了……我们可以对数组先从小到大排序,然后利用一个队列装合并得到的数,每次取数组和队列中较小的数,注意用一个变量pos记录数组取完数后的下一个位置,队列中取完数后要删除这个数,为什么可以这样呢? 因为每次合并得到的数一定小于等于上次合并得到的数,所以最小数一是 数组pos位置和队列首中的较小者。另外,这些数的个数不一定满足k个k个的合并,所以要先合并不足的几个数,什么时候不满足呢,(N-1)%(k-1)!=0 时;为了使总代价最小,我们先合并前面的几个数。

AC代码:

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;

const int maxn = 100000+5;
int a[maxn];
int n,T;

int cal(int k)
{
    queue<long long> q;
    int pos = 0;
    long long sum = 0;
    if((n-1)%(k-1)!=0)
    {
        pos = (n-1)%(k-1)+1;
        for(int i=0;i<pos;i++)
        {
            sum += a[i];
        }
        q.push(sum);
    }
    while(pos<n || !q.empty())
    {
        long long sum2 = 0;
        for(int i=0;i<k;i++)
        {
            if(!q.empty())
            {
                if(pos<n && q.front()>a[pos])
                {
                    sum2 += a[pos];
                    sum += a[pos];
                    pos++;
                }
                else
                {
                    sum2 += q.front();
                    sum += q.front();
                    q.pop();
                }
            }
            else if(pos<n)
            {
                sum2 += a[pos];
                sum += a[pos];
                pos++;
            }
        }
        if(sum>T) return 0;
        if(pos<n || !q.empty())
            q.push(sum2);
    }
    if(sum>T) return 0;
    else return 1;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%lld",&n,&T);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
        int l = 2, r = n,mid;
        while(l<=r)
        {
            mid = (l + r)>>1;
            int f = cal(mid);
            if(f==0) l = mid + 1;
            else r = mid - 1;
        }
        printf("%d\n",l);
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值