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.

Difficulty:二分,mid(即最小money)与组数M成单调递减的函数,对mid进行二分。

#include<cstdio>
#include<cstring>
using namespace std;
int n,m,mid,high,low,f;
int a[100005];
int judge(int x)
{int count=1;int sum=0;//count为组数计量
for(int i=0;i<m;i++)
{if(sum+a[i]<=mid)//前i天比mid值小,合为一组。
sum+=a[i];
else//前i天比mid大,那前i-1天已经成为一组,第i天作为下一组的开始
{
sum=a[i];
count++;
}
}
if(count>n)//组数计量大于题目所要求组数,说明mid值偏小
return 1;
else//反之,说明mid值偏大
return 0;
}
int main()
{
while(~scanf("%d%d",&m,&n))
{high=low=mid=f=0;
for(int i=0;i<m;i++)
{scanf("%d",&a[i]);
high+=a[i];//最大值的情况组数为1
if(low<a[i])
low=a[i]; //最小值的情况组数n=m
}
if(n==1)
{printf("%d",high);f=1;}
if(n==m)
{printf("%d",low);f=1;
}
if(f==0)
{mid=(high+low)/2;//开始进行二分;
while(low<high)
{
if(judge(mid))//可能在low==high之前,满足了题目所要求的组数,但不是最优解,继续二分,mid的值是一步一步减小或增大(即-1或+1)当low=high时,即最优解。
low=mid+1;
else
high=mid-1;
mid=(high+low)/2;//调整这个式可以微调答案。
}
printf("%d\n",mid);}}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Monthly Expense(二分) 分类: 二分查找 2015-06-06 00:31 10人阅读 评论(0) 收藏的更多相关文章

  1. 递归查找无效的符号链接 分类: linux c/c++ 2014-06-02 00:14 345人阅读 评论(0) 收藏

    本程序实现在指定目录下递归查找无效的符号链接. 1.设计思路 逐个读取给定目录中的目录项,判断类型 (1)若为目录,则读取该目录中的目录项并判断类型: (2)若为链接文件,则读取出其指向文件的名称(绝 ...

  2. 随机带权选取文件中一行 分类: linux c/c++ 2014-06-02 00:11 344人阅读 评论(0) 收藏

    本程序实现从文件中随即选取一行,每行被选中的概率与改行长度成正比. 程序用一次遍历,实现带权随机选取. 算法:假设第i行权重wi(i=1...n).读取到文件第i行时,以概率wi/(w1+w2+... ...

  3. Mahout快速入门教程 分类: B10_计算机基础 2015-03-07 16:20 508人阅读 评论(0) 收藏

    Mahout 是一个很强大的数据挖掘工具,是一个分布式机器学习算法的集合,包括:被称为Taste的分布式协同过滤的实现.分类.聚类等.Mahout最大的优点就是基于hadoop实现,把很多以前运行于单 ...

  4. Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 58072   Accepted: 18061 ...

  5. 各种排序算法的分析及java实现 分类: B10_计算机基础 2015-02-03 20:09 186人阅读 评论(0) 收藏

    转载自:http://www.cnblogs.com/liuling/p/2013-7-24-01.html 另可参考:http://gengning938.blog.163.com/blog/sta ...

  6. MS SQLServer 批量附加数据库 分类: SQL Server 数据库 2015-07-13 11:12 30人阅读 评论(0) 收藏

    ************************************************************ * 标题:MS SQLServer 批量附加数据库 * 说明:请根据下面的注释 ...

  7. MS SQL数据批量备份还原(适用于MS SQL 2005+) 分类: SQL Server 数据库 2015-03-10 14:32 103人阅读 评论(0) 收藏

    我们知道通过Sql代理,可以实现数据库的定时备份功能:当数据库里的数据库很多时,备份一个数据库需要建立对应的定时作业,相对来说比较麻烦: 还好,微软自带的osql工具,比较实用,通过在命令行里里输入命 ...

  8. Hiking 分类: 比赛 HDU 函数 2015-08-09 21:24 3人阅读 评论(0) 收藏

    Hiking Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...

  9. 哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏

    4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 17875 Accepted: ...

随机推荐

  1. Match类

    Regex在匹配的时候,常常会返回一个Match类的对象,今天就来看看怎么玩这个对象. 一.属性 Captures 按从里到外.从左到右的顺序获取由捕获组匹配的所有捕获的集合(如果正则表达式用 Reg ...

  2. DeflateStream类

    DeflateStream是另外一种压缩与解压缩流,使用方法与GZipStream类似,而且压缩之后的带下也差不多. 一.属性 BaseStream 获取对基础流的引用. CanRead  获取一个值 ...

  3. Android Fragment中使用Intent组件拍照

    要在activity里面去接受,然后传递给fragment对象,fragment有很多回调调用不到 你的设备有摄像头吗? 为了确保市场上的大多数设备都能运行你的程序,必须在项目中做一些检测,保证使用的 ...

  4. JavaScript加密解密7种方法总结分析

    原文地址:http://wenku.baidu.com/view/9048edee9e31433239689357.html 本文一共介绍了七种javascript加密方法: 在做网页时(其实是网页木 ...

  5. 汉子英文同行 连续英文不折行断行 的问题 兼容FIREFOX浏览器CSS

    #intro {white-space: normal;word-break: break-all;overflow: hidden;} --------------------- 案例2

  6. 百度地图LV1.5实践项目开发工具类bmap.util.jsV1.2

    /** * 百度地图使用工具类-v1.5 * * @author boonya * @date 2013-7-7 * @address Chengdu,Sichuan,China * @email b ...

  7. Climbing Stairs 解答

    Question You are climbing a stair case. It takes n steps to reach to the top. Each time you can eith ...

  8. Unity position和localposition

    1. position是根据世界原点为中心 2. localPosition是根据父节点为中心,如果没有父节点,localpositon和position是没有区别的 3.选中一个物体左上角Globa ...

  9. IOS 调用系统发邮件Api

    // 判断设备是否有发送邮件功能 NSString *deviceType = [UIDevice currentDevice].model; if([deviceType isEqualToStri ...

  10. sql语句收集

    一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...