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设计模式》之抽象工厂模式

    场景问题 举个生活中常见的样例--组装电脑.我们在组装电脑的时候.通常须要选择一系列的配件,比方CPU.硬盘.内存.主板.电源.机箱等. 为讨论使用简单点.仅仅考虑选择CPU和主板的问题. 其实,在选 ...

  2. Tidhy

    JavaBean.hbm.xml(hibernate配置方面的): <?xml version="1.0" encoding="UTF-8"?> & ...

  3. golang 操作 Redis & Mysql & RabbitMQ

    golang 操作 Redis & Mysql & RabbitMQ Reids 安装导入 go get github.com/garyburd/redigo/redis import ...

  4. php实现把数组排成最小的数(核心是排序)(看别人的代码其实也没那么难)(把php代码也看一下)(implode("",$numbers);)(usort)

    php实现把数组排成最小的数(核心是排序)(看别人的代码其实也没那么难)(把php代码也看一下)(implode("",$numbers);)(usort) 一.总结 核心是排序 ...

  5. vs 2013 常用快捷键及常见问题的解决

    1. 代码编辑 关闭当前文档:ctrl + F4 打开光标所在位置的文档:ctrl + G(shift + g) 返回上次编辑的位置:ctrl + -(键盘数字键 0 后的那个按键) 移动光标所在的行 ...

  6. MySQL 监控-innotop

    innotop 编写者Balon Schwartz,<高性能MySQL>的作者之一. innotop的作用为实时地展示服务器正在发生的事情,监控innodb,监控多个MySQL实例,是一款 ...

  7. BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树

    题意:链接 方法:线段树 解析: 题意即题解. 多次询问区间最大值与最小值的差.显然直接上线段树或者rmq维护区间最值就可以. 代码: #include <cstdio> #include ...

  8. SqlBulkCopy 帮助类

    using System;using System.Collections.Generic;using System.Configuration;using System.Data;using Sys ...

  9. 【hdu 2955】Robberies

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  10. [React Router v4] Render Catch-All Routes with the Switch Component

    There are many cases where we will need a catch-all route in our web applications. This can include ...