POJ3273--Monthly Expense(Binary Search)
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
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Sample Input
7 5
100
400
300
100
500
101
400
Sample Output
500
Hint
#include<iostream>
#include<numeric>
#include<algorithm>
using namespace std;
int money[];
int n,m; bool C(int d){
int periods=,curSum=;
for(int i=;i<n;i++){
curSum+=money[i];
if(curSum>d){
curSum=money[i];
periods++;
}
if(periods>m)
return false;
}
return true;
} int main(){
while(cin>>n>>m){
for(int i=;i<n;i++){
cin>>money[i];
}
int lb=*max_element(money,money+n);
int ub=accumulate(money,money+n,);
int mid=(lb+ub)/;
while(ub>lb){
if(C(mid))
ub=mid-;
else
lb=mid+;
mid=(lb+ub)/;
}
cout<<mid<<endl; }
return ;
}
POJ3273--Monthly Expense(Binary Search)的更多相关文章
- POJ-3273 Monthly Expense (最大值最小化问题)
/* Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10757 Accepted: 4390 D ...
- POJ3273 Monthly Expense 2017-05-11 18:02 30人阅读 评论(0) 收藏
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25959 Accepted: 10021 ...
- POJ3273 Monthly Expense —— 二分
题目链接:http://poj.org/problem?id=3273 Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Tota ...
- poj3273 Monthly Expense(二分搜索)
https://vjudge.net/problem/POJ-3273 认真审题,代码仔细!!ans的初值应该是1 #include<iostream> #include<cstdi ...
- POJ3273 Monthly Expense
查看原题 边界,就是边界和思维,怎么有效的判断中间值是大了还是小了,以及准确的找到边界!一个<写成<=就前功尽弃,还特别难找到错误! #include <cstdio> #in ...
- POJ3273 Monthly Expense (二分最小化花费)
链接:http://poj.org/problem?id=3273 题意:FJ想把n天分成m组,每组是连续的,同一组的花费加起来算,求所分组情况中最高花费的最低值 思路:二分答案.二分整数范围内的花费 ...
- Algo: Binary search
二分查找的基本写法: #include <vector> #include <iostream> int binarySearch(std::vector<int> ...
- Monthly Expense(最大值最小化问题)
POJ-3273 ...
- Divide and Conquer:Monthly Expense(POJ 3273)
Monthly Expense 题目大意:不废话,最小化最大值 还是直接套模板,不过这次要注意,是最小化最大值,而不是最大化最小值,判断的时候要注意 联动3258 #include <iostr ...
随机推荐
- json等序列化模块 异常处理
今日学习内容如下: 1.序列化模块 什么叫序列化——将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 比如,我们在python代码中计算的一个数据需要给另外一段程序使用,那我们怎么给? 现 ...
- maven项目打包发布到私有仓库
在项目开发中通常会引用其他的jar,怎样把自己的项目做为一个jar包的形式发布到私服仓库中,主要有以下三个步骤 (怎样配置maven私服仓库,就不再这里说明了,可以参考以前的文章)1.在maven的s ...
- dev NavBarControl控件
一.新建一个导航栏 拖入一个panel到窗口上做为导航栏的容器,然后再拖入一个NavBarControl到其上,点击NavBarControl控件的右上角三角箭头展开任务列表,选择PaintStyle ...
- Oracle_PL/SQL(8) 动态sql
动态sql0.pl/sql块的限制 不能执行ddl操作(create.drop.alter): 不能执行部分dcl操作(grant.revoke). 1.语法动态sql:在执行时才能确定要执行的sql ...
- nginx + fastdfs 的开机自启动
虚拟机每次启动之后都要重新启动一下fastdfs 和 nginx服务,比较麻烦,所以增加开机自启动: 编辑 /etc/rc.d/rc.local 文件,增加启动项: 1.编辑文件 vim /etc/r ...
- 比较两个List列表,取得List中不同项返回
/// <summary> /// 比对模型及属性数组 /// </summary> /// <typeparam name="TM">< ...
- 调试程序时如何用syslog来打印信息
转自:https://www.cnblogs.com/vigarbuaa/archive/2013/02/05/2892544.html Linux下C语言编程的-把程序输出信息加到系统日志里去关键词 ...
- php中static静态关键字的使用方法和应用场景
php中除了常规类和方法的使用,访问控制之外,还有静态关键字static,静态变量可以是局部变量也可以是全局变量,当一个程序段执行完毕时,静态变量并没有消失,它依然存在于内存中,下次在定义时还是以前的 ...
- qt 5.2.1类和模块的关系图
QT│ ├─ActiveQt│ │ ActiveQt│ │ ActiveQtDepends│ │ ActiveQtVersion│ │ QAxAggregated│ │ QAxB ...
- sqli-labs:24,二次注入
二次排序注入 二次排序注入也称为存储型注入,就是将可能导致 sql 注入的字符先存入到数据库中,当再次调用这个恶意构造的字符时,就可以出发 sql 注入. 二次排序注入思路: 1. 黑客通过构造数据的 ...