zoukankan      html  css  js  c++  java
  • ZZULIOJ 1919: D【二分】

    1919: D

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 187  Solved: 42

    Description

    晴天想把一个包含n个整数的序列a分成连续的若干段,且和最大的一段的值最小,但他有强迫症,分的段数不能超过m段,然后他就不会分了。。。他想问你这个分出来的和最大的一段的和最小值是多少?

    Input

    第一行输入一个整数t,代表有t组测试数据。
    每组数据第一行为两个整数n,m分别代表序列的长度和最多可分的段数。
    接下来一行包含n个整数表示序列。
    0<=n<=50000 1<=m<=n,0<=ai<=10000。

    Output

    输出一个整数表示和最大的一段的最小值。

    Sample Input

    1
    3 2
    1 3 5

    Sample Output

    5

    HINT

    1 3 5 分成一段可以为1 3 5和为9,分成两段可以为1,3 5或者1 3,5,和最大的一段值分别为8,5,所以答案为5

    没看见连续,我还一直在用尺取法进行二分判断。。。。。。。。。。。。

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define space " "
    using namespace std;
    //typedef __int64 Int;
    //typedef long long Long;
    const int INF = 0x3f3f3f3f;
    const int MAXN = 50000 + 10;
    const double Pi = acos(-1.0);
    const double ESP = 1e-5;
    int ar[MAXN], n, m;
    bool judge(int x) {
        int cnt = 0, s = 0;
        for (int i = 0; i < n; i++) {
            if (s + ar[i] > x) {
                cnt++; s = ar[i];
            }
            else s += ar[i];
        }
        if (s > 0) cnt++;
        return cnt <= m;
    }
    int main() {
        int t, sum, maxx;
        scanf("%d", &t);
        while (t--) {
            scanf("%d%d", &n, &m); sum = maxx = 0;
            for (int i = 0; i < n; i++) {
                scanf("%d", &ar[i]);
                sum += ar[i]; maxx = max(maxx, ar[i]);
            }
            int ub = sum, lb = maxx, ans;
            while (ub >= lb) {
                int mid = (ub + lb) >> 1;
                if (judge(mid)) {
                    ans = mid; ub = mid - 1;
                }
                else lb = mid + 1;
            }
            printf("%d
    ", ans);
        }
        return 0;
    }


  • 相关阅读:
    django中间件和常用模块
    django之forms组件
    django和ajax、分页器、批量插入数据
    django之模型层ORM操作
    (专题三)02-1 程序和程序设计流程-选择结构
    (专题三)01 程序和程序设计流程-顺序结构
    (专题二)05 矩阵的存储方式
    (专题二)04 矩阵的处理-矩阵的特征值
    [代码片段]YEAH!连通域标记和计数
    TTL和CMOS
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770822.html
Copyright © 2011-2022 走看看