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. C语言的本质(34)——静态库

    库是一种软件组件技术,库里面封装了数据和函数. 库的使用可以使程序模块化. Windows系统包括静态链接库(.lib文件)和动态链接库(.dll文件). Linux通常把库文件存放在/usr/lib ...

  2. < IOS > X-code 5.1 x86 - 64 编译问题

    关于xcode 5.1   x86 - 64 编译问题   坐等了N久,终于IOS 7.1 发布了,作为一个果粉,忍不住第一时间升级了.结果用设备测试的时候,出问题了,一直检测不到设备,哈哈,纠结了半 ...

  3. java没有条件编译

    摘自http://maosidiaoxian.iteye.com/blog/1290740 条件编译绝对是一个好东西.如在C或CPP中,可以通过预处理语句来实现条件编译.代码如下: #IFDEF DE ...

  4. WINDOWS 下搭建 OC 集成开发环境

    Objective-C是苹果软件的编程语言,想要上机学习.调试,有一个集成开发环境(IDE)方便很多.有三类方法搭建Objective-C的集成开发环境: 1)   使用苹果的平台,集成开发环境使用X ...

  5. Windows Message Queue(优先队列)

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Windows Message Queue Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  6. C,C#,C++中&&和||,&和|的联系和区别

    本文来自:http://www.cnblogs.com/GT_Andy/archive/2010/03/30/1921805.html 两者计算结果相同(针对各自的运算对象),只是性能上有差别而已. ...

  7. Java正則表達式

    近期工作中常常要用到正則表達式,不得不花点时间对其进行一定的学习.   JDK中提供了2个类来支持正則表達式,各自是java.util.regex.Pattern和java.util.regex.Ma ...

  8. VS2010+ICE3.5运行官方demo报错----std::bad_alloc

    纠结了一晚上,在release版本下运行没问题,一到debug就报错,卡在 Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWith ...

  9. ios8加入通知栏开始

    ios8加入通知栏开始 by 吴雪莹 以打开vpn设置为例: @IBAction func open(sender: AnyObject) { let context = self.extension ...

  10. jQuery UI的基本使用方法与技巧

    一.概述 jQuery UI is a widget and interaction library built on top of the jQuery JavaScript Library, th ...