LightOJ - 1076 Get the Containers (二分)模板题

LightOJ - 1076
Get the Containers
Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

Submit Status

Description

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

2

5 3

1 2 3 4 5

3 2

4 78 9

Sample Output

Case 1: 6

Case 2: 82

Source

Special Thanks: Jane Alam Jan (Solution, Dataset)
//题意:输入一个n,m,接下来是n个数
表示有n瓶牛奶,现在要将这n瓶奶合并到m个瓶中,问要达到这个要求的最小的容量是多少?
//思路:
直接二分
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int a[1010];
int n,m;
int ans;
bool judge(int mid)
{
	int cnt=1;
	int k=0;
	for(int i=0;i<n;i++)
	{
		k+=a[i];
		if(k>mid)
		{
			cnt++;
			k=a[i];
		}
	}
	if(cnt<=m)
		return true;
	else
		return false;
}
int erfen(int l,int r)
{
	int mid,ans=0;
	while(l<=r)
	{
		mid=(l+r)/2;
		if(judge(mid))
		{
			ans=mid;
			r=mid-1;
		}
		else
			l=mid+1;
	}
	return ans;
}
int main()
{
	int t,T=1;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		int sum=0,mm=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			mm=max(mm,a[i]);
			sum+=a[i];
		}
		int ans=erfen(1,sum);
		ans=max(ans,mm);
		printf("Case %d: %d\n",T++,ans);
	}
	return 0;
}

 

你可能感兴趣的:(LightOJ - 1076 Get the Containers (二分)模板题)