链接: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);
}

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

  1. 【2019牛客暑期多校训练营(第二场) - H】Second Large Rectangle(单调栈,全1子矩阵变形)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/H 来源:牛客网 题目描述 Given a N×MN \times MN×M binary matrix. ...

  2. 2019牛客暑期多校训练营(第一场)E-ABBA(dp)

    链接:https://ac.nowcoder.com/acm/contest/881/E 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

  3. 2019牛客暑期多校训练营(第一场)

    传送门 参考资料: [1]:官方题解(提取码:t050 ) [2]:标程(提取码:rvxr ) [3]:牛客题解汇总 A.Equivalent Prefixes(单调栈) •题意 定义两个数组 u,v ...

  4. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  5. 【2019牛客暑期多校训练营(第二场)- E】MAZE(线段树优化dp,dp转矩阵乘法,线段树维护矩阵乘法)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/E?&headNav=acm 来源:牛客网 Given a maze with N rows an ...

  6. 【2019牛客暑期多校训练营(第二场)- F】Partition problem(dfs,均摊时间优化)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/F 来源:牛客网 Given 2N people, you need to assign each of ...

  7. 【2019牛客暑期多校训练营(第二场) - D】Kth Minimum Clique(bfs,tricks)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/D 来源:牛客网 Given a vertex-weighted graph with N vertice ...

  8. 【2019牛客暑期多校训练营(第一场) - A】Equivalent Prefixes(单调栈,tricks)

    题干: 链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Two arrays u and v each with m distinct elem ...

  9. 【2019牛客暑期多校训练营(第一场) - H】XOR(线性基,期望的线性性)

    题干: 链接:https://ac.nowcoder.com/acm/contest/881/H 来源:牛客网 Bobo has a set A of n integers a1,a2,-,ana1, ...

  10. 2019牛客暑期多校训练营(第九场)H Cutting Bamboos(主席树+二分)

    链接:https://ac.nowcoder.com/acm/contest/889/H 来源:牛客网 题目描述 There are n bamboos arranged in a line. The ...

最新文章

  1. Find Minimumd in Rotated Sorted Array
  2. CVPR2020人脸防伪检测挑战赛冠亚军论文解读(下篇)
  3. oracle 容器切换,oracle12c 多租户管理四(容器连接切换)
  4. 数字图像噪声_Python
  5. linux mxnet 编译,MXnet安装和编译
  6. Elasticsearch Curator使用
  7. Android网络课程笔记-----本地音乐播放
  8. 使用transforms.Compose套件做图像数据变换
  9. java vector实现的接口_java的List接口的实现类 ArrayList,LinkedList,Vector 的区别
  10. Object类解析(简)
  11. 2022_天勤数据结构高分笔记_第二章_算法
  12. 淘口令 java,抓包获取淘口令的解决方案
  13. 项目案例:乌龟吃鱼小游戏
  14. [乡土民间故事_徐苟三传奇]第十五回_拦县令写诗救难民
  15. android 小说下载器 源码 分享
  16. python文件操作和模块
  17. 硕士论文各章节的篇幅多少比较合适
  18. win10计算机无法复制文件,Win10系统禁止U盘拷贝文件的方法【图文】
  19. 区块链如何解决数据安全问题?
  20. 计算机信息处理员证书可以在东莞入户,东莞入户办理:人才入户东莞有哪些职称考?可靠吗?...

热门文章

  1. 百度搜索热点热搜怎么关闭?
  2. npm i 命令安装失败提示:npm WARN read-shrinkwrap,解决方法
  3. 蓝桥杯第八届省赛 电子钟 by YYC
  4. 爬取电影天堂的最新电影
  5. pgsql依赖性追踪
  6. Photoshop:如何使图片覆盖在文字上以及一种海报效果实现
  7. vm15安装mac10.14提取ipa包
  8. 【HotSpot、G1】垃圾回收算法和垃圾收集器
  9. 动态网站数据采集 - 时光网电影信息爬虫
  10. 《WebRTC 1.0: Real-Time Communication Between Browsers》学习