Problem 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
 

#include<iostream>
#include<cstdio>
using namespace std;
int n,m;
int a[100010];
int fun(int s) //fun函数是推断当前的mid值能把n分成几组
{ //通过比較q与m的大小,对mid的值进行优化
int sum=0,q=1;
for(int i=1;i<=n;i++)//从第一天開始向下遍历每天的花费
{
if(sum+a[i]<=s)//前第I天和<=mid时,把他们归为这一组
sum+=a[i];
else//否则。第i天做为下一组的第一天
{
sum=a[i];
q++;//组数加一
}
}
if(q>m)//组数大于m,代表mid数值小了,
return 0;
return 1;//否则大了
}
int main()
{
while(~scanf("%d%d",&n,&m)){
int l,r=0,mid,min=0,sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
if(a[i]>r)r=a[i];
}
l=sum;
int ans=0;
mid=(l+r)/2;
while(r<l)
{
if(!fun(mid))//假设mid偏小
{ r=mid+1;//下界上升
}
else//否则
{
l=mid-1;//上限下降
}
mid=(r+l)/2;
}
cout<<mid<<endl;
}
return 0;
}

Monthly Expense的更多相关文章

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

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

  2. Monthly Expense(二分查找)

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

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

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

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

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

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

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

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

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

  7. 1639: [Usaco2007 Mar]Monthly Expense 月度开支

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

  8. POJ 3273 Monthly Expense(二分答案)

    Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...

  9. bzoj1639 / P2884 [USACO07MAR]每月的费用Monthly Expense

    P2884 [USACO07MAR]每月的费用Monthly Expense 二分经典题 二分每个段的限制花费,顺便统计下最大段 注意可以分空段 #include<iostream> #i ...

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

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

随机推荐

  1. java I/O库的设计模式

    在java语言 I/O库的设计中,使用了两个结构模式,即装饰模式和适配器模式.      在任何一种计算机语言中,输入/输出都是一个很重要的部分.与一般的计算机语言相比,java将输入/输出的功能和使 ...

  2. php 静态方法和非静态方法的调用说明

    1. php类中,静态方法调用当前类的非静态方法必须用self关键字,不能用$this 2. php类中,公有方法调用私有方法使用$this关键字,只能实例化调用 3. php类中,公有方法调用私有方 ...

  3. EntityFrameworkCore 中的 Attach 方法

    Attach 的坑 Model Filed Database Value Console Value User Phone +123000000000 +12333333333 User Email ...

  4. Java内部抛出异常外部不能catch问题分析

    今天在论坛看到一篇关于异常处理的文章,异常处理机制详解开头就搬出了这样一个例子: public class TestException { public TestException() { } boo ...

  5. iOS开发:父子控制器简介:

    #import "ViewController.h" #import "ScoietyViewController.h" #import "HotVi ...

  6. iOS开发之Quarz2D:九:图形上下文矩阵操作

    #import "VCView.h" @implementation VCView - (void)drawRect:(CGRect)rect { // Drawing code ...

  7. css3背景透明文字不透明

    在 FF/Chrome 等较新的浏览器中可以使用css属性background-color的rgba轻松实现背景透明,而文字保持不透明.而IE6/7/8浏览器不支持rgba,只有使用IE的专属滤镜fi ...

  8. 前端js实现打印(导出)excel表格

    产品原型: 图片.png 功能需求:点击导出考勤表格按钮,会自动下载成Excel格式 图片.png 图片.png jsp页面代码: <div class="tools"> ...

  9. JasperReport html 导出

    In my last blog post I discussed about Generating jasper reports in different formats using json fil ...

  10. <Linux> Linux下一些常用命令(个人记录)

    mount -o loop XXXX  XXX ./XXX &   后台运行 rm -rf XXX  删除文件夹 rm -rf *  删除当前目录下的所有文件 包括文件夹 ps -ef | g ...