前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ 3273 | Monthly Expense 农场的窘境(经典二分)

POJ 3273 | Monthly Expense 农场的窘境(经典二分)

作者头像
ACM算法日常
发布2018-08-07 16:56:23
5590
发布2018-08-07 16:56:23
举报
文章被收录于专栏:ACM算法日常ACM算法日常

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.

农夫John是一个会计巫师,他发现自己快没钱运营农场了。他已经计算好了接下来的N天里每天需要的花费。

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.

John想创建一个连续的M天预算,这些M天称之为fajomonths。每一个fajomonths包含1天以上的连续日子。每一天都包含在一个fajomonth里。John的目标是求出这些fajomonths中最大的fajomonth。这样来确定他每个月的开销限制。(翻译有点拗口,见后面得题解)

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

第一行输入N和M

接下来N行表示第i天的支出

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天中每天的花费,要求把这n天分成连续的m组,要求各组的花费之和应该尽可能地小,最后输出各组花费之和中的最大值。

思路:

经典的二分+检验题目,找到答案所在的区间【left,right】,然后二分区间mid=(left+right)/2,然后用judge_group()函数检验mid是否符合题目要求,时间复杂度log(n)。

把所有天数的总花费作为上界high(相当于把n天都分作1组),把n天中花费最多的那一天的花费作为下界low(相当于把n天分为n组),那么要求的值必然在[low,high]范围内。

代码语言:javascript
复制
 1#include<iostream>
 2using namespace std;
 3
 4int n; //天数
 5int m; //规定的分组数
 6
 7/*判断用当前的mid值能把天数n分成几组*/
 8/*通过比较group与m的大小,对mid值进行优化*/
 9
10bool judge_group(int mid,int money[])
11{
12    int sum=0;
13    int group=1;    //当前mid值能把n天分成的组数(初始把全部天数作为1组)
14
15    for(int i=1;i<=n;i++)  //从第一天开始向下遍历每天的花费
16        if(sum+money[i]<=mid)  //当前i天之和<=mid时,把他们归并到一组
17            sum+=money[i];
18        else               //若 前i-1天之和 加上第i天的花费 大于mid
19        {
20            sum=money[i];  //则把前i-1天作为一组,第i天作为下一组的第一天
21            group++;    //此时划分的组数+1
22        }
23
24    if(group>m)
25        return false;   //若利用mid值划分的组数比规定的组数要多,则说明mid值偏小
26    else
27        return true;    //否则mid值偏大
28}
29
30int main(void)
31{
32    while(cin>>n>>m)
33    {
34        int* money=new int[n+1];  //每天花费的金额
35        int low=0;  //下界
36        int high=0; //上界
37
38        for(int i=1;i<=n;i++)
39        {
40            cin>>money[i];
41
42            high+=money[i];   //把所有天数的总花费作为上界high(相当于把n天都分作1组)
43            if(low<money[i])
44                low=money[i]; //把n天中花费最多的那一天的花费作为下界low(相当于把n天分为n组)
45        }                     //那么要求的值必然在[low,high]范围内
46
47        int mid=(low+high)/2;
48
49        while(low<high)  //可能在low==high之前,分组数就已经=m,但是mid并不是最优,因此要继续二分
50        {
51            if(!judge_group(mid,money))
52                low=mid+1;     //mid值偏小,下界前移
53            else
54                high=mid-1;    //mid值偏大,上界后移
55
56            mid=(low+high)/2;
57        }
58
59        cout<<mid<<endl;  //二分搜索最后得到的mid值必然是使得分组符合要求的最优值
60
61        delete money;
62    }
63    return 0;
64}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-07-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 ACM算法日常 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档