1076 - Get the Containers

   PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

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 jth container, 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

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.

Output

For each case, print the case number and the desired result. See the samples for exact formatting.

Sample Input

Output for Sample Input


2

5 3

1 2 3 4 5

3 2

4 78 9


Case 1: 6

Case 2: 82

Note

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.

思路:二分答案;

和昨天那道一样还简单http://www.cnblogs.com/zzuli2sjy/p/5571396.html

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<string.h>
 5 #include<queue>
 6 #include<stack>
 7 #include<set>
 8 #include<math.h>
 9 using namespace std;
10 int ans[2000];
11 int uu[2000];
12 bool check(int k,int n,int m)
13 {
14         int i,j;
15         int sum=0;
16         int cnt=1;
17         for(i=0; i<n; i++)
18         {
19                         if(sum+ans[i]>k)
20                         {
21                                 uu[cnt-1]=sum;
22                                 sum=ans[i];
23                                 cnt++;
24                         }
25                         else if(sum+ans[i]<=k)
26                         {
27                                 sum+=ans[i];
28                         }
29         }uu[cnt-1]=sum;
30         if(m>=cnt)
31                 return true;
32         else return false;
33 }
34 int main(void)
35 {
36         int i,j,k;
37         int s;
38         scanf("%d",&k);
39         for(s=1; s<=k; s++)
40         {       memset(uu,0,sizeof(uu));
41                 int n;
42                 int m;
43                 int maxx=0;
44                 int  sum=0;
45                 scanf("%d %d",&n,&m);
46                 for(i=0; i<n; i++)
47                 {
48                         scanf("%d",&ans[i]);
49                         maxx=max(maxx,ans[i]);
50                         sum+=ans[i];
51                 }
52                 int l=maxx;
53                 int r=sum;
54                 int answer=-1;
55                 while(l<=r)
56                 {
57                         int mid=(l+r)/2;
58                         bool us=check(mid,n,m);
59                         if(us)
60                         {
61                                 answer=mid;
62                                 r=mid-1;
63                         }
64                         else l=mid+1;
65                 }
66                 printf("Case %d:",s);
67                 printf(" %d\n",answer);
68         }
69         return 0;
70 }
时间: 2024-10-10 14:39:41

1076 - Get the Containers的相关文章

LightOJ 1076 Get the Containers 二分答案

1076 - Get the Containers PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB 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' c

LightOj 1076 - Get the Containers (折半枚举 绝世好题)

题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1076 题目描述: 给出n个数,要求分成m段,问这m段中最大的总和,最小是多少? 解题思路: 二分每一段的长度,然后判定枚举长度是否合法. 刚看到这个题目就想到了dp,但是dp的复杂度还是很高的,然后就一直放着,放着....学了折半枚举,拿起来一看,哇塞,简直惊喜啊! 1 #include<cstdio> 2 #include<cstring> 3 #inclu

Jan&#39;s light oj 01--二分搜索篇

碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计数问题,利用二分直接枚举可能的结果,再检查是否符合题目要求. 4.区间求解,即利用两次二分分别查找有序序列左右上下限,再求差算出总个数. 题型知识补充: 1. 三分的一般写法: 1 double thfind(double left,double right) 2 { 3 double midmid

1076. Trash(KM算法 二分最佳完美匹配)

1076. Trash Time limit: 1.0 second Memory limit: 64 MB You were just hired as CEO of the local junkyard.One of your jobs is dealing with the incoming trash and sorting it for recycling.The trash comes every day in N containers and each of these conta

Learning docker--Chapter2--Handling Docker Containers

Content: (1)    Clarifying the Docker terms (2)    Working with the Docker images and containers (3)    The Docker Hub Registry (4)    Searching the Docker images (5)    Working with an interactive container (6)    Tracking the changes inside the con

uva 1076 - Password Suspects(AC自动机+记忆化搜索)

题目链接:uva 1076 - Password Suspects 题目大意:有一个长度为n的密码,存在m个子串,问说有多少种字符串满足,如果满足个数不大于42,按照字典序输出. 解题思路:根据子串构建AC自动机,然后记忆化搜索,dp[i][u][s]表示第i个字符,在u节点,匹配s个子串. #include <cstdio> #include <cstring> #include <queue> #include <string> #include <

Understand the Qt containers(有对应表)

Container classes are one of the cornerstones of object-oriented programming, invaluable tools that free us from having to permanently think about memory management. Qt comes with its own set of container classes, closely modeled after those in the S

URAL 1076 Trash Trash(最大权匹配)

Trash Time limit: 1.0 secondMemory limit: 64 MB You were just hired as CEO of the local junkyard.One of your jobs is dealing with the incoming trash and sorting it for recycling.The trash comes every day in N containers and each of these containers c

[转载]Vertica &ldquo;ERROR: Too many ROS containers exist&rdquo;

最近在用Vertica的时候碰到一个问题,Vertica在运行了一段时间后总是出现类似下面的错误 1: java.sql.SQLException: [Vertica][VJDBC](5065) 2: ERROR: Too many ROS containers exist for the following projections: 3: 4: <projection> (limit = 18078, ROS files = 12088, DV files = 5992, new files