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,求分成m段且每段和最小的和。
思路:可以贪心验证,这个最小和是单调的,可以二分搜索,代码如下:
const int maxm = ;
const int INF = 0x7fffffff; int n, buf[maxm], m; bool check(int d) {
int sum = , now = buf[];
for (int i = ; i < n; ++i) {
if(now + buf[i] <= d)
now += buf[i];
else {
now = buf[i];
++sum;
}
}
return sum <= m;
} int main() {
int l = -INF, r = , mid;
scanf("%d%d", &n, &m);
for(int i = ; i < n; ++i) {
scanf("%d", &buf[i]);
l = max(l, buf[i]);
r += buf[i];
}
while(l <= r) {
mid = (l + r) >> ;
if(check(mid))
r = mid - ;
else
l = mid + ;
}
printf("%d\n", l);
return ;
}

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. JDBC 获取自动生成的主键

    为什么需要获取自动生成的主键 例如:

  2. 前x个数据中至少有m个元素最小值与最大值之差不超过K

    题意 给一组数据,从左到右开始,寻找最小的x,使得第1个元素到第x个元素中,至少存在m个数据,最小值与最大值之差不超过K. INPUT 第一行是T,代表数据组数 每组数据的第一行是三个整数,n.m.k ...

  3. Ubuntu18 mongodb 离线安装

    环境 Ubuntu 18 + mongodb 4.0.10 1.下载版本所需库 https://www.mongodb.com/download-center/community https://re ...

  4. 11. 搭建一个完整的K8S集群

    11. 搭建一个完整的Kubernetes集群 1. kubectl的命令遵循分类的原则(重点) 语法1: kubectl 动作 类 具体的对象 例如: """ kube ...

  5. 新建文件的UID和GID

    默认情况下:新建文件的用户ID为操作当前文件进程的有效用户ID(参考以前文章),新建文件的组ID为操作当前文件的进程的有效组ID 特殊情况:当当前新建文件的目录的SET-GID位被设置时,那么新建文件 ...

  6. Python数据分析之Numpy操作大全

    从头到尾都是手码的,文中的所有示例也都是在Pycharm中运行过的,自己整理笔记的最大好处在于可以按照自己的思路来构建矿建,等到将来在需要的时候能够以最快的速度看懂并应用=_= 注:为方便表述,本章设 ...

  7. http协议学习 —— post请求方法提交application/x-www-form-urlencoded类型的数据格式

    先推荐一篇很不错的文章:https://imququ.com/post/four-ways-to-post-data-in-http.html 说一下,如果是自己编写底层,那么要注意了,不能只有提交数 ...

  8. 简单bat脚本

    hwf.bat: set GAP_HOME=%~dp0\.. ::copy "%JAVA_HOME%\bin\javaw.exe" "%JAVA_HOME%\bin\HW ...

  9. DOM的一些小总结

    HTML DOM的结构:可以把HTML看成一个个的节点. 节点: HTML DOM对象 --  方法和属性 一些DOM 对象方法 nodeName 属性 nodeValue 属性 nodeType:节 ...

  10. c#DDOS代码

    //在工程属性中设置"允许不安全代码"为true ?using System; using System.Net; using System.Net.Sockets; using ...