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.

#include<stdio.h>
#include<algorithm>
#include<math.h>
const int inf=1e8;
using namespace std;
int a[100005];
int m,n;
bool check(int d)
{int sum=0;int ans=0;for(int i=0;i<=m-1;)    {if(sum+a[i]<d){sum+=a[i];i++;}else if(sum+a[i]==d){ans++;sum=0;}else{ans++;sum=0;}}return ans>=n;
}
int main()
{int i; int s=0;   int max=0;scanf("%d%d",&m,&n);   for(i=0;i<m;i++){scanf("%d",&a[i]);s+=a[i];if(a[i]>=max)max=a[i];}int l=max; int r=s;int mid;mid=(l+r)/2;while(r-l>1){//  printf("%d,%d,%d\n",l,r,mid);if(check(mid)){l=mid;}else{r=mid;} mid=(l+r)/2;}printf("%d\n",l);}

Monthly Expense相关推荐

  1. 二分搜索 POJ 3273 Monthly Expense

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

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

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

  3. Monthly Expense【二分】

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

  4. bzoj1639[Usaco2007 Mar]Monthly Expense 月度开支*

    bzoj1639[Usaco2007 Mar]Monthly Expense 月度开支 题意: n天,每天有一个花费,现在要将它们分成连续的m段,要求所有段的总花费的最大值最小.求这个值.n,m≤10 ...

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

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

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

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

  7. USACO Monthly Expense

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

  8. Monthly expense(二分)

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

  9. Monthly Expense(二分)

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

  10. Monthly Expense POJ - 3273(二分最大值最小化)

    Farmer John is an astounding accounting wizard and has realized he might run out of money to run the ...

最新文章

  1. 用rand_7()实现rand_10()
  2. 小程序获取input 的value_[小发现] 现在可以获取任意小程序的小程序码了!
  3. python 生成html文件浏览器_python如何将数据生成html文件+浏览器中文显示乱码问题...
  4. 合并excel文件 C语言,再见Ctrl + C!合并100个Excel表格,只需30秒!
  5. rest_framework04:ModelSerializer/Serializer高级用法
  6. apache2+支持php7,Ubuntu14.04下配置PHP7.0+Apache2+Mysql5.7
  7. Java什么时候提高境界支持async/await写法啊?
  8. 在Mac系统下架设WiFi热点,对比Win7
  9. Netty的并发编程实践5:不要依赖线程优先级
  10. 【转】VC2008 配置OpenGL环境
  11. 双击事件 转载 http://blog.sina.com.cn/s/blog_739365a30100vk8p.html
  12. 计算机毕业设计最新选题汇总(持续更新)
  13. Caffe框架的基本操作和分析
  14. origin绘图同时添加柱状图和折线图
  15. P4199-FFT,manacher
  16. 基于微信小程序的网上订餐系统 报告+任务书+开题报告+文献综述+中期PPT+外文翻译及原文+PPT+项目源码及数据库文件
  17. 采用高德地图 实现打车功能代码
  18. 有感于Google退出中国
  19. ExcelPatternTool: Excel表格-数据库互导工具
  20. 微型计算机中奇偶校验,奇偶校验位

热门文章

  1. MATLAB 代码资料大全
  2. html中定义动画anima,css中animation怎么用
  3. 服务器装系统2t以上,华为RH2288 V3服务器安装Cent OS 7时容量大于2T导致系统安装失败...
  4. 【75】颜色分类--荷兰国旗问题
  5. 【CODEVS 3287】【NOIP2013】火车运输
  6. java取当前周期、月初至月末、季度初至季度末日期。
  7. 数据分析入门:初识数据埋点(一)
  8. 多线程知识点(详解)
  9. Caffe for Python 官方教程
  10. 网络状态显示小地球不要慌!