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 ≤ 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组,每组必须是连续的几个数,求所有组的和的最大值的最小值。

思路:二分枚举答案。从low(n天中一天的最大花费,理论的最小值)到high(n天的总花费),寻找一个数,检查是否符合题意(以这个数为最大花费,所得最少组数小于m,且low等于high)。用二分就是加快寻找这个数的速度,例如在100内寻找一个数,用遍历的方法平均复杂度是50,用二分就是log2 100,范围越大,二分的速度优势更明显。

#include<iostream>
#include<cmath>
#include<stdio.h>
using namespace std;int n,m;
int a[100002];
bool find(int top){   //检查解是否在mid和low之间int num=1;int cur=0;for(int i=0;i<n;i++){if(cur+a[i]<=top)cur+=a[i];//归入同意组else{num++;    //新的一组cur=a[i];}}return (num<=m);
}int main()
{while(cin>>n>>m){int sum=0;int maxx=0;for(int i=0;i<n;i++){cin>>a[i];sum+=a[i];maxx=max(maxx,a[i]);}int low=maxx,high=sum,mid;//初始化上下界while(low!=high){mid=(high+low)/2;  //二分if(find(mid))high=mid;elselow=mid;}cout<<low<<endl;}return 0;
}

Monthly Expense (二分初级典例)相关推荐

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

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

  2. Monthly expense(二分)

    又又又又是二分啦 憨憨桃子又来啦 题目链接:monthly expense 题目描述: Farmer John is an astounding accounting wizard and has r ...

  3. POJ3273:Monthly Expense(二分)

    Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...

  4. BZOJ 1639: [Usaco2007 Mar]Monthly Expense 月度开支( 二分答案 )

    直接二分答案然后判断. ----------------------------------------------------------------------------- #include&l ...

  5. bzoj 1639: [Usaco2007 Mar]Monthly Expense 月度开支(二分)

    1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1057  Solve ...

  6. Monthly Expense(二分)

    Monthly Expense Farmer John is an astounding accounting wizard and has realized he might run out of ...

  7. Monthly Expense【二分】

    B - Monthly Expense POJ - 3273 Farmer John is an astounding accounting wizard and has realized he mi ...

  8. USACO Monthly Expense

    洛谷 P2884 [USACO07MAR]每月的费用Monthly Expense https://www.luogu.org/problem/P2884 POJ 3273 Monthly Expen ...

  9. 二分搜索 POJ 3273 Monthly Expense

    题目传送门 1 /* 2 题意:分成m个集合,使最大的集合值(求和)最小 3 二分搜索:二分集合大小,判断能否有m个集合. 4 */ 5 #include <cstdio> 6 #incl ...

最新文章

  1. 【OpenCV 4开发详解】边缘检测原理
  2. 电脑音频服务未运行怎么解决_电脑故障维修技巧教程:新手必看的修电脑技巧!...
  3. leetcode226. 翻转二叉树
  4. 字符串匹配之PabinKarp(模式匹配)
  5. 运行elasticsearch时报错:could not find java; set JAVA_HOME or ensure java is in PATH
  6. Mysql授权远程登陆
  7. 杭州公司java开发工程师常见面试问题
  8. UI自动化常见的等待方式
  9. 【金猿技术展】视频矫正技术——基于参数估计的自由几何变换算法
  10. ppt纯文字设计的几种玩法(词云、文字云)
  11. [安装fastfds中的nginx执行make命令报错]src/core/ngx_murmurhash.c:37:11: error
  12. 高等数学-线性代数:已知特征值,求解特征空间的特征向量
  13. 大数据行业常用的软件工具有哪些?
  14. idata 单片机 新唐_请教新唐单片机N76E003内存空间不足的问题
  15. spring开发_邮箱注册_激活_获取验证码
  16. debian linux win7,win7debian双系统
  17. ERP原理 : 第六节 采购管理的工作原理
  18. web上传整个文件夹
  19. 强大的Http监控工具Fidder
  20. 无需删除D盘文件,将D盘空间分给C盘

热门文章

  1. java happens before_Java并发编程之happens-before
  2. js根据ip地址获取省份城市的方法
  3. linux partprobe命令
  4. linux xvfb 中文乱码,在Linux上安装xvfb
  5. JavaScript设计模式(三):结构型设计模式-外观模式、适配器模式、代理模式、装饰者模式、桥接模式、组合模式、享元模式
  6. 即将逝去的Delphi前景在何方
  7. tf.py_func()函数
  8. python入门之字符串
  9. miui android 8.0 彩蛋,关于小米8探索版的13个隐藏彩蛋:只有真爱米粉看得懂
  10. 免费是敲门砖 2B应用还得靠产品和服务