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. 修改QList中的item(使用下标([index])才可以获得可修改的item的引用)

    QList算是最常用的集合了,今儿偶然间需要修改QList中的值,结果郁闷了.QList中提供了replace函数来替换item,但不是修改.而at().value()操作均返回的是const的ite ...

  2. linux下dd命令详解及应用实例

    名称: dd使用权限: 任何使用者dd 这个指令在 manual 里的定义是 convert and copy a file使用方式:dd [option]查看帮助说明dd --help或是info ...

  3. adobe-flash-player离线下载方法

    https://www.neowin.net/news/adobe-flash-player-3000134 http://fpdownload.adobe.com/get/flashplayer/p ...

  4. Spring-boot更改成war包的方式

    转载至:  https://blog.csdn.net/zhuwei_clark/article/details/82114102  Step1 修改启动类 Step2 修改配置文件为properti ...

  5. cocos2d-x win8下的环境配置和建立项目

    cocos2dx 跨平台.可是看网上说开发最合适还是在vs2010中,基本是编完后再移植到安卓. 1.去官网下载源代码2.2.3版本号的. 2.然后运行根文件夹下的build-win32.bat(须要 ...

  6. [Compose] 14. Build curried functions

    We see what it means to curry a function, then walk through several examples of curried functions an ...

  7. Multi-Tenancy模式,基础服务大规模扩张的时候,是应该推进了。

    这不是技术介绍.这是我要推进的工作,记在这里.服务的对象太多,必须隔离为不同租户了.

  8. 【t075】郁闷的记者

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 你是一个体育报社的记者,你接受到一个艰难的任务:有N支足球队参加足球比赛,现在给你一些比赛的结果,需要 ...

  9. [Ramda] Get Deeply Nested Properties Safely with Ramda's path and pathOr Functions

    In this lesson we'll see how Ramda's path and pathOr functions can be used to safely access a deeply ...

  10. linux网络编程学习笔记之二 -----错误异常处理和各种碎碎(更新中)

    errno 在unix系统中对大部分系统调用非正常返回时,通常返回值为-1.并设置全局变量errno(errno.h),如socket(), bind(), accept(), listen(). e ...