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‘ 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.

题解

又是二分二分二分题。整数二分答案即可。题意是说,有一些等待灌装的牛奶,要装到一些容器里。一个罐子中的牛奶只能灌到一个容器里,不能一个罐子倒到两个容器。再有就是要求第i个容器里必须装第j个容器装的以前的罐子的牛奶,并且必须满足i < j。。。语文不好英语不好翻译得不行。。。。简单的说,不妨设第5个容器装了第6个罐子的牛奶,那么第4个容器只能从1 - 5这些罐子中选牛奶来灌装。然后就是给你n个罐子m个容器,分别告诉你这些罐子的牛奶容量,求这些容器的最小容量。

当然直接二分答案就行了,判断的时候就是如果容器的容量少了,那么就会造成期望使用的容器数cnt大于要求数m,这时候就要增加容量;反之减少。

代码示例

/****
	*@author    Shen
	*@title     LightOj 1076
	*/

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

const int maxN = 1005;

int a[maxN];
int t, tt;
int n, m;

inline bool test(int x)
{
	int sum = 0, cnt = 1;
	for (int i = 0; i < n; i++)
    {
		sum += a[i];
		if (sum > x)
			sum = a[i], cnt++;
	}
	return cnt > m;
}

int Bsearch(int l, int r)
{
    while (r > l)
    {
        int mid = (r + l) / 2;
        if (test(mid)) l = mid + 1;
        else r = mid;
    }
    return r;
}

void solve()
{
    scanf("%d %d", &n, &m);
    int l = 0, r = 0;
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        if (a[i] > l) l = a[i];
        r += a[i];
    }
    int res = Bsearch(l, r);
    printf("Case %d: %d\n", ++tt, res);
}

int main()
{
    scanf("%d", &t);
    while (t--) solve();
    return 0;
}

LightOJ 1076 Get the Containers 二分答案

时间: 2024-08-05 21:32:03

LightOJ 1076 Get the Containers 二分答案的相关文章

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

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 constrai

LightOJ 1088 Points in Segments 二分查找

1088 - Points in Segments PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given n points (1 dimensional) and q segments, you have to find the number of points that lie in each of the segments. A point pi will lie in a segme

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store

HDU3081Marriage Match II(二分答案+并查集+最大流SAP)经典

Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2507    Accepted Submission(s): 856 Problem Description Presumably, you all have known the question of stable marriage match. A

Codeforce 371C Hamburgers (二分答案)

题目链接 Hamburgers 二分答案,贪心判断即可. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define REP(i,n) for(int i(0); i < (n); ++i) 6 #define LL long long 7 8 char str[1010]; 9 LL len; 10 LL b, c, s, nb, nc, ns, pb, pc, ps; 11 LL money; 12 13 bool

POJ 3080 Blue Jeans(后缀数组+二分答案)

[题目链接] http://poj.org/problem?id=3080 [题目大意] 求k个串的最长公共子串,如果存在多个则输出字典序最小,如果长度小于3则判断查找失败. [题解] 将所有字符串通过拼接符拼成一个串,做一遍后缀数组,二分答案,对于二分所得值,将h数组大于这个值的相邻元素分为一组,判断组内元素是否覆盖全字典,是则答案成立,对于答案扫描sa,输出第一个扫描到的子串即可. [代码] #include <cstdio> #include <cstring> #inclu

【二分答案+智障的字符串hash】BZOJ2946-[Poi2000]公共串(Ranklist倒一达成!!!!!)【含hash知识点】

[题目大意] 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. [字符串hash的小笔记] hash[i]=(hash[i-1]*p+idx(s[i]))%mod,idx为映射值,一般a..z映射1..26: 习惯上,p取一个6到8位的素数即可,mod一般取大素数 1e9+7(1000000007)或1e9+9(1000000009). hash[i]=(hash[i-1]*p+idx(s[i]))%mod 表示第 i 个前缀的hash值,是一个hash的前缀和,那么,要求S[l…r]

IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) 二分答案 + 网络流

这道题的意思是给你一个有向图, 每条边上有一个最大载重量, 现在有x头牛要从顶点1走向顶点n, 每头牛要载的重量都是一样的, 问你最多能载多少的重量? 可以二分答案, 算出每头牛的载重, 然后修改边权, 跑一次最大流即可判断当前答案是否正确, 二分答案即可, 注意由于原始边权/每头牛的载重量可能会很大, 因此我们在修改边权时应该注意这一点,将边权的最大值控制在1000000之内, 防止溢出, 代码如下: #include <bits/stdc++.h> using namespace std;