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

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.

Source

解题思路:

题意为给定一个n个数组成的序列,划分为m个连续的区间,每一个区间全部元素相加,得到m个和,m个和里面肯定有一个最大值,我们要求这个最大值尽可能的小。

用二分查找能够非常好的解决问题。这类问题的框架为,找出下界left和上界right, while(left< right), 求出mid,看这个mid值是符合题意,继续二分。最后right即为答案。

本题中的下界为n个数中的最大值,由于这时候,是要划分为n个区间(即一个数一个区间),left是满足题意的n个区间和的最大值,上届为全部区间的和,由于这时候,是要划分为1个区间(全部的数都在一个区间里面),    1<=m<=n, 所以我们所要求的值肯定在 [left, right] 之间。对于每个mid,遍历一遍n个数,看能划分为几个区间,假设划分的区间小于(或等于)给定的m,说明上界取大了, 那么 另 right=mid,否则另 left=mid+1.

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=100010;
int money[maxn];
int n,m; int main()
{
scanf("%d%d",&n,&m);
int left=-1,right=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&money[i]);
if(left<money[i])
left=money[i];
right+=money[i];
}
while(left<right)
{
int mid=(left+right)/2;
int cnt=0;
int cost=0;
for(int i=1;i<=n;i++)
{
if(cost+money[i]>mid)
{
cnt++;//划分区间,不包含当前的money[i]
cost=money[i];
}
else
cost+=money[i];
}
cnt++;//最后一个cost值也要占一天
if(cnt<=m)
right=mid;
else
left=mid+1;
}
cout<<right<<endl;
return 0;
}

[ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)的更多相关文章

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

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

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

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

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

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

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

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

  5. POJ 3273 Monthly Expense 二分枚举

    题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...

  6. poj 3273 Monthly Expense (二分)

    //最大值最小 //天数的a[i]值是固定的 不能改变顺序 # include <algorithm> # include <string.h> # include <s ...

  7. 二分搜索 POJ 3273 Monthly Expense

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

  8. 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】

    链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  9. POJ 3273 Monthly Expense(二分搜索)

    Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...

随机推荐

  1. node-webkit 使用nodejs第三方C/C++插件

    node-webkit 在window环境下使用C/C++插件,需要使用nw-gyp先编译.本文以编译node-expat演示操作过程: 1.安装nodejs: 最好将nodejs的执行路径添加进系统 ...

  2. MapReuce 编程总结-多MapReduce执行

    学习hadoop,必不可少的就是写MapReduce程序,当然,对于简单的分析程序,我们只需一个MapReduce就能搞定,这里就不提单MapReuce的情况了,网上例子很多,大家可以百度Google ...

  3. 利用python进行数据分析之数据加载存储与文件格式

    在开始学习之前,我们需要安装pandas模块.由于我安装的python的版本是2.7,故我们在https://pypi.python.org/pypi/pandas/0.16.2/#downloads ...

  4. halcon与C#混合编程

    halcon源程序: dev_open_window(0, 0, 512, 512, 'black', WindowHandle)read_image (Image, 'C:/Users/BadGuy ...

  5. HTML禁止使用右键

    <html> <script type="text/javascript"> <!-- document.oncontextmenu=function ...

  6. 输入输出函数库stdio.h

    函数名 函数类型与形参类型 函数功能 函数返回值 clearerr void clearerr(fp) FILE * fp; 清除文件指针错误 无 close int close(fp) int fp ...

  7. html 实现网址链接

    <a href="http://acm.nyist.net/JudgeOnline/problemset.php">南工oj</a> HTML学习 < ...

  8. 为过程或函数sp_Adduser指定了过多的参数

    前些天写用户注册模块,用存储过程添加用户,一开始就报“为过程或函数sp_Adduser指定了过多的参数”.仔细检查数据层的用户添加函数,结果在为存储过程添加sqlparameter参数的时候,数组给写 ...

  9. Android 流媒体系列(二)

    import java.io.IOException; import android.app.Activity; import android.content.ContentResolver; imp ...

  10. BZOJ 2253: [2010 Beijing wc]纸箱堆叠

    题目 2253: [2010 Beijing wc]纸箱堆叠 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 239  Solved: 94 Descr ...