zoukankan      html  css  js  c++  java
  • Monthly Expense(二分--最小化最大值)

    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

    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.
    题意:农场主john为了节省开支自己做了一个预算。他已经计算好了接下来几天每天要花的钱,他将这些天分为m个时间段,要求分得各组的花费之和应该尽可能地小,最后输出各组花费之和中的最大值。
    分析:这是一个最小化最大值的问题,二分枚举即可。
     1 #include <iostream>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<cstdio>
     5 #include<algorithm>
     6 using namespace std;
     7 typedef long long ll;
     8 const int MAXN=1e5+10;
     9 const int Inf=0x3f3f3f3f;
    10 int m,n;
    11 int str[MAXN];
    12 int solve(int mid)
    13 {
    14     int sum=0,cnt=1;
    15     for(int i=0;i<m;i++)
    16     {
    17         if(sum+str[i]<=mid)
    18         {
    19             sum+=str[i];
    20         }
    21         else
    22         {
    23             sum=str[i];
    24             cnt++;
    25         }
    26     }
    27     if(cnt>n)
    28         return 0;
    29     return 1;
    30 }
    31 int main()
    32 {
    33     while(scanf("%d%d",&m,&n)!=-1)
    34     { int left=0,right=0,mid;
    35     for(int i=0;i<m;i++)
    36     {
    37         scanf("%d",&str[i]);
    38         right+=str[i];
    39         left=max(left,str[i]);
    40     }
    41     while(left<=right)
    42     {
    43         mid=(right+left)/2;
    44         if(solve(mid))
    45         {
    46             right=mid-1;
    47         }
    48         else
    49         {
    50             left=mid+1;
    51         }
    52     }
    53     printf("%d
    ",mid);}
    54     return 0;
    55 }
  • 相关阅读:
    [书籍精读]《JavaScript异步编程》精读笔记分享
    [技术翻译]在现代JavaScript中编写异步任务
    [技术翻译]Web网页内容是如何影响电池使用寿命的?
    [技术翻译]使用Nuxt生成静态网站
    [Vue源码]一起来学Vue模板编译原理(二)-AST生成Render字符串
    [Vue源码]一起来学Vue双向绑定原理-数据劫持和发布订阅
    [Vue源码]一起来学Vue模板编译原理(一)-Template生成AST
    [技术翻译]您应该知道的13个有用的JavaScript数组技巧
    css清除默认样式
    [小技巧]让你的GridView支持IQueryable,并自动实现真分页
  • 原文地址:https://www.cnblogs.com/moomcake/p/9426856.html
Copyright © 2011-2022 走看看