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.
思路:最大值最小化,典型的二分问题。需要注意一点,如果按照当前最大值所得分组,是小于m的,说明我们当前设定的最大值太大,需要变小一些。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;const int maxx=1e5+100;
int a[maxx];
int n,m;bool fcs(int mid)
{int cnt=0;int sum=0;for(int i=1;i<=n;i++){if(a[i]>mid) return 0;if(sum+a[i]<mid) sum+=a[i];else if(sum+a[i]==mid) sum=0,cnt++;else sum=0,cnt++,i--;}if(sum<=mid) cnt++;return cnt<=m;//这里需要注意
}
int main()
{while(~scanf("%d%d",&n,&m)){int r=0;for(int i=1;i<=n;i++) scanf("%d",&a[i]),r+=a[i];int l=0,mid,ans;while(l<=r){mid=l+r>>1;if(fcs(mid)){ans=mid;r=mid-1;}else l=mid+1;}cout<<ans<<endl;}return 0;
}

努力加油a啊

Monthly Expense POJ - 3273(二分最大值最小化)相关推荐

  1. I - Monthly Expense POJ - 3273

    ZYH学长非常菜,这一天他看到了这一题: 给你一个长度为N的序列,现在需要把他们切割成M个子序列(所以每一份都是连续的),使得每个子序列和均不超过某个值X 但是ZYH学长实在太菜了,这个问题困扰了他很 ...

  2. BZOJ 1639: [Usaco2007 Mar]Monthly Expense 月度开支( 二分答案 )

    直接二分答案然后判断. ----------------------------------------------------------------------------- #include&l ...

  3. 最大值最小化(网易有道2013年校园招聘面试一面试题)

    题目描述: 在印刷术发明之前,复制一本书是一个很困难的工作,工作量很大,而且需要大家的积极配合来抄写一本书,团队合作能力很重要. 当时都是通过招募抄写员来进行书本的录入和复制工作的, 假设现在要抄写m ...

  4. 【算法 | 实验8】分配最小页数(数组划分和最大值最小化问题)

    文章目录 题目 问题分析与算法设计思路 思路1:类似枚举的分治(暴力) 思路2:二分法 算法实现(C++) 思路1实现 思路2实现 bug记录 1.子问题对最大值没有实现最小化 2.保存的最大值是局部 ...

  5. leetcode 二分法 最大值最小化/最小值最大化

    一.(leetcode 410)分割数组的最大值 题目描述: 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小. 代 ...

  6. uva 714 Copying Books(二分法求最大值最小化)

    题目连接:714 - Copying Books 题目大意:将一个个数为n的序列分割成m份,要求这m份中的每份中值(该份中的元素和)最大值最小, 输出切割方式,有多种情况输出使得越前面越小的情况. 解 ...

  7. 蓝桥试题 算法提高 打包(二分法,最大值最小化)

    资源限制 时间限制:1.0s   内存限制:256.0MB 问题描述 Lazy有N个礼物需要打成M个包裹,邮寄给M个人,这些礼物虽然很便宜,但是很重.Lazy希望每个人得到的礼物的编号都是连续的.为了 ...

  8. 摘枇杷(最大值最小化)

    理工学院的枇杷快熟了,ok,大家都懂得.而且大家都知道,学校的枇杷树都是一列一列的.现在小Y同学已经在筹划怎么摘枇杷了.现在我们假设有一列枇杷树,而且每棵枇杷树上枇杷果的数量小Y都已经知道了. 假设现 ...

  9. Monthly Expense【二分】

    B - Monthly Expense POJ - 3273 Farmer John is an astounding accounting wizard and has realized he mi ...

最新文章

  1. Swift和Java在函数(method/方法)方面的比较
  2. iOS UIVisualEffectView毛玻璃亮度不符合要求
  3. 利用sendmail搭建邮件服务器
  4. 阿里来了位技术新童鞋,一秒K.O八位律师
  5. 获取手机信息(UIDevice、NSBundle、NSLocale)
  6. (原创总结) 几种通信编码方式
  7. SQL学习笔记之存储过程的编写
  8. Win10下安装wireshark不能正常使用,cmd管理员身份调用net start npf命令显示无法启动该服务
  9. http之httpClient工具类
  10. 【详细解读】知识图谱的这一人工智能技术分支的概念、技术、应用、与发展趋势
  11. 一键清除系统垃圾 bat文件
  12. c# RoundUp函数
  13. KDL简介---KDL、PyKDL、pykdl_utils之间关系
  14. Android获得手机唯一设备ID号
  15. 黄油安卓_寻找可爱,定义外观为黄油皇家字符
  16. ligerui 表格滚动条放在表格里,固定表头
  17. 【信号去噪】基于NLM时间序列心电信号去噪附matlab代码
  18. mirna富集分析_miRNA富集分析数据库
  19. oa系统服务器地址怎么查,如何查询oa服务器地址
  20. 【加解密】在线加密工具推荐

热门文章

  1. lock锁和monitor.enter锁
  2. IOS基础之打砖块项目演练
  3. java生成pdf_Java实现PDF文件生成并且打印pdf文件 demo
  4. c语言求成绩标准差,C程序计算标准偏差
  5. 2、AD工程创建步骤
  6. C语言1e12怎么识别,掌握C语言中基本的运算符
  7. 微信小程序通过getUserProfile和wx.login获取后端的token
  8. 如何将目录下几百个lib加入到vs项目属性下的链接器
  9. 计算机四年级下册教案泰山版,泰山版信息技术四年级下册4、制作作息时间表教案设计...
  10. php7.1 aes 加密解密,PHP7.1中AES加密解密方法 mcrypt_module_open()替换方案