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 ( ≤ moneyi ≤ ,) that he will need to spend each day over the next N ( ≤ N ≤ ,) days.

FJ wants to create a budget for a sequential set of exactly M ( ≤ M ≤ N) fiscal periods called "fajomonths". Each of these fajomonths contains a set of  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 : Two space-separated integers: N and M
Lines ..N+: Line i+ contains the number of dollars Farmer John spends on the ith day

Output

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

Sample Input


Sample Output


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 $ in any month. Any other method of scheduling gives a larger minimum monthly limit.

Source

 
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
#define N 100006
#define inf 1<<30
int n,m;
int a[N];
bool solve(int x){
int sum=;
int group=;
for(int i=;i<n;i++){
if(sum+a[i]<=x){
sum+=a[i];
}
else{
sum=a[i];
group++;
}
}
if(group>=m) return false;//如果当前的mid值能分成大于等于m的组数,说明mid太小,mid变大的时候组数才能变少
return true;
}
int main()
{ while(scanf("%d%d",&n,&m)==){
int high=;
int low=;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
high+=a[i];
if(a[i]>low){
low=a[i];//把花费最多的那天当做是最低low(相当于把n天分为n组)
}
} while(low<high){ int mid=(low+high)>>;
if(solve(mid)){
high=mid;
}
else{
low=mid+;
}
}
printf("%d\n",low);
}
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"(二分搜索+最小化最大值)

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

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

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

  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. CSS3:优雅地绘制不规则ICON

    早上在w3ctech上看到 中国第二届CSS Conf总结  的时候,真是开心极了: 自从去年在慕课网上看了第一届CSS conf 视频之后,整个人都震惊了,原来还有大会是专门用来讨论CSS的,而且分 ...

  2. 【C#基础】CSA控件编写秘籍

    新建CSA控件 1.新建一个纯类,命名空间是: namespace SimPerfect.CSAControlLibrary.CSAControls 2.实现两个构造函数:无参和传Candy参数 pu ...

  3. JavaScript异步编程 ( 一 )

    1. 异步编程 Javascript语言的执行环境是"单线程"(single thread).所谓"单线程",就是指一次只能完成一件任务.如果有多个任务,就必须 ...

  4. 内容提供者 ContentResolver 数据库 示例 -1

    MainActivity public class MainActivity extends ListActivity {     private TextView tv_info;     priv ...

  5. 解决操作过快导致ajax异常的办法

    //控制点击过快ajax异常 var state = true; function test() { if (state) { state = false; var val = accMul((uCo ...

  6. rhel6.4 using centos source

    rhel 默认的 yum 依赖于 rhn,要注册收费才能 update,改用 centos 的源: rpm -e yum* --nodeps rpm -qa | grep yum | xargs rp ...

  7. SQL Server 2005无日志文件附加数据库

    公司网站运营两年多了,日志文件超级大,在重装系统的时候,为了省事,就没有备份日志文件,而且是没有分离就把日志文件给删掉了(下次一定要记得先分离再删日志文件).结果造成数据库怎么都附加不上.出现错误. ...

  8. [Jquery] 操作html 不常用元素方法大全

    除http://www.w3school.com.cn/jquery/jquery_selectors.asp上的以外该大全应都有. 第一章 input控件篇 1.操作select 下拉框 1.1 获 ...

  9. C#ArrayList对象集合

    ArrayList alist = new ArrayList(); //集合对像 长度可以改变,类型不限 //添加单个元素可以Add() alist.Add("在在的"); al ...

  10. hibernate像MySQL数据库里面存值是中文乱码的解决合辑

    解决办法一: 1.把数据库表每一行改成gb2312_chinese(使用PHPMyAdmin)   2.导入时候还是不行,因此查网上的资料说hibernate持久化层在传输过程中未设置编码格式,这样在 ...