Monthly Expense(二分) 分类: 二分查找 2015-06-06 00:31 10人阅读 评论(0) 收藏
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) 收藏的更多相关文章
- 递归查找无效的符号链接 分类: linux c/c++ 2014-06-02 00:14 345人阅读 评论(0) 收藏
本程序实现在指定目录下递归查找无效的符号链接. 1.设计思路 逐个读取给定目录中的目录项,判断类型 (1)若为目录,则读取该目录中的目录项并判断类型: (2)若为链接文件,则读取出其指向文件的名称(绝 ...
- 随机带权选取文件中一行 分类: linux c/c++ 2014-06-02 00:11 344人阅读 评论(0) 收藏
本程序实现从文件中随即选取一行,每行被选中的概率与改行长度成正比. 程序用一次遍历,实现带权随机选取. 算法:假设第i行权重wi(i=1...n).读取到文件第i行时,以概率wi/(w1+w2+... ...
- Mahout快速入门教程 分类: B10_计算机基础 2015-03-07 16:20 508人阅读 评论(0) 收藏
Mahout 是一个很强大的数据挖掘工具,是一个分布式机器学习算法的集合,包括:被称为Taste的分布式协同过滤的实现.分类.聚类等.Mahout最大的优点就是基于hadoop实现,把很多以前运行于单 ...
- 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 ...
- 各种排序算法的分析及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 ...
- MS SQLServer 批量附加数据库 分类: SQL Server 数据库 2015-07-13 11:12 30人阅读 评论(0) 收藏
************************************************************ * 标题:MS SQLServer 批量附加数据库 * 说明:请根据下面的注释 ...
- MS SQL数据批量备份还原(适用于MS SQL 2005+) 分类: SQL Server 数据库 2015-03-10 14:32 103人阅读 评论(0) 收藏
我们知道通过Sql代理,可以实现数据库的定时备份功能:当数据库里的数据库很多时,备份一个数据库需要建立对应的定时作业,相对来说比较麻烦: 还好,微软自带的osql工具,比较实用,通过在命令行里里输入命 ...
- 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 ...
- 哈希-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: ...
随机推荐
- Amazon Redshift and Massively Parellel Processing
Today, Yelp held a tech talk in Columbia University about the data warehouse adopted by Yelp. Yelp u ...
- HTML5 canvas translate() 方法
HTML5 canvas translate() 方法 translate() 方法重新映射画布上的 (0,0) 位置.
- android-读取Assets图片资源保存到SD - 随心
public class ReadBitmap { public void readByte(Context c, String name, int indexInt) { byte[] b = nu ...
- WIN7 64位系统安装JDK并配置环境变量
本文来自:http://jingyan.baidu.com/article/3c343ff70bc6ea0d377963df.html 工具/原料 JDK 方法/步骤 首先,下载JDK安装包,到官 ...
- PCL库配置出现的问题(WIN10+VS2013)
边看电影边配终于配好了,中间出现了一些问题,在网上很难搜到,可能每个人都碰到的不同.摸索了一会终于都解决了,记录在这里,免得又碰到. PCL是什么东西就不在此介绍了. 主要是参考这篇博客做得,不过我后 ...
- hdu2095 像水题的不错题 异或运算
异或运算的基础有点忘记了 先介绍一下..2个数异或 就是对于每一个二进制位进行位运算 具有2个特殊的性质 1.一个数异或本身恒等于0,如5^5恒等于0: 2.一个数异或0恒等于本身,如5^0恒等于5. ...
- CSS学习笔记——盒模型,块级元素和行内元素的区别和特性
今天本来打算根据自己的计划进行前端自动化的学习的,无奈早上接到一个任务需求需要新增一个页面.自从因为工作需要转前端之后,自己的主要注意力几 乎都放在JavaScript上面了,对CSS和HTML这方面 ...
- 使用一个HttpModule拦截Http请求,来检测页面刷新(F5或正常的请求)
在Web Application中,有个问题就是:“我怎么来判断一个http请求到底是通过按F5刷新的请求还是正常的提交请求?” 相信了解ASP.NET的人知道我在说什么,会有同感,而且这其实不是一个 ...
- 【转载】Xcode6中添加pch文件
//原文地址:http://www.cnblogs.com/YouXianMing/p/3989155.html 1. 新建工程: 2. 创建pch文件:cmd+n->other->PCH ...
- HDU 5728 - PowMod
HDU 5728 - PowMod 题意: 定义: k = ∑(i=1,m) φ(i∗n) mod 1000000007 给出: n,m,p ,且 n 无平方因子 求: ans= k^(k^(k ...