题干:

链接: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 viv_ivi​. 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≤T≤201 \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≤n,K≤10001 \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 v1v_1v1​, v2v_2v2​, …\ldots…, vnv_nvn​ (1≤v1,v2,…,vn≤10001 \leq v_1, v_2, \ldots, v_n \leq 10001≤v1​,v2​,…,vn​≤1000), where the i-th integer viv_ivi​ 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

输入

复制

1
5 3
1 2 3 4 5

输出

复制

Case #1: 5

题目大意:

你有n件行李,有k个箱子体积相同的箱子,遵循下面的规则将行李放进箱子里面,每次都取当前最大的可以放进箱子的行李放进箱子,如果该箱子放不进任何行李那么就换一个新的箱子再按照这一条规则进行放行李,请问箱子最小的体积是多少,可以放进所有行李。

解题报告:

打眼一看是二分,但是其实并没有单调性,大概这么理解:假设是这些行李,你如果箱子的体积减小一点,或许就能拿出来一个大行李,放进去更多的小行李。这样导致后面的箱子需要放的行李的序列和 不减小箱子体积的行李序列是完全不一样的了,所以答案肯定也会不同,所以不能二分。

这题的正解是我们可以确定一个下界和一个上界,并且这个界的范围不会很大,所以可以直接在这个上下界内暴力就好。

当然由于不具有二分性质的样例不好构造,我们可以近似认为这是一个具有二分性质的题目,只不过加点随机化的思想。注意这题如果先二分完了在最后进行随机数处理的话也过不去,需要在二分的过程中扩大搜索范围。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;int a[MAX];
int n,k;
bool ok(int x) {multiset<int> ms;int cnt = 0,res = x;for(int i = 1; i<=n; i++) ms.insert(a[i]);while(1) {if(ms.empty() || cnt == k) break;auto it = ms.upper_bound(res);if(it == ms.begin()) {res = x;cnt++;} else {--it;res -= *it;ms.erase(it);}}if(ms.empty()) return 1;else return 0;
}
int main() {int t,iCase=0;cin>>t;while(t--) {scanf("%d%d",&n,&k);for(int i=1; i<=n; i++) scanf("%d",a+i);int l = 1,r=1e6,ans=1e6,mid;while(l+20<=r) {mid=(l+r)>>1;int flag = 0;for(int i = mid; i<=mid+10; i++) {if(ok(i)) {flag = 1,r = i-1,ans=i;break;}}if(flag == 0) l = mid+1;}for(int i = max(0,ans-20); i<=ans; i++) {if(ok(i)) {printf("Case #%d: %d\n",++iCase,i);break;}}}return 0;
}

【2019牛客暑期多校训练营(第六场)- D】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. transformers理论解释
  2. 计算机维修工国家职业标准,计算机维修工国家职业标准.pdf
  3. swift 项目_如何对iOS项目进行静态分析
  4. Excel单元格里面提取或去掉某些字符
  5. Android之elevation实现阴影效果
  6. Linux 中使用 sort 指令分组排序详解
  7. php oci8 11,Linux下PHP5.2 Oracle客户端扩展(OCI8)安装
  8. 追踪盗窃12亿用户登录数据的网络犯罪团伙
  9. 读取MySQL二进制文件_MYSQL: mysqlbinlog读取二进制文件报错read_log_event()
  10. html的table的子节点,HTMLTableElement子节点并不如预期
  11. vim插件管理工具pathogen
  12. python 判断回文数
  13. 【SQL】经典50题 [ 3 ]:Q31-Q50
  14. c语言霍夫变换圆检测,Hough Transform(霍夫变换)检测Circle(圆)的几种方法
  15. Tableau基础 Tableau 数据集的使用
  16. mysql中的binlog用法
  17. 【数学模型】层次分析
  18. 博通Broadcom SDK源码学习与开发4——ECOS系统数据流
  19. 心理服务OA系统——让心理服务机构管理工作一站到位【心悦灵】
  20. 数据库建表原则,SQL数据库建表前期优化,SQL数据库操作优化,数据库命名规范...

热门文章

  1. 算法十——深度优先搜索和广度优先搜索
  2. [Leetcode][第841题][JAVA][钥匙和房间][DFS][BFS]
  3. [小技巧][JAVA][转换]List, Integer[], int[]的相互转换
  4. [剑指offer]面试题第[50]题[JAVA][第一个只出现一次的字符][哈希表][HashMap]
  5. c语言获取dll文件路径,C语言URLDownloadToFile获取文件下载进度
  6. java gb13000 ucs2_采用GB 13000的UCS-2进行存储的文件怎么转换
  7. mysql分库一致性_分库分表带来的完整性和一致性问题
  8. python 隐藏命令行窗口_python如何只执行cmd中的动作,但消除或隐藏cmd窗口 - 小众知识...
  9. STM32 MDK编译后生成的 .map文件深入分析
  10. 真实AIS数据,解码,可视化