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个时间段,要求分得各组的花费之和应该尽可能地小,最后输出各组花费之和中的最大值。
分析:这是一个最小化最大值的问题,二分枚举即可。

 #include <iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN=1e5+;
const int Inf=0x3f3f3f3f;
int m,n;
int str[MAXN];
int solve(int mid)
{
int sum=,cnt=;
for(int i=;i<m;i++)
{
if(sum+str[i]<=mid)
{
sum+=str[i];
}
else
{
sum=str[i];
cnt++;
}
}
if(cnt>n)
return ;
return ;
}
int main()
{
while(scanf("%d%d",&m,&n)!=-)
{ int left=,right=,mid;
for(int i=;i<m;i++)
{
scanf("%d",&str[i]);
right+=str[i];
left=max(left,str[i]);
}
while(left<=right)
{
mid=(right+left)/;
if(solve(mid))
{
right=mid-;
}
else
{
left=mid+;
}
}
printf("%d\n",mid);}
return ;
}

Monthly Expense(二分--最小化最大值)的更多相关文章

  1. OJ 21658::Monthly Expense(二分搜索+最小化最大值)

        Description Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的.他已经计算了他以后N(1<=N< ...

  2. poj 3273"Monthly Expense"(二分搜索+最小化最大值)

    传送门 https://www.cnblogs.com/violet-acmer/p/9793209.html 题意: 有 N 天,第 i 天会有 a[ i ] 的花费: 将这 N 天分成 M 份,每 ...

  3. POJ_3273_Monthly_Expense_(二分,最小化最大值)

    描述 http://poj.org/problem?id=3273 共n个月,给出每个月的开销.将n个月划分成m个时间段,求m个时间段中开销最大的时间段的最小开销值. Monthly Expense ...

  4. POJ_3104_Drying_(二分,最小化最大值)

    描述 http://poj.org/problem?id=3104 n件衣服,第i件衣服里面有水a[i],自然风干每分钟干1个水,用吹风机每分钟干k个水,但是同时只能对一件衣服使用吹风机,求干完所有衣 ...

  5. POJ 3273 Monthly Expense二分查找[最小化最大值问题]

    POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...

  6. poj 3273 Monthly Expense (二分搜索,最小化最大值)

    题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...

  7. POJ3273-Monthly Expense (最小化最大值)

    题目链接:cid=80117#problem/E">click here~~ [题目大意] 农夫JF在n天中每天的花费,要求把这n天分作m组.每组的天数必定是连续的.要求分得各组的花费 ...

  8. 【POJ 3273】 Monthly Expense (二分)

    [POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...

  9. [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)

    Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14158   Accepted: 5697 ...

随机推荐

  1. 【tensorflow:Google】二、Tensorflow环境搭建

    2.1 Tensorflow 主要依赖包 2.1.1 Protocol Buffer 结构化数据序列化的过程,另外的工具:XML, JSON, 区别:二进制(不可读):先定义数据格式,还原的时候将需要 ...

  2. 关于vsftpd.conf配置

    vsftpd配置文件详解   转自http://yuanbin.blog.51cto.com/363003/108262/   1.默认配置: 1>允许匿名用户和本地用户登陆.      ano ...

  3. Android中从SD卡中读取歌曲

    先看看我的效果图吧 Activity类 private TextView nameTextView; private SeekBar seekBar; private ListView listVie ...

  4. iOS 单元测试和UI测试教程

    原文:iOS Unit Testing and UI Testing Tutorial 作者:Audrey Tam 译者:kmyhy 编写测试不是为了追求刺激,测试是为了避免你崭新的 App 变成了充 ...

  5. Flux架构

    引言:本文的目标是通过了解Flux 提出的模式,来明白Flux 的核心要点,以及弄清楚它到底是什么.并且,由于Flux不是传统意义上的软件包,因此我们将仔细研究通过Flux 来解决设计思路上的问题. ...

  6. C++ Primer 第四版中文版

    C++Primer是C++的经典教程. 开始时间:2014-08-10 完成时间:2014-08-28 学习成果:基础语法+

  7. OneNote如何使用

    自从安装了Office2013后发现office套件中有很多的好东西,今天要和大家分享的就是Office套件中的OneNote软件,这狂软件能够很方便的记录我们生活中的一些学习资料.一些决绝方法的经验 ...

  8. Consul做服务发现

    使用Consul做服务发现的若干姿势 https://www.cnblogs.com/bossma/p/9756809.html 从2016年起就开始接触Consul,使用的主要目的就是做服务发现,后 ...

  9. 剑指offer-第五章优化时间和空间效率(在字符串中第一次出现切只出现一次的字符)

    题目:在字符串中第一次出现切只出现一次的字符 思路:用HashMap来存放对应的char值和该char出现的次数.做一次变量就可以得到第一个只出现一次的字符. Java代码: import java. ...

  10. codechef Graph on a Table

    codechef Graph on a Table https://www.codechef.com/problems/TBGRAPH 题意 : 一个\(n\times m\)的网格图.\(q\) 个 ...