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

Line 1: Two space-separated integers: N and M 
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day

Output

Line 1: The smallest possible monthly limit Farmer John can afford to live with.

Sample Input

7 5
100
400
300
100
500
101
400

Sample Output

500

Hint

If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.
 
思路
1. 二分法
 
代码
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(二分搜索)的更多相关文章

  1. poj 3273 Monthly Expense (二分搜索,最小化最大值)

    题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...

  2. poj 3273 Monthly Expense(二分搜索之最大化最小值)

    Description Farmer John ≤ moneyi ≤ ,) that he will need to spend each day over the next N ( ≤ N ≤ ,) ...

  3. poj 3273"Monthly Expense"(二分搜索+最小化最大值)

    传送门 https://www.cnblogs.com/violet-acmer/p/9793209.html 题意: 有 N 天,第 i 天会有 a[ i ] 的花费: 将这 N 天分成 M 份,每 ...

  4. 二分搜索 POJ 3273 Monthly Expense

    题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...

  5. POJ 3273 Monthly Expense(二分查找+边界条件)

    POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...

  6. POJ 3273 Monthly Expense二分查找[最小化最大值问题]

    POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...

  7. [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)

    Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14158   Accepted: 5697 ...

  8. POJ 3273 Monthly Expense(二分答案)

    Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...

  9. poj 3273 Monthly Expense(贪心+二分)

    题目:http://poj.org/problem?id=3273 题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和. 思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max) ...

随机推荐

  1. fiddler手机端抓包

    1. 买个360随身wifi,插在台式机上生成热点,手机连上自己的wifi 如果电脑与手机本就在一个局域网,省略这一步 2. 在fiddler中如下设置: 3. 查看电脑无线连接属性 4. 在手机上设 ...

  2. 【C】——extern

    直接上例子: 1.c #include<stdio.h> int main() { extern int a; a += ; printf("%d\n",a); tex ...

  3. JS事件对象,筋斗云导航练习,跟随鼠标练习,放大镜练习,进度条练习

    JS事件对象,筋斗云导航练习,跟随鼠标练习,放大镜练习,进度条练习 btn.onclick = function(event) { 语句 } 其中event就是事件对象,在这个方法中指向的对象是onc ...

  4. 在PHP中使用协程实现多任务调度

    PHP5.5一个比较好的新功能是加入了对迭代生成器和协程的支持.对于生成器,PHP的文档和各种其他的博客文章已经有了非常详细的讲解.协程相对受到的关注就少了,因为协程虽然有很强大的功能但相对比较复杂, ...

  5. Jquery函数大全 - 案例说明

    function(){$(this).addClass(”out”);}); toggle( fn, fn ) 如果点击了一个匹配的元素,则触发指定的第一个函数,当再次点击同一元素时,则触发指定的第二 ...

  6. C++ operator关键字详解

    C++中的operator主要有两个作用,一是操作符的重载,一是自定义对象类型的隐式转换. 类型转换操作符(type conversion operator)是一种特殊的类成员函数,它定义将类类型值转 ...

  7. 使用x11vnc作为vncserver端

    1 安装x11vnc $ sudo apt-get update $ sudo apt-get install x11vnc 2 生成密码 $ x11vnc -storepasswd Enter VN ...

  8. 【转】]监听SMS消息/编程实现短信拦截

    当设备接收到一条新的SMS消息时,就会广播一个包含了android.provider.Telephony.SMS_RECEIVED动作的Intent.注意,这个动作是一个字符串值,SDK 1.0不再包 ...

  9. 【Python】【Flask】前端调用后端方法返回页面

    后端代码: @app.route("/test",methods=['POST','GET']) def test(): return "我是测试的" 前端代码 ...

  10. 嵌入式开发之hi3519---i2c EEPROM

    http://pdf1.alldatasheetcn.com/datasheet-pdf/view/163283/MICROCHIP/24LC024.html http://www.elecfans. ...