POJ 3273 | Monthly Expense 农场的窘境(经典二分)
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.
农夫John是一个会计巫师,他发现自己快没钱运营农场了。他已经计算好了接下来的N天里每天需要的花费。
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.
John想创建一个连续的M天预算,这些M天称之为fajomonths。每一个fajomonths包含1天以上的连续日子。每一天都包含在一个fajomonth里。John的目标是求出这些fajomonths中最大的fajomonth。这样来确定他每个月的开销限制。(翻译有点拗口,见后面得题解)
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
第一行输入N和M
接下来N行表示第i天的支出
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.
题意解析: 给出农夫在n天中每天的花费,要求把这n天分成连续的m组,要求各组的花费之和应该尽可能地小,最后输出各组花费之和中的最大值。
思路:
经典的二分+检验题目,找到答案所在的区间【left,right】,然后二分区间mid=(left+right)/2,然后用judge_group()函数检验mid是否符合题目要求,时间复杂度log(n)。
把所有天数的总花费作为上界high(相当于把n天都分作1组),把n天中花费最多的那一天的花费作为下界low(相当于把n天分为n组),那么要求的值必然在[low,high]范围内。
1#include<iostream>
2using namespace std;
3
4int n; //天数
5int m; //规定的分组数
6
7/*判断用当前的mid值能把天数n分成几组*/
8/*通过比较group与m的大小,对mid值进行优化*/
9
10bool judge_group(int mid,int money[])
11{
12 int sum=0;
13 int group=1; //当前mid值能把n天分成的组数(初始把全部天数作为1组)
14
15 for(int i=1;i<=n;i++) //从第一天开始向下遍历每天的花费
16 if(sum+money[i]<=mid) //当前i天之和<=mid时,把他们归并到一组
17 sum+=money[i];
18 else //若 前i-1天之和 加上第i天的花费 大于mid
19 {
20 sum=money[i]; //则把前i-1天作为一组,第i天作为下一组的第一天
21 group++; //此时划分的组数+1
22 }
23
24 if(group>m)
25 return false; //若利用mid值划分的组数比规定的组数要多,则说明mid值偏小
26 else
27 return true; //否则mid值偏大
28}
29
30int main(void)
31{
32 while(cin>>n>>m)
33 {
34 int* money=new int[n+1]; //每天花费的金额
35 int low=0; //下界
36 int high=0; //上界
37
38 for(int i=1;i<=n;i++)
39 {
40 cin>>money[i];
41
42 high+=money[i]; //把所有天数的总花费作为上界high(相当于把n天都分作1组)
43 if(low<money[i])
44 low=money[i]; //把n天中花费最多的那一天的花费作为下界low(相当于把n天分为n组)
45 } //那么要求的值必然在[low,high]范围内
46
47 int mid=(low+high)/2;
48
49 while(low<high) //可能在low==high之前,分组数就已经=m,但是mid并不是最优,因此要继续二分
50 {
51 if(!judge_group(mid,money))
52 low=mid+1; //mid值偏小,下界前移
53 else
54 high=mid-1; //mid值偏大,上界后移
55
56 mid=(low+high)/2;
57 }
58
59 cout<<mid<<endl; //二分搜索最后得到的mid值必然是使得分组符合要求的最优值
60
61 delete money;
62 }
63 return 0;
64}
- 《Android基础:Fragment,看这篇就够了》
- Android 7.0中ContentProvider实现原理
- 《iOS APP 性能检测》
- iOS 11 安全区域适配总结
- Linux下巧用chattr、watch命令的实例
- 【特斯拉组件】iOS高性能PageController
- SUSE Linux系统在线安装软件命令zypper参数详解
- Linux下通过rdesktop连接Windows远程桌面
- 微信iOS收款到账语音提醒开发总结
- React Native按需加载 手Q狼人杀探索之路
- nginx日志切割及7天前的历史日志删除脚本
- 《ios爆内存问题解决方案-OOMDetector组件》
- Linux/SUSE安装svn客户端的问题记录
- 《广研Android卡顿监控系统》
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- jquery.ajax()怎么把获取来的内容转为JSON,并使用。
- Spring:IOC 控制反转
- Spring:AOP 面向切面编程
- JavaScript进阶教程(5)-一文让你搞懂作用域链和闭包
- JavaScript进阶教程(6)—硬核动图让你轻松弄懂递归与深浅拷贝
- Spring:JDBC Template,声明式事务
- Spring:讲解编程题
- SpringMVC:基本应用
- SpringMVC:进阶
- SpringMVC:SSM 整合
- 12 | Tornado源码分析:BaseIOStream 对象(上)
- 数据一致性校验及数据同步,运维必看
- Windows使用scoop包管理器安装RabbitMQ
- 深入理解web协议(二):DNS、WebSocket
- Elasticsearch:使用 function_score 及 soft_score 定制搜索结果的分数