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) ...
随机推荐
- 解決BufferedReader读取UTF-8文件中文乱码(转)
读取txt文件乱码 BufferedReader read = new BufferedReader(new FileReader(new File(filename))); 解决办法: InputS ...
- Nodejs中export的作用
在上一节,我们编写了一个hello.js文件,这个hello.js文件就是一个模块,模块的名字就是文件名(去掉.js后缀),所以hello.js文件就是名为hello的模块. 我们把hello.js改 ...
- PCL中IO模块和类的介绍
I/O模块中共有21个类 (1)class pcl::FIleReader:定义了PCD文件的读取接口,主要用作其他读取类的父类 pcl::FileReader有pcl::PCDReader和pc ...
- 获取checkbox的选中的值
var arr = new Array(); var idsObj = $("input [name='ids']"); for(var i=0;i<idsObj.lengt ...
- eclipse如何设置高亮代码的背景色,比如选中某个单词,高亮所有的
设置方法如下: 1.单击IDE顶部Window菜单下的Prefences,如图: 2.在打开对话框的左侧树上,找到Java节点下的Editor中的Mark Occurren,如图: 3.点击Mark ...
- Java sun.misc.unsafe类
Java是一个安全的开发工具,它阻止开发人员犯很多低级的错误,而大部份的错误都是基于内存管理方面的.如果你想搞破坏,可以使用Unsafe这个类.这个类是属于sun.*API中的类,并且它不是J2SE中 ...
- CentOS执行ping命令报错 name or service not know
在虚拟机上安装的CentOS,但是当执行ping命令的时候,提示name or service not known 解决方法如下: 1. 添加DNS服务器 vi /etc/resolv.conf 1 ...
- tornado 添加请求头进行允许跨域
什么是跨域? 这个例子是csdn找的, 声明下哈 什么是跨域? 跨域,指的是浏览器不能执行其他网站的脚本.它是由浏览器的同源策略造成的,是浏览器施加的安全限制. 所谓同源是指,域名,协议,端口均相同, ...
- [转]浅谈Android五大布局(一)——LinearLayout、FrameLayout和AbsoulteLayout
Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.Android的五大布局分别是LinearLay ...
- python epoll
需要用python实现中断的功能,所以用epoll监听gpio文件的变化.写个demo测试一下. 参考: http://www.cnblogs.com/coser/archive/2012/01/06 ...