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:
- 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.
- 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.
- 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 ",answer); 68 } 69 return 0; 70 }