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


  • 相关阅读:
    细说VMWare加入OpenStack
    云计算和大数据的崛起
    HTML5 Web app开发工具Kendo UI Web教程:如何配置Kendo UI Calendar
    Android:增强目录选择器对话框
    Gartner指明2014年几大战略技术趋势
    Android开发人员终于在“app-构建控制台”中获得分析
    谷歌升级Android分析应用程序
    Android是Windows开发人员可选择的下一个平台
    Greek
    为什么质数有无穷多个
  • 原文地址:https://www.cnblogs.com/Hilda/p/2635869.html
Copyright © 2011-2022 走看看