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 ...
随机推荐
- python 3.6.5 hashlib 和 hmac 模块
import hashlib m=hashlib.md5()# m=hashlib.sha256() m.update('hello'.encode('utf8'))print(m.hexdigest ...
- c# 线程启动while(true) 死循环,里边的return导致线程退出情况,查错
写了一个线程 线程下启动了一个循环 while(true) { 里边有个判断 如果为空不操作, 有余这个线程是后加的,老程序里边因为有个return没关注,导致线程退出而不能不听的监控 } 线程启动一 ...
- 关于java.io.IOException: HADOOP_HOME or hadoop.home.dir are not set.的问题
报错如下: 300 [main] DEBUG org.apache.hadoop.util.Shell - Failed to detect a valid hadoop home directory ...
- Linux系统性能监控工具介绍之-tsar
Linux系统性能监控工具介绍之-tsar Linux系统性能监控工具介绍之-tsar 2017-03-02 20:25 175人阅读 评论(0) 收藏 举报 分类: LINUX调优(9) 目 ...
- flask部署
https://blog.csdn.net/zhuod/article/details/77850783
- APICloud开发
2018-06-16 今天在看房角石APPIOS版本闪退的问题,后来定位到了 elements.find("video").attr("preload", &q ...
- 《C#从现象到本质》读书笔记(二)第2章 C#类型基础(上)
<C#从现象到本质>读书笔记第二篇 第2章 C#类型基础(上) 类型指的是集合{类,结构,接口,枚举,委托}中的任意一个成员.任何拥有某类型的值(value)称为某类型的一个实例(inst ...
- python+selenium环境配置及浏览器调用
最近在学习python自动化,从项目角度和技术基础角度出发,我选择了python+selenium+appium的模式开始我的自动化测试之旅: 一.python安装 二.python IDE使用简介 ...
- spring学习 十一 AspectJ-based的通知入门 不带参数的通知
AspectJ-Based方式的AOP,通知类不需要实现任何接口,且前置通知,后置通知,环绕通知,异常通知都可以写在一个类中,下面先实现一个简单的,不带参数的通知. 第一步定义通知类,通知类中的通知( ...
- 企业IT资产管理功能大全