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

  1. 7 5
  2. 100
  3. 400
  4. 300
  5. 100
  6. 500
  7. 101
  8. 400

Sample Output

  1. 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,求分成m段且每段和最小的和。
思路:可以贪心验证,这个最小和是单调的,可以二分搜索,代码如下:
  1. const int maxm = ;
  2. const int INF = 0x7fffffff;
  3.  
  4. int n, buf[maxm], m;
  5.  
  6. bool check(int d) {
  7. int sum = , now = buf[];
  8. for (int i = ; i < n; ++i) {
  9. if(now + buf[i] <= d)
  10. now += buf[i];
  11. else {
  12. now = buf[i];
  13. ++sum;
  14. }
  15. }
  16. return sum <= m;
  17. }
  18.  
  19. int main() {
  20. int l = -INF, r = , mid;
  21. scanf("%d%d", &n, &m);
  22. for(int i = ; i < n; ++i) {
  23. scanf("%d", &buf[i]);
  24. l = max(l, buf[i]);
  25. r += buf[i];
  26. }
  27. while(l <= r) {
  28. mid = (l + r) >> ;
  29. if(check(mid))
  30. r = mid - ;
  31. else
  32. l = mid + ;
  33. }
  34. printf("%d\n", l);
  35. return ;
  36. }

Day3-N - Monthly Expense POJ3273的更多相关文章

  1. POJ3273 Monthly Expense 2017-05-11 18:02 30人阅读 评论(0) 收藏

    Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25959   Accepted: 10021 ...

  2. POJ-3273 Monthly Expense (最大值最小化问题)

    /* Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10757 Accepted: 4390 D ...

  3. POJ3273 Monthly Expense —— 二分

    题目链接:http://poj.org/problem?id=3273   Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Tota ...

  4. 【POJ - 3273】Monthly Expense (二分)

    Monthly Expense 直接上中文 Descriptions 给你一个长度为N的序列,现在要让你把他们切割成M份(所以每一份都是连续的),然后每一份都有一个和sum[i],其中最大的一个是ma ...

  5. Monthly Expense(最大值最小化问题)

                                                                                POJ-3273                 ...

  6. Divide and Conquer:Monthly Expense(POJ 3273)

    Monthly Expense 题目大意:不废话,最小化最大值 还是直接套模板,不过这次要注意,是最小化最大值,而不是最大化最小值,判断的时候要注意 联动3258 #include <iostr ...

  7. Monthly Expense(二分查找)

    Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...

  8. BZOJ1639: [Usaco2007 Mar]Monthly Expense 月度开支

    1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 529  Solved: ...

  9. POJ 3273 Monthly Expense(二分查找+边界条件)

    POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...

随机推荐

  1. BGP前缀过滤(正则表达式)

    BGP的正则表达式一般用在as-path中,常用的如下: .(点):表示匹配任意一个字符,包括空格. *:表示匹配零个或多个模式的出现.即前一个字符出现0次或多次. +:表示匹配一个或多个模式的出现. ...

  2. Nexus-配置vPC 实验三

    配置EvPC(增强的vPC),下面两个FEX可以同时被两个N5K管理.注意:FEX只支持静态的Channel-group(mode on) N5K-1配置:配置FEXN5K-1(config)#fea ...

  3. Nexus-vPC和STP BPDU

    1.为了交互vPC拓扑,STP机制被修改适应到vPC peer环境.2.对于vPC ports,只有主角色运行STP,换句话说,vPC下的STP由主角色设备控制.3.只有主角色设备在DP(指定端口)上 ...

  4. Python学习第二十二课——Mysql 表记录的一些基本操作 (增删改)

    记录基本操作: 增:(insert into) 基本语法: insert into 表名(字段) values(对应字段的值): 例子1: insert into employee(id,name,a ...

  5. 【SSM 验证码】登录验证码

    LoginController /** * 登陆方法 */ @ResponseBody @RequestMapping("login2") public Map<String ...

  6. LoRa基础知识

    摘自:LoRaWAN介绍 - LoRa从业者读这篇就够了 https://blog.csdn.net/iotisan/article/details/69939241    LoRa网络结构      ...

  7. windows与linux的文件路径

    在windows操作系统中,文件路径的分隔符是反斜杠(“\\”),例如: E:\\hsta\\pdf(这里为防止转义,所以要写成两个反斜杠) 但是在linux操作系统中,文件的分隔符是斜杠(“/”), ...

  8. 牛客网Sql

    牛客网Sql: 1.查询最晚入职的员工信息  select * from employees where hire_date =(select max(hire_date) from employee ...

  9. (踩过的坑)使用Github Page搭建个人博客

    最近需要搭建一个网站,作为导航网址,但是自己的域名备案还要等上几天,就想着有没有别的办法来搭建一个公网可以访问的网站. Github Page的话是一个github个人主页,完全适合用来搭建普通网站. ...

  10. Kubernetes集群部署及简单命令行操作

    三个阶段部署docker:https://www.cnblogs.com/rdchenxi/p/10381631.html 环境准备 [root@master ~]# hostnamectl set- ...