Monthly Expense
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.
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
7 5
100
400
300
100
500
101
400
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的更多相关文章
- Divide and Conquer:Monthly Expense(POJ 3273)
Monthly Expense 题目大意:不废话,最小化最大值 还是直接套模板,不过这次要注意,是最小化最大值,而不是最大化最小值,判断的时候要注意 联动3258 #include <iostr ...
- Monthly Expense(二分查找)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...
- BZOJ1639: [Usaco2007 Mar]Monthly Expense 月度开支
1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 529 Solved: ...
- POJ 3273 Monthly Expense(二分查找+边界条件)
POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...
- [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14158 Accepted: 5697 ...
- BZOJ 1639: [Usaco2007 Mar]Monthly Expense 月度开支( 二分答案 )
直接二分答案然后判断. ----------------------------------------------------------------------------- #include&l ...
- 1639: [Usaco2007 Mar]Monthly Expense 月度开支
1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 593 Solved: ...
- POJ 3273 Monthly Expense(二分答案)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...
- bzoj1639 / P2884 [USACO07MAR]每月的费用Monthly Expense
P2884 [USACO07MAR]每月的费用Monthly Expense 二分经典题 二分每个段的限制花费,顺便统计下最大段 注意可以分空段 #include<iostream> #i ...
- POJ3273 Monthly Expense 2017-05-11 18:02 30人阅读 评论(0) 收藏
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25959 Accepted: 10021 ...
随机推荐
- Mybatis全面详解——上(学习总结)
原文地址:https://blog.csdn.net/ITITII/article/details/79969447 一.什么是Mybatis 这里借用官网的一句话介绍什么是mybatis:MyBat ...
- [Angular2 Form] Reactive form: valueChanges, update data model only when form is valid
For each formBuild, formControl, formGroup they all have 'valueChanges' prop, which is an Observable ...
- 对延时敏感的应用是否应该使用Docker?
在High Scalability上看到一篇文章 How Does The Use of Docker Effect Latency? .文章回答了一个问题 I keep hearing about ...
- 魔兽争霸war3心得体会(四):不死族vs人族1本火魔塔
QQ对战平台上玩随机的人特别多,为了应对对方的"出其不意",我最近一直用小狗去探路,小狗在家采集30个木头-摆放商店,就可以去探路了.主要有几个好处:知道对方的种族-出生点位-开局 ...
- jquery插件课程2 放大镜、多文件上传和在线编辑器插件如何使用
jquery插件课程2 放大镜.多文件上传和在线编辑器插件如何使用 一.总结 一句话总结:插件使用真的还是比较简单的,引包,初始化,配置参数(json),配置数据(json),而后两步不是必须的.而且 ...
- android的edittext设置输入限制,只能输入数字
EditText的属性里面已经封装好了相关的设置,上一篇文章里面也提到了,不熟悉的可以去查看上一篇EditText属性大全,这里着重讲输入限制的属性: android:digits="123 ...
- xml传参
前端调用后端方法时要传递多个参数,在前端js中拼接xml形式的字符串: var args = "<?xml version='1.0' encoding='utf-8' ?>&q ...
- MD5工具类--可以直接拿来用不抛异常的MD5Util
public class MD5Util { private static String byteArrayToHexString(byte b[]) { StringBuffer resultSb ...
- 【oracle11g ,19】索引管理
一.索引的分类: 1.逻辑上分为: 单列索引和复合索引 唯一索引和非唯一索引 函数索引 domain索引 2.物理上分: 分区索引和非分区索引 b-tree bitmap 注意:表和索引最好 ...
- Html Vedio标签全屏
http://ask.csdn.net/questions/221701 http://www.zhangxinxu.com/wordpress/2010/03/every-browser-suppo ...