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.
 
#include<iostream>
#include<numeric>
#include<algorithm>
using namespace std;
int money[];
int n,m; bool C(int d){
int periods=,curSum=;
for(int i=;i<n;i++){
curSum+=money[i];
if(curSum>d){
curSum=money[i];
periods++;
}
if(periods>m)
return false;
}
return true;
} int main(){
while(cin>>n>>m){
for(int i=;i<n;i++){
cin>>money[i];
}
int lb=*max_element(money,money+n);
int ub=accumulate(money,money+n,);
int mid=(lb+ub)/;
while(ub>lb){
if(C(mid))
ub=mid-;
else
lb=mid+;
mid=(lb+ub)/;
}
cout<<mid<<endl; }
return ;
}

POJ3273--Monthly Expense(Binary Search)的更多相关文章

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

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

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

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

  3. POJ3273 Monthly Expense —— 二分

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

  4. poj3273 Monthly Expense(二分搜索)

    https://vjudge.net/problem/POJ-3273 认真审题,代码仔细!!ans的初值应该是1 #include<iostream> #include<cstdi ...

  5. POJ3273 Monthly Expense

    查看原题 边界,就是边界和思维,怎么有效的判断中间值是大了还是小了,以及准确的找到边界!一个<写成<=就前功尽弃,还特别难找到错误! #include <cstdio> #in ...

  6. POJ3273 Monthly Expense (二分最小化花费)

    链接:http://poj.org/problem?id=3273 题意:FJ想把n天分成m组,每组是连续的,同一组的花费加起来算,求所分组情况中最高花费的最低值 思路:二分答案.二分整数范围内的花费 ...

  7. Algo: Binary search

    二分查找的基本写法: #include <vector> #include <iostream> int binarySearch(std::vector<int> ...

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

                                                                                POJ-3273                 ...

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

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

随机推荐

  1. 11.2JS笔记

    1.为什么要面向对象:JS一开始就是写网页特效,面向过程,作者发现这样的写不好,代码重复利用率太高,计算机内存消耗太大,网页性能很差,所以作者就受到java和c语言的影响,往面向对象对齐,JS天生有一 ...

  2. opencv 3.2图像矩(Image Moments)

    图像矩乍看比较难理解,看了很多资料,大概明白了一些,但还是无法在脑海里形成一个模型概念,于是从源码中寻找它的应用. 今天就通过公式和程序抓取数据,来进一步理解图像矩 先看一个图片 这是程序运行结果, ...

  3. Oracle_PL/SQL(6) 触发器(序列、视图)

    序列1.创建序列create sequence seq_alog start with 1 increment by 1 maxvalue 999999999999999999999999999 mi ...

  4. Maven 系列 二 :Maven 常用命令,手动创建第一个 Maven 项目

    1.根据 Maven 的约定,我们在D盘根目录手动创建如下目录及文件结构: 2.打开 pom.xml 文件,添加如下内容: 1 <project xmlns="http://maven ...

  5. 同一个线程多次调用start()会出现的问题

    结果: 线程首先会运行一次,然后抛出java.lang.IllegalThreadStateException异常. 根据控制台的异常信息,定位到Thread.java的第708行,也就start() ...

  6. IDEA工具 常用的设置

    以下均为笔者常用的的设置. 首先选择菜单栏的选项:“File” ----> “settings” 1.字体设置 (1)代码区的字体设置 如下图的选择,"Primary font&quo ...

  7. 转录本组装软件StringTie的使用说明

    转录本组装软件StringTie的使用说明 StringTie 转录本组装软件StringTie的使用说明 转录组分析流程 HISTA + StringTie 组合.其Protocol 发表在Natu ...

  8. HISAT,sTRINGTIE,ballgown三款RNA-seq信息分析软件

    HISAT,sTRINGTIE,ballgown三款RNA-seq信息分析软件 2015年04月02日 11:35:47 夜丘 阅读数:8940 标签: 生物 更多 个人分类: 论文笔记   Bowt ...

  9. BZOJ4033或洛谷3177 [HAOI2015]树上染色

    BZOJ原题链接 洛谷原题链接 很明显的树形\(DP\). 因为记录每个点的贡献很难,所以我们可以统计每条边的贡献. 对于每一条边,设边一侧的黑点有\(B_x\)个,白点有\(W_x\),另一侧黑点有 ...

  10. memcache的add和set区别

    add可以做memcache锁 使用场景:用户兑换商品,在网络不好的情况下,点击多次,set会将多次提交全纪录下来,add只会记录一次