Monthly Expense解题报告

这是一个经典的二分方法求最大值最小问题;

原题如下:

Monthly Expense

Time Limit: 2000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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 nextN (1 ≤ N ≤ 100,000) days.

FJ wants to create a budget for a sequential set of exactly M (1 ≤ M ≤ N) 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

Hint

If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.
题目解析:题目需要特别注意的一点就是划分过程中不能改变原有的顺序。

建模方法:对给给出的n个数,划分为m块,使每一块中所有数的和的最大值最小,是经典的最大值最小问题。

代码和具体注释如下:

#include<iostream>
using namespace std;


int n; //天数
int m; //规定的分组数


/*判断用当前的mid值能把天数n分成几组*/
/*通过比较group与m的大小,对mid值进行优化*/




int money[100005];  //每天花费的金额


bool judge_group(int mid)
{
    int sum=0;
    int group=1;    //当前mid值能把n天分成的组数(初始把全部天数作为1组)


    for(int i=1;i<=n;i++)  //从第一天开始向下遍历每天的花费
        if(sum+money[i]<=mid)  //当前i天之和<=mid时,把他们归并到一组
            sum+=money[i];
        else               //若 前i-1天之和 加上第i天的花费 大于mid
        {
            sum=money[i];  //则把前i-1天作为一组,第i天作为下一组的第一天
            group++;    //此时划分的组数+1
        }


    if(group>m)
        return false;   //若利用mid值划分的组数比规定的组数要多,则说明mid值偏小
    else
        return true;    //否则mid值偏大
}


int main(void)
{
    while(cin>>n>>m)
    {


        int low=0;  ///下界
        int high=0; ///上界


        for(int i=1;i<=n;i++)
        {
            cin>>money[i];


            high+=money[i];   ///把所有天数的总花费作为上界high(相当于把n天都分作1组)
            if(low<money[i])
                low=money[i]; ///把n天中花费最多的那一天的花费作为下界low(相当于把n天分为n组)
        }                     ///那么要求的值必然在[low,high]范围内
        int mid=(low+high)/2;


        while(low<high)  ///可能在low==high之前,分组数就已经=m,但是mid并不是最优,因此要继续二分
        {
            if(!judge_group(mid))
                low=mid+1;     ///mid值偏小,下界前移
            else
                high=mid-1;    ///mid值偏大,上界后移


            mid=(low+high)/2;
        }


        cout<<mid<<endl;  //二分搜索最后得到的mid值必然是使得分组符合要求的最优值


    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值