zoukankan      html  css  js  c++  java
  • POJ 3273 Monthly Expense【二分】

    Description

    Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.

    FJ wants to create a budget for a sequential set of exactly M (1 ≤ MN) fiscal periods called "fajomonths". Each of these fajomonths contains a set of 1 or more consecutive days. Every day is contained in exactly one fajomonth.

    FJ's goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.

    Input

    Line 1: Two space-separated integers: N and M
    Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day

    Output

    Line 1: The smallest possible monthly limit Farmer John can afford to live with.

    Sample Input

    7 5
    100
    400
    300
    100
    500
    101
    400

    Sample Output

    500

    题目大意:给出农夫在n天中每天的花费,要求把这n天分作m组,每组的天数必然是连续的,要求分得各组的花费之和应该尽可能地小,最后输出各组花费之和中的最大值
    思路:二分,标志性的字眼“最小中的最大值”, “最大之中的最小值”.
    很好的解题报告:http://user.qzone.qq.com/289065406/blog/1301655498
    代码如下:
    View Code
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    int n, m, f[100005];
    int judge(int mm)
    {
        int i, count=0, change=0;
        for(i=0; i<n; i++)
        {
            change+=f[i];
            if(change>mm)
            {
                count++;
                change=f[i];
            }
        }
        count++;
        if(count>m)
            return 1;
        return 0;
    }
    int main()
    {
        while(scanf("%d%d", &n, &m)!=EOF)
        {
            int i, sum=0, maxmon=0;
            memset(f, 0, sizeof(f));
            for(i=0; i<n; i++)
            {
                scanf("%d", &f[i]);
                sum+=f[i];
                maxmon=maxmon>f[i]?maxmon:f[i];
            }
            int left=maxmon, right=sum;
            while(left<right)
            {
                int mid=left+(right-left)/2;
                if(judge(mid))
                    left=mid+1;
                else
                    right=mid;
            }
            printf("%d\n", left);
        }
    }


  • 相关阅读:
    C# winform 打包成安装程序(exe)
    gitHub----【Mac】sourcetree连接github,报错:fatal:Authentication failed for'https://git…。或提示password required 解决方案
    python3-----往一个字符串中循环添加数据
    python3.8----从多层嵌套Json中解析所需要的值
    记录一些工作知识
    【转】彻底搞懂 async & defer
    【转】判断JS数据类型的四种方法
    动态表单设计
    封装,继承,多态
    语义化版本
  • 原文地址:https://www.cnblogs.com/Hilda/p/2635869.html
Copyright © 2011-2022 走看看