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;
    }


  • 相关阅读:
    SQL Server 创建触发器(trigger)
    jQuery插件-json2.js
    Opengl创建机器人手臂代码示例
    OpenGL超级宝典完整源码(第五版)
    基于Opengl的太阳系动画实现
    Opengl创建几何实体——四棱锥和立方体
    ubuntu16.04安装labelme
    Visual Studio Command Prompt 工具配置方法
    OpenNi安装示例
    Opencv读取图片像素值并保存为txt文件
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770822.html
Copyright © 2011-2022 走看看