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 二分答案