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. IOS-UITableView开发常用各种方法总结

    实现列表有两种方式 方式一 继承UIViewController,实现UITableViewDataSource和UITableViewDelegate协议.声明UITableView. UserIn ...

  2. android 删除的警告对话框

    在图形界面之中,对话框也是人机交互的一种重要的形式,程序可以通过对话框对用户进行一些信息的提示,而 用户也可以通过对话框和程序进行一些简单的交互操作. 在Android的开发之中,所有的对话框都是从a ...

  3. boost::pool与内存池技术

      建议看这个链接的内容:http://cpp.winxgui.com/cn:mempool-example-boost-pool Pool分配是一种分配内存方法,用于快速分配同样大小的内存块,    ...

  4. Java 之文件目录操作

    1.判断文件是否存在 File file = new File("d:\\study\\temp\\test.java"); boolean bl = file.exists(); ...

  5. Java基础知识强化52:经典排序之冒泡排序(BubbleSort)

    1. 冒泡排序的原理图: 2. 冒泡排序代码实现: package cn.itcast_01; /* * 数组排序之冒泡排序: * 相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处 ...

  6. Python字符串格式符号含义

    ====== #字符串格式化符号含义 #%C 格式化字符串及其ASCLL码 >>> '%c' %97 'a' >>> '%c' % 97 'a' >>& ...

  7. linux od命令

    用户通常使用od命令查看特殊格式的文件内容.通过指定该命令的不同选项可以以十进制.八进制.十六进制和ASCII码来显示文件.od命令系统默认的显示方式是八进制,这也是该命令的名称由来(Octal Du ...

  8. css3选择符使用个人理解。

    元素选择符: 通配选择符:一般用* 星号表示  他会命中整体标签. 类型选择符:一般用者空格表示  他会命中标签自己的所有子元素. ID选择符:  一般用# 井号表示  他会命中以id为属性的标签   ...

  9. ORACLE用户操作的一些常用操作总结【weber出品】

    一.创建一个表空间 create tablespace pioneer_data datafile '/u01/datafile/pioneer_datadbf' size 100m autoexte ...

  10. Ubuntu下安装搜狗拼音输入法

    Ubuntu默认输入法是ibus输入法,其实用着也可以了,但是说句实话在某些情况下真的不怎么智能.习惯了搜狗所以,查阅资料测试成功后整理如下, 1.安装搜狗拼音 sudo apt-add-reposi ...