Lightoj 1076-get The Containers (two-point approximation)

Source: Internet
Author: User

1076 - Get the Containers


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


Output for Sample Input
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.

SPECIAL THANKS: JANE ALAM JAN (SOLUTION, DATASET)
Developed and Maintained by 
JANE ALAM JAN
Copyright © 2012 
LightOJ, Jane Alam Jan

Subject summary: n bottles of milk, put in a container for transportation, the number of times of transportation must not be more than m, each bottle of milk must be poured into the same container, and the milk must be transported in the order of the milk. Find the minimum capacity of the container

Problem analysis: Divide the capacity directly and find the minimum value that meets the conditions



code show as below:

#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 10009
using namespace std;
const int inf = 1e9;
const int mod = 1<<30;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef long long LL;
int a[N];
 
int main()
{
    int t, i, j, m, n, cnt = 0;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &n, &m);
        int l, r; l = r = 0;
        for(i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            l = max(l, a[i]);
            r += a[i];
        }
        int k, now, md;
        while(l <= r)
        {
            k = 1; now = 0;
            md = (l + r) >> 1;
            for(j = 1; j <= n; j++)
            {
                if(now + a[j] > md) k++, now = a[j];
                else now += a[j];
            }
            if(k <= m) r = md - 1;
            else l = md + 1;
        }
        printf("Case %d: %d\n", ++cnt, l);
    }
    return 0;
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.