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.

Source

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<string>
using namespace std;
#define MAXN 100005
#define INF 0x3f3f3f3f
/*
将一个序列分为M段,使得这M段中最大的和 最小!
二分搜索
*/
int n, m, a[MAXN];
bool check(int x)
{
int tmp = ,cnt = ;
for (int i = ; i < n; i++)
{
if (tmp + a[i] <= x)
{
tmp += a[i];
}
else
{
tmp = a[i];
cnt++;
}
}
return (cnt <= m);
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
int beg = , end = , mid,ans;
for (int i = ; i < n; i++)
{
scanf("%d", &a[i]);
end += a[i];
beg = max(a[i], beg);
}
while (beg <= end)
{
mid = (beg + end) / ;
//cout << mid << endl;
if (check(mid))
{
ans = mid;
end = mid - ;
}
else
beg = mid + ;
}
printf("%d\n", ans);
}
}

Monthly Expense POJ 二分的更多相关文章

  1. [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)

    Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14158   Accepted: 5697 ...

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

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

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

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

  4. Monthly Expense(二分) 分类: 二分查找 2015-06-06 00:31 10人阅读 评论(0) 收藏

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

  5. poj 3273 Monthly Expense(贪心+二分)

    题目:http://poj.org/problem?id=3273 题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和. 思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max) ...

  6. POJ 3273 Monthly Expense 【二分答案】

    题意:给出n天的花费,需要将这n天的花费分成m组,使得每份的和尽量小,求出这个最小的和 看题目看了好久不懂题意,最后还是看了题解 二分答案,上界为这n天花费的总和,下界为这n天里面花费最多的那一天 如 ...

  7. Monthly Expense(二分--最小化最大值)

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

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

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

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

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

随机推荐

  1. mvn scope (转)

    策略一: 对于容器提供的(如:servlet-api-2.3等)和测试需要时的(如:junit-3.81等),可以直接在pom.xml中去掉. maven的dependency中有一个tag是< ...

  2. Linux命令补充及基础优化。

    1.用户部分 1.1 创建新用户 涉及命令 useradd [root@oldboyedu-50 ~]# useradd oldboy #添加用户 oldboy 1.2 设置密码 [root@oldb ...

  3. 解决UTF-8方法归纳

    1:通过spring配置过滤器解决 <!-- 配置Spring提供的字符编码过滤器 --> <filter> <filter-name>SpringCharacte ...

  4. sql简单学习---备忘

    2:Hibernate public boolean deleteFlowerById(int id) { Session session = sessionFactory.openSession() ...

  5. PHPExcel读取文件日期处理,含时分秒(Thinkphp)

    我们使用PHPExcel读取excel文件后发现,时间都是类似于这样的数字:41890.620138889,那么如何将它处理成我们想要的2014-09-08 14:53:00这样格式的日期呢,看代码: ...

  6. MySQL 批量插入值

    MySQL 批量插入值 今天遇到个问题,需要从类似以下语句中的各个小括号[其实也有中括号等]抽取相关字段进行组合[相关规则,此处略去不提],并保存到数据库中,此时如果每次插入一条,会连续进行多次插入, ...

  7. ACM_三角形蛇形矩阵

    三角形蛇形矩阵 Time Limit: 2000/1000ms (Java/Others) Problem Description: 小铠觉得各类题型是要温故而知新的,所以他叫小发出一道类似做过的题. ...

  8. JSP执行原理图

  9. 15年用canvas画的

    请恕我当年的工作太轻松,用canvas手打了一个图,技术含量并没有什么,现在看看,甚是怀念_(¦3」∠)_ <!DOCTYPE html> <html> <head&g ...

  10. 研磨JavaScript系列(五):奇妙的对象

    在JavaScript中,只有object和function两种东西有对象化的能力.我们先来说说函数的对象化能力. 任何一个函数都可以为其动态地添加或去除属性,这些属性可以是简单类型,可以是对象,也可 ...