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. 成都传智播客java就业班和基础班

    传智播客成都Java培训,带你走进Java的世界... 我们有咨询的教育团队,一流的名师指导: 我们是重视基础理论建设,强化高端应用技能: 我们有四大JavaEE项目,海量Android项目: 我们是 ...

  2. [React] React Router: Redirect

    The Redirect component in react-router does exactly what it sounds like. It allows us to redirect fr ...

  3. Struts的核心配置

    一.配置struts.xml文件 1.struts.xml文件 2.常量配置 <constant> struts.properities web.xml中的<init-param&g ...

  4. C#中几种换行符

    1.Windows 中的换行符"\r\n" 2.Unix/Linux 平台换行符是 "\n". 3.MessageBox.Show() 的换行符为 " ...

  5. Android推送等耗电原因剖析

    原文链接:http://www.jianshu.com/p/584707554ed7 Android手机有两个处理器,一个是Application Processor(AP)基于ARM处理器,主要运行 ...

  6. 学习OkHttp wiki--Interceptors

    Interceptors 拦截器(Interceptors)是一种强有力的途径,来监控,改写和重试HTTP访问.下面是一个简单的拦截器,对流出的请求和流入的响应记录日志. class LoggingI ...

  7. arcmap从excel坐标数据生成点shp文件

    概述 今天试图在ArcMap中将excel数据转成点文件,在"Display XY Data"的时候,无法选择X,Y字段,很是纳闷,原来Excel中列的格式是文本,导致无法选择.有 ...

  8. IPointCollection,ISegmentCollection和IGeometryCollection

    Engine 提供了三个主要的几何图形集合接口用于对几何对象的操作,分别是 IPointCollection,ISegmentCollection 和 IGeometryCollection,这些接口 ...

  9. C# winform DataTable 批量数据处理 增、删、改 .

    1.批量新增,采用高效的SqlBulkCopy SqlBulkCopy DTS = new System.Data.SqlClient.SqlBulkCopy(con); DTS.NotifyAfte ...

  10. nginx入门手册(一)

    1.nginx进程: nginx会启动多个进程: 一个主进程Master. 几个工作进程worker. 缓存加载器进程 缓存管理器进程 master主要工作: 1. 读取并验正配置信息: 2. 创建. ...