2019牛客暑假多校训练赛第六场 - D - Move (假二分)

题目链接:https://ac.nowcoder.com/acm/contest/886/D

题意:给你 n 个物品的体积和 k 个相同体积的盒子,让你将 n 个物品装进 k 个盒子,在物品体积小于盒子体积的前提下尽量先装体积较大的物品。问盒子的最小体积。

思路:本题不具有单调性(物品的体积不连续),暴力枚举答案即可。

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7;
int a[N], b[N], n, k;
multiset <int> s;
multiset<int> ::iterator it;
int check(int x)
{
    s.clear();
    for(int i = 1; i <= n; i++) s.insert(a[i]);
    for(int i = 0; i < k; i++)
    {
        int v = x;
        while(!s.empty() && (it = s.upper_bound(v) ) != s.begin())
        {
            it--; v -= *it;
            s.erase(it);
        }
        if(s.empty()) return 1;
    }
    return 0;
}
int main()
{
    int T; scanf("%d",&T);
    for(int Cas = 1; Cas <= T; Cas++)
    {
        scanf("%d%d",&n, &k);
        int sum = 0, mx = 0;
        for(int i = 1; i <= n; i++)
            scanf("%d",&a[i]), sum += a[i], mx = max(mx, a[i]);
        int ans = max(mx, (int)ceil(1.0 * sum / k));
        while(1)
        {
            if(check(ans))
            {
                printf("Case #%d: %d\n",Cas, ans);
                break;
            }
            ans++;
        }
    }
}
/*
1
15 5
39 39 39 39 39 60 60 60 60 60 100 100 100 100 10
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值