lightoj 1076 - Get the Containers 【二分 + 贪心】

题目链接: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.

题意:将n个数分成连续的m组,最小化这m组里面最大组的和。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <iostream>
#include <cmath>
#include <queue>
#include <stack>
#define ll o<<1
#define rr o<<1|1
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e3 + 10;
int sum[MAXN];
int n, m;
bool judge(int o) {
    int ans = 0; sum[n+1] = INF;
    for(int i = 0; i < n; ) {
        int W = o + sum[i] + 1; ans++;
        int j = lower_bound(sum+i, sum+n+1, W) - sum;
        if(j == i + 1) return false;
        i = j - 1;
    }
    return ans <= m;
}
int main()
{
    int t, kcase = 1; scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &m); sum[0] = 0;
        for(int i = 1; i <= n; i++) {
            int v; scanf("%d", &v);
            sum[i] = sum[i-1] + v;
        }
        int l = 1, r = n * 1000000;
        int ans;
        while(r >= l) {
            int mid = (l + r) >> 1;
            if(judge(mid)) {
                ans = mid;
                r = mid - 1;
            }
            else {
                l = mid + 1;
            }
        }
        printf("Case %d: %d\n", kcase++, ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值