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) ...
随机推荐
- Map与实体之间转换
package com.thunisoft.maybee.engine.utils; import java.lang.reflect.Field; import java.lang.reflect. ...
- ajax 多个参数问题,如何既能表单序列化获取,又能加参数,加全部代码
$.param({'address':address,'delivity':delivity,'payment':payment}) + '&' + $('#card_form').ser ...
- 记一次MyEclipse闪退的解决方法
http://www.th7.cn/Program/java/201408/262487.shtml ———————————————————————————————————————————————— ...
- CentOS的Gearman安装
背景:用PHP做一些简单的上传是没有任何的问题,但是要做断点上传好像也是没有大问题,但要是并发的切片加断点上传可能就会有问题了哟.第一个问题是合并问题:如果一上传就合并,PHP老半天不返回是一个方面( ...
- 解决Eclipse闪退问题的方法总结
1.在C:/WINDOWS/system32 系统文件夹中ctrl+F 然后搜索java.exe,如果存在java.exe, javaw.exe etc.全部删除. 2.内存不足,打开Eclipse目 ...
- 微信小程序 --- e.currentTarget.dataset.id 获取不到值
直接代码 wxml代码片段 <view class='ranksList' wx:for="{{ranksLb}}"> <view class='ranksLis ...
- 深入浅出学习hibernate框架(三):java的反射机制
上篇博客写到了JDBC的基本操作,今天准备写一篇关于JAVA反射机制的文章,因为java的反射机制和上一篇JDBC都是Hibernate框架的基本要素.在Hibernate的运行机制中,这两块的内容正 ...
- sparkR集群启动脚本的封装。
sparkR默认是以单机方式运行的.实现sparkR的集群启动,需要指定master地址,占用内存.CPU,及UI端口等,这对分析人员来说是比较麻烦的. 如何实现对其启动的封装,方便分析人员来使用: ...
- Sword websocket分析二
//websocket发送数据 int send(uint8_t* message, uint64_t message_size) { //掩码 ] = { 0x12, 0x34, 0x56, 0x7 ...
- Linux_iptables
Linux:网络防火墙 netfilter:Frame iptables: 生成防火墙规则,并附加到netfilter上实现数据报文过滤 NAT mangle等规则生成的工具 TCP有限状态机 LIS ...