2019牛客暑期多校训练营(第六场) Move

链接:https://ac.nowcoder.com/acm/contest/886/D
来源:牛客网

题目描述

After the struggle of graduating from college, TangTang is about to move from a student apartment to his new home.

TangTang has n items to move, the i-th of which is of volume v_i. He can pack all these items into at most K boxes of the same volume.

TangTang is so clever that he uses the following strategies for packing items:

  • Each time, he would put items into a box by the next strategy, and then he would try to fill another box.
  • For each box, he would put an unpacked item of the largest suitable volume into the box repeatedly until there is no such item that can be fitted in the box.

Now, the question is what is the minimum volume of these boxes required to pack all items.

输入描述:

There are multiple test cases. The first line contains an integer T (1 \leq T \leq 201≤T≤20), indicating the number of test cases. Test cases are given in the following.

For each test case, the first line contains two integers n, K (1 \leq n, K \leq 10001≤n,K≤1000), representing the number of items and the number of boxes respectively.

The second line contains n integers v_1 , v_2, …, v_n

(1 \leq v_1, v_2, \ldots, v_n \leq 10001≤v_i <= 1000), where the i-th integer v_i represents the volume of the i-th item.

输出描述:

For each test case, output “Case #x: y” in one line (without quotes), where x indicates the case number starting from 1, and y denotes the answer to this test case.

输入

1
5 3
1 2 3 4 5

输出

Case #1: 5

Brief description

将n个物品放在k个等体积的箱子里,每次放的策略是优先选最大能放进箱子里的物品
问箱子的最少体积是多少

Solution

 上来就二分, 先WA了一发后,想到解可能没有单调性。并不是体积越大越好。
那没办法,只能改为挨个遍历
发现结果一定在区间【(ceil(sum / k)), (ceil(sum / k) + maxVol)】中
sum为物品重量和,maxVol为最重物品的重量

WA Code

#include <bits/stdc++.h>
 
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d", &a, &b)
#define siii(a, b, c) scanf("%d", &a, &c)
#define siiii(a, b, c, d) scanf("%d", &a, &c, &d)
#define pi(a) printf("%d\n", a)
#define pii(a, b) printf("%d %d\n", a, b)
#define piii(a, b, c) printf("%d %d% d\n", a, b, c)
#define piiii(a, b, c, d) printf("%d %d %d %d\n", a, b, c, d)
#define forRange(i, j, k) for(int i = j; i < k; ++i)
#define mem(a, b) memset(a, b, sizeof(a))
#define mCopy(to, from) memcpy(to, from, sizeof(from))
#define testData(f) freopen(f,"r",stdin)
using namespace std;
const int maxn = 1005;
const int INF = 0x3f3f3f3f;
int n, k;
int volume[maxn];
bool used[maxn];
 
bool test(int boxVol) {
    mem(used, 0);
    int currBox = 1, currVolLeft = boxVol;
    while (currBox <= k) {
        for (int i = n - 1; i >= 0; --i) {
            if (!used[i]) {
                if (volume[i] <= currVolLeft) {
                    currVolLeft -= volume[i];
                    used[i] = true;
                }
            }
        }
        ++currBox;
        currVolLeft = boxVol;
    }
    for (int i = 0; i < n; ++i)
        if (!used[i])
            return false;
    return true;
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    for (int i = 1; i <= t; ++i) {
        cin >> n >> k;
        int sum = 0, maxVol = -1;
        for (int j = 0; j < n; ++j) {
            cin >> volume[j];
            sum += volume[j];
            maxVol = max(maxVol, volume[j]);
        }
        sort(volume, volume + n);
        int l = maxVol, r = sum;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (test(mid))
                r = mid - 1;
            else l = mid + 1;
        }
        cout << "Case #" << i << ": " << l << "\n";
    }
    fflush(stdout);
}

AC Code

#include <bits/stdc++.h>
 
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d", &a, &b)
#define siii(a, b, c) scanf("%d", &a, &c)
#define siiii(a, b, c, d) scanf("%d", &a, &c, &d)
#define pi(a) printf("%d\n", a)
#define pii(a, b) printf("%d %d\n", a, b)
#define piii(a, b, c) printf("%d %d% d\n", a, b, c)
#define piiii(a, b, c, d) printf("%d %d %d %d\n", a, b, c, d)
#define forRange(i, j, k) for(int i = j; i < k; ++i)
#define mem(a, b) memset(a, b, sizeof(a))
#define mCopy(to, from) memcpy(to, from, sizeof(from))
#define testData(f) freopen(f,"r",stdin)
using namespace std;
const int maxn = 1005;
const int INF = 0x3f3f3f3f;
int n, k;
int volume[maxn];
bool used[maxn];
 
bool test(int boxVol) {
    mem(used, 0);
    int currBox = 1, currVolLeft = boxVol;
    while (currBox <= k) {
        for (int i = n - 1; i >= 0; --i) {
            if (!used[i]) {
                if (volume[i] <= currVolLeft) {
                    currVolLeft -= volume[i];
                    used[i] = true;
                }
            }
        }
        ++currBox;
        currVolLeft = boxVol;
    }
    for (int i = 0; i < n; ++i)
        if (!used[i])
            return false;
    return true;
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    for (int i = 1; i <= t; ++i) {
        cin >> n >> k;
        int sum = 0, maxVol = -1;
        for (int j = 0; j < n; ++j) {
            cin >> volume[j];
            sum += volume[j];
            maxVol = max(maxVol, volume[j]);
        }
        sort(volume, volume + n);
        int l = static_cast<int>(ceil(sum / k)), r = static_cast<int>(ceil(sum / k) + maxVol);
        int ans = 1;
        for (int j = l; j <= r; ++j)
            if (test(j)) {
                ans = j;
                break;
            }
        cout << "Case #" << i << ": " << ans << "\n";
    }
    fflush(stdout);
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值