hdu 5884 二分+队列



链接:戳这里


Sort

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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
n个有序序列的归并排序。每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的合并代价不能超过TT, 问kk最小是多少。


思路:

1:首先想到的是二分这个答案k,然后check每个k是否可行

2:对于每个k,总共需要归并n-1个数,每次归并k-1个数

所以当(n-1)%(k-1)!=0的时候,会出现归并不能最大化个数的情况,这样会影响二分的单调性

我们先取(n-1)%(k-1)个小的数凑成一次k

      接下来的数我们已经知道了肯定是(n-1)/(k-1)块了,所以可以直接想到放进优先队列,然后每次都拿前k个数,新的数继续丢进去,但是这样会T

      那么?我们这样处理,将其余的数丢进队列qu1中,再开一个队列qu2,每次新的数丢进qu2中。每次再qu1和qu2中一共拿k个小的数来组成一个新的数,新的数丢到qu2队尾去。接下来证明为什么不丢进优先队列,反而队列就可以了(也就是为什么qu2都是排好序的?),每次新丢进来数都是由前k个小的数取和得来的,所以每次丢进队尾的数都比前面丢进去的数要大,随手画一下就可以了,然后拿完qu1再去拿qu2。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
ll a[200100],T;
int n;
queue<int> qu1,qu2;
bool pd(int k){
    while(!qu1.empty()) qu1.pop();
    while(!qu2.empty()) qu2.pop();
    for(int i=1;i<=n;i++) qu1.push(a[i]);
    int num=0;
    ll sum=0,ans=0;
    if((n-1)%(k-1)!=0){
        num=(n-1)%(k-1)+1;
        for(int i=1;i<=num;i++) {
            sum+=qu1.front();
            qu1.pop();
        }
        qu2.push(sum);
        ans+=sum;
    }
    while(!qu1.empty()){
        sum=0;
        for(int i=1;i<=k;i++){
            if(!qu1.empty() && !qu2.empty() ){
                if(qu1.front()<=qu2.front()){
                    sum+=qu1.front();
                    qu1.pop();
                } else {
                    sum+=qu2.front();
                    qu2.pop();
                }
            } else if(qu1.empty()){
                sum+=qu2.front();
                qu2.pop();
            } else if(qu2.empty()){
                sum+=qu1.front();
                qu1.pop();
            }
        }
        ans+=sum;
        qu2.push(sum);
    }
    if(ans>T) return false;
    sum=0;num=0;
    while(!qu2.empty()){
        sum+=qu2.front();
        qu2.pop();
        num++;
        if(num==k){
            qu2.push(sum);
            ans+=sum;
            sum=0;
            num=0;
            if(qu2.size()==1) break;
        }
    }
    if(ans>T) return false;
    return true;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%I64d",&n,&T);
        for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
        sort(a+1,a+n+1);
        int l=2,r=n,mid,ans=1;
        while(l<=r){
            mid=(l+r)/2;
            if(pd(mid)) {
                r=mid-1;
                ans=mid;
            } else l=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
对于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))。希望这个解法对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值