HDU - 5884

一年前入门的题目了,当时没写出来,现在填坑

题目

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.

坑点

  1. 明显用二分 + 贪心,二分k,每次选择cost最少的来合并

  2. 假如枚举的 k,那么每次合并消耗的是 k−1个,最后合并完后剩下一个,所以总消耗是 n−1 个,如果 (n−1)%(k-1)!=0,会存在零头,要先合并 零头 + 1个序列

  3. 有点卡常

方法1

  1. 用priority_queue有点卡,可以先把n个序列从小到大排序,优先队列每次要check函数内定义,n <= 10^5,这样优先队列内存占用小于 1M (栈空间) 所以可以开的下,输入输出用scanf printf

  2. 时间复杂度 O(n * logn * logn j) 出队入队次数 * 二分次数 * 优先队列单次插入删除

  3. code

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 1e5 + 10;
int a[N];

ll n,t;    

bool check(int k){
    
    priority_queue< int, vector<int> , greater<int> > q1;
    for(int i = 0; i < n; ++i)
        q1.push(a[i]);

    int ct = 2,sz = (n - 1) %  (k - 1);
    ll res = 0;
    if(sz){
        for(int i = 0; i <= sz; ++i){
            res += q1.top();
            q1.pop();
        }
        q1.push(res);
    }

    ll z;
    while(q1.size() > 1 && res <= t){
        z = 0;
        for(int i = 0; i < k && q1.size(); ++i){
            z += q1.top();
            q1.pop();
        }
        q1.push(z);
        res += z;
    }
    
    return res <= t;
} 

int main(){
    
    int T; scanf("%d",&T);
    while(T--){

        scanf("%lld %lld",&n,&t);

        
        for(int i = 0; i < n; ++i)
        {
            scanf("%d",&a[i]);        
        }
        sort(a, a + n);
        
        int l = 2,r = n,ans = n;
        while(l <= r && l < ans){
            int m = (l + r) >> 1;
            if(check(m)){
                ans = min(ans,m);
                r = m - 1;
            }else l = m + 1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

方法2

  1. 先排序,然后用两个队列模拟优先队列,这样就少了一个log。很好过

  2. 时间复杂度 O(nlogn)

  3. code

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 1e5 + 10;
int a[N];

ll n,t;    

bool check(int k){
    
    queue<ll> q1,q2;
    for(int i = 0; i < n; ++i)
        q1.push(a[i]);

    int ct = 2,sz = (n - 1) %  (k - 1);
    ll res = 0;
    if(sz){
        for(int i = 0; i <= sz; ++i){
            res += q1.front();
            q1.pop();
        }
        q1.push(res);
    }

    ll x,y;
    while(q1.size() + q2.size() > 1 && res <= t){
        ll z = 0;
        for(int i = 0; i < k && q1.size() + q2.size(); ++i){
            if(q1.size() && q2.size()){
                x = q1.front();
                y = q2.front();
                if(x < y){
                    z += x;
                    q1.pop();
                }else{
                    z += y;
                    q2.pop();
                }
            }else if(q1.size()){
                ct = 2;
                z += q1.front();
                q1.pop();
            }else{
                ct = 1;
                z += q2.front();
                q2.pop();
            }
        }
        if(ct == 1)
            q1.push(z);
        else q2.push(z);
        res += z;
    }
    
    return res <= t;
} 

int main(){
    
    int T; scanf("%d",&T);
    while(T--){

        scanf("%lld %lld",&n,&t);

        
        for(int i = 0; i < n; ++i)
        {
            scanf("%d",&a[i]);        
        }
        sort(a, a + n);
        
        int l = 2,r = n,ans = n;
        while(l <= r && l < ans){
            int m = (l + r) >> 1;
            if(check(m)){
                ans = min(ans,m);
                r = m - 1;
            }else l = m + 1;
        }
        printf("%d\n",ans);
    }
    return 0;
}
### 关于HDU - 6609 的题目解析 由于当前未提供具体关于 HDU - 6609 题目的详细描述,以下是基于一般算法竞赛题型可能涉及的内容进行推测和解答。 #### 可能的题目背景 假设该题目属于动态规划类问题(类似于多重背包问题),其核心在于优化资源分配或路径选择。此类问题通常会给出一组物品及其属性(如重量、价值等)以及约束条件(如容量限制)。目标是最优地选取某些物品使得满足特定的目标函数[^2]。 #### 动态转移方程设计 如果此题确实是一个变种的背包问题,则可以采用如下状态定义方法: 设 `dp[i][j]` 表示前 i 种物品,在某种条件下达到 j 值时的最大收益或者最小代价。对于每一种新加入考虑范围内的物体 k ,更新规则可能是这样的形式: ```python for i in range(n): for s in range(V, w[k]-1, -1): dp[s] = max(dp[s], dp[s-w[k]] + v[k]) ``` 这里需要注意边界情况处理以及初始化设置合理值来保证计算准确性。 另外还有一种可能性就是它涉及到组合数学方面知识或者是图论最短路等相关知识点。如果是后者的话那么就需要构建相应的邻接表表示图形结构并通过Dijkstra/Bellman-Ford/Floyd-Warshall等经典算法求解两点间距离等问题了[^4]。 最后按照输出格式要求打印结果字符串"Case #X: Y"[^3]。 #### 示例代码片段 下面展示了一个简单的伪代码框架用于解决上述提到类型的DP问题: ```python def solve(): t=int(input()) res=[] cas=1 while(t>0): n,k=list(map(int,input().split())) # Initialize your data structures here ans=find_min_unhappiness() # Implement function find_min_unhappiness() res.append(f'Case #{cas}: {round(ans)}') cas+=1 t-=1 print("\n".join(res)) solve() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值