POJ 3273 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.
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
Source Code Problem: User: blazing
Memory: 612K Time: 329MS
Language: C++ Result: Accepted
Source Code
#include <iostream>
using namespace std;
const int MAXN = ;
int input[MAXN];
int n, k;
int low, high;
int curMax, curSum;
int binarySearch(const int & low, const int &high) {
if(low > high) return low;
int mid = (low + high)>>;
//cout << "mid : " << mid << endl;
int count = , sum = ;
for( int i = ; i < n; i ++ ) {
if( sum+input[i] <= mid ) { }else{
count += ;
sum = ;
}
sum += input[i];
}
count ++;
if( count > k )
binarySearch(mid+, high);
else
binarySearch(low, mid-); }
int main() {
//freopen("E:\\Copy\\ACM\\poj\\3273_v1\\in.txt", "r", stdin);
while( cin >> n >> k ) {
curMax = , curSum = ;
for( int i = ; i < n; i ++ ) {
cin >> input[i];
curMax = max( curMax, input[i]);
curSum += input[i];
} int curSize = binarySearch( curMax, curSum ); cout << curSize << endl;
}
return ;
}
POJ 3273 Monthly Expense(二分搜索)的更多相关文章
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
- poj 3273 Monthly Expense(二分搜索之最大化最小值)
Description Farmer John ≤ moneyi ≤ ,) that he will need to spend each day over the next N ( ≤ N ≤ ,) ...
- poj 3273"Monthly Expense"(二分搜索+最小化最大值)
传送门 https://www.cnblogs.com/violet-acmer/p/9793209.html 题意: 有 N 天,第 i 天会有 a[ i ] 的花费: 将这 N 天分成 M 份,每 ...
- 二分搜索 POJ 3273 Monthly Expense
题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...
- POJ 3273 Monthly Expense(二分查找+边界条件)
POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
- [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14158 Accepted: 5697 ...
- POJ 3273 Monthly Expense(二分答案)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...
- poj 3273 Monthly Expense(贪心+二分)
题目:http://poj.org/problem?id=3273 题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和. 思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max) ...
随机推荐
- elasticsearch6.4 memory locking requested for elasticsearch process but memory is not locked 终极解决
echo "es hard memlock unlimited">>/etc/security/limits.confecho "es soft memloc ...
- Spring Cloud Config 配置中心高可用
详细参见 <Spring Cloud 与 Docker微服务架构实战> p163-9.10 Spring Cloud Config 与 Eureka 配合使用 p163-9.12 Conf ...
- matlab std函数 用法及实例
MATLAB常常用到std函数来进行标准差计算,下面我就通过实例介绍一下 matlab std函数怎么用. 1. std函数是用来计算标准偏差的一个函数,由于其有不同的参数,我们就用下面的例子进行介绍 ...
- TopComponent设置某个控件焦点
仅仅在 @Override public void componentOpened() { syncBtn.requestFocus(); } 中指定还不够,还需要重载 @Override publi ...
- php curl_multi系列函数实现多线程抓取网页
最近几天在做一个多搜索引擎关键字排名查询工具,用于及时方便的了解关键词在各大搜索引擎的排名. 在抓取360搜索的时候,发现360搜索每页只支持显示10个搜索结果,如果想获取100个搜索结果数据,就得搜 ...
- json如何把键名作为变量?
有时候在项目开发过程中,我们需要把json对象的键名作为一个变量.此时我们该怎么做呢? 传统的json数据格式如下: <script type="text/javascript&quo ...
- jQuery验证控件jquery.validate.js的使用介绍
官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载 ...
- USB学习笔记连载(二十):FX2LP如何实现高速和全速切换(转载)
CYPRESS的USB外设控制器CY7C68013A是一款广泛应用于USB打印机,手机,存储设备,USB测试等多个领域的经典产品.该产品符合USB2.0协议规范,支持full speed和high s ...
- ADT安装
Eclipse安装ADT很简单,直接把下载好的ADT文件解压,覆盖到eclipse目录下的features和plugins目录即可, MyEclipse配置就稍微麻烦了点,我刚开始配置了好几次都不成功 ...
- C++复合类型(数组)
1.数组 数组之所以被称为复合类型, 是因为它是使用其他类型来创建的 例如: short months[12]: 那么格式为 typename arrayname [arraysize] 注意:ar ...