div2 334 B binary search/ greedy 1400

原文题目
Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter, where there is actually grass instead of corn. Before moving, he must pack his cowbells into k boxes of a fixed size. In order to keep his collection safe during transportation, he won’t place more than two cowbells into a single box. Since Kevin wishes to minimize expenses, he is curious about the smallest size box he can use to pack his entire collection.

Kevin is a meticulous cowbell collector and knows that the size of his i-th (1 ≤ i ≤ n) cowbell is an integer si. In fact, he keeps his cowbells sorted by size, so si - 1 ≤ si for any i > 1. Also an expert packer, Kevin can fit one or two cowbells into a box of size s if and only if the sum of their sizes does not exceed s. Given this information, help Kevin determine the smallest s for which it is possible to put all of his cowbells into k boxes of size s.

Input
The first line of the input contains two space-separated integers n and k (1 ≤ n ≤ 2·k ≤ 100 000), denoting the number of cowbells and the number of boxes, respectively.

The next line contains n space-separated integers s1, s2, …, sn (1 ≤ s1 ≤ s2 ≤ … ≤ sn ≤ 1 000 000), the sizes of Kevin’s cowbells. It is guaranteed that the sizes si are given in non-decreasing order.

Output
Print a single integer, the smallest s for which it is possible for Kevin to put all of his cowbells into k boxes of size s.

原文题意
给了两个数,n和k,分别代表数组中的元素个数和可用的箱子个数,已知一个箱子中最多可以装两个物品,求把这些物品装完之后,箱子所需要的最小容量是多少

题解
首先,假设我们已知箱子的重量,那么这个题就变成了一个贪心的裸题了,那么我们可想,这时候问题就变成了怎么样装箱子就可以装下了,那么这时候我们可以开一个标记数组,因为数组已经是排序好的,所以我们就可以直接贪心取最小个数的箱子了。我们为了节省箱子,最大的物品只能和最小的物品排在一起,但如果他俩不能排在一起,说明最大的物品比较大,没有物品和它一起,即它只能单独存放;但是如果可以放在一起,那么我们就继续向中间遍历,并且将用过的物品进行标记,这时候,两个物品只占用了一个箱子,就是最佳情况;
注意,大物品单独存放之后意味着我们要找次大的物品和原来的小物品进行比较,继续判断能否存下,这里的贪心策略就是最优的
之后,我们解决了箱子个数的问题,就要解决箱子大小的问题,因为是找到箱子大小的最小值,我们很容易可以想到可以进行枚举,但10的5次方的数据枚举的话必然超时,因为我们内部已经采用了一个O(n)的算法了,这里不能变成O(n^2),所以我们可以利用二分查找的方法找到箱子的大小,显然我们可以发现,二分查找的时候,左边界的必然是数组中最大元素的值,因为箱子的大小始终要保证可以装下任何一个物品,而右边界,应该是数组中最大的两个数进行求和,因为一个箱子中最多装两个物品,所以其大小必然不会超过其求和
完整代码

#include <bits/stdc++.h>
using namespace std;
int a[100005];
int b[100005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,i,j,k,t;
    cin>>n>>k;
    int r=0;
    for(i=1;i<=n;i++){
        cin>>a[i];
    }
    r=a[n]+a[n-1];//r是右边界,右边界只用存最大的两个数就可以了
    int l=a[n];
    int mid;
    while(l<=r){//二分对左右边界内进行查找
        for(i=0;i<=n;i++)
            b[i]=0;//每次枚举都要进行初始化
            t=0;
        mid=l+(r-l)/2;//mid是当前查找到的箱子大小
            j=n;
            i=1;
        while(i<=j){//对箱子装载进行贪心
            if(b[i]==0&&b[j]==0){
                if(a[i]+a[j]<=mid){
                t++;//如果能装下说明消耗
                b[i]=1;
                b[j]=1;//用过之后对物品进行标记
                i++;
                j--;//下个物品的位置发生改变
                }
                else j--;//没找到可以组的说明当前太大了,要变小
            }
            else if(b[i]==1&&b[j]==0) i++;
            else if(b[i]==0&&b[j]==1) j--;
            else i++,j--;//有使用过的就要改变物品
        }
        for(i=1;i<=n;i++)
            if(b[i]==0) t++;//统计还有多少没组队的物品,都要单独装
        if(t>k){
            l=mid+1;
        }
        else r=mid-1;//二分
    }
    cout<<l<<endl;
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值