Get the Containers
来源:互联网 发布:java需求分析 编辑:程序博客网 时间:2024/06/03 20:48
点击打开链接
A conveyor belt has a number of vessels of different capacities each filled to brim with milk. The milk from conveyor belt is to be filled into 'm' containers. The constraints are:
1. Whenever milk from a vessel is poured into a container, the milk in the vessel must be completely poured into that container only. That is milk from same vessel cannot be poured into different containers.
2. The milk from the vessel must be poured into the container in order which they appear in the conveyor belt. That is, you cannot randomly pick up a vessel from the conveyor belt and fill the container.
3. The ith container must be filled with milk only from those vessels that appear earlier to those that fill jthcontainer, for all i < j.
Given the number of containers m, you have to fill the containers with milk from all the vessels, without leaving any milk in the vessel. The containers need not necessarily have same capacity. You are given the liberty to assign any possible capacities to them. Your job is to find out the minimal possible capacity of the container which has maximal capacity.
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains two integers n (1 ≤ n ≤ 1000), the number of vessels in the conveyor belt and then m (1 ≤ m ≤ 106), which specifies the number of containers to which you have to transfer the milk. The next line contains the capacity c (1 ≤ c ≤ 106) of each vessel in order which they appear in the conveyor belt. Note that, milk is filled to the brim of any vessel. So the capacity of the vessel is equal to the amount of milk in it.
For each case, print the case number and the desired result. See the samples for exact formatting.
2
5 3
1 2 3 4 5
3 2
4 78 9
Case 1: 6
Case 2: 82
For the first case, the capacities of the three containers be 6, 4 and 5. So, we can pour milk from the first three vessels to the first container and the rest in other two containers. So, the maximum capacity of the container is 6. Suppose the capacities of the containers be 3, 7 and 5. Then we can also pour the milk, however, the maximum capacity is 7. As we want to find the result, where the maximum capacity is as low as possible; the result is 6.
题解:分别以最大牛奶容量,所有牛奶容量之和为初始left,right.判断以mid容量装牛奶是否<=m.
#include<cstdio>long long a[1005],left,right,mid,ans;int m,n;bool cmp(int k){int i,g,j=n;g=k-a[0];for(i=1;i<n;i++){g-=a[i];if(g>=0) j--;else g=k-a[i];}if(j<=m) return true;else return false;}int main(){int t,i,k=1;scanf("%d",&t);while(t--){long long sum=0;left=0;scanf("%d %d",&n,&m);for(i=0;i<n;i++){ scanf("%lld",&a[i]); sum+=a[i]; if(a[i]>left) left=a[i]; } ans=0; right=sum; while(left<=right) { mid=(left+right)/2; if(cmp(mid)) { ans=mid; right=mid-1; }else left=mid+1;}printf("Case %d: %lld\n",k++,ans);}return 0;}
- Get the Containers
- Get the Containers
- LightOJ 1076 - Get the Containers
- 【Light】[1076 ]Get the Containers
- LightOJ 1076 Get the Containers
- lightoj1076 Get the Containers 二分
- LightOJ - 1076 Get the Containers(二分)
- lightoj 1076 - Get the Containers 二分答案
- lightoj 1076 - Get the Containers 【二分 + 贪心】
- light oj 1076 - Get the Containers
- LightOJ - 1076 Get the Containers (二分)模板题
- LightOJ--1076--Get the Containers(二分好题)
- LightOJ 1076 Get the Containers(二分经典)
- LIghtOJ 1076 - Get the Containers(二分逼近)
- light1076Get the Containers
- UVa 11413 - Fill the Containers
- Containers
- The Standard Containers as Class Templates
- 获取当天的日期 判断当天为星期几
- 认知计算与深度学习
- 成员变量 局部变量 和类变量的区别
- servlet中的监听器深入
- java编码规则和运算符
- Get the Containers
- dinic
- 数组优缺点总结
- Spark中cache和persist的作用以及存储级别
- 重载比较函数 的用法
- ThinkPHP5 批量注册路由
- 11149 Power of Matrix(矩阵快速幂)
- 停车管理C语言站和队列
- 并发编程--线程池拒绝策略RejectedExecutionHandler(三)