[ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14158 | Accepted: 5697 |
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
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Sample Input
7 5
100
400
300
100
500
101
400
Sample Output
500
Hint
Source
解题思路:
题意为给定一个n个数组成的序列,划分为m个连续的区间,每一个区间全部元素相加,得到m个和,m个和里面肯定有一个最大值,我们要求这个最大值尽可能的小。
用二分查找能够非常好的解决问题。这类问题的框架为,找出下界left和上界right, while(left< right), 求出mid,看这个mid值是符合题意,继续二分。最后right即为答案。
本题中的下界为n个数中的最大值,由于这时候,是要划分为n个区间(即一个数一个区间),left是满足题意的n个区间和的最大值,上届为全部区间的和,由于这时候,是要划分为1个区间(全部的数都在一个区间里面), 1<=m<=n, 所以我们所要求的值肯定在 [left, right] 之间。对于每个mid,遍历一遍n个数,看能划分为几个区间,假设划分的区间小于(或等于)给定的m,说明上界取大了, 那么 另 right=mid,否则另 left=mid+1.
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=100010;
int money[maxn];
int n,m; int main()
{
scanf("%d%d",&n,&m);
int left=-1,right=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&money[i]);
if(left<money[i])
left=money[i];
right+=money[i];
}
while(left<right)
{
int mid=(left+right)/2;
int cnt=0;
int cost=0;
for(int i=1;i<=n;i++)
{
if(cost+money[i]>mid)
{
cnt++;//划分区间,不包含当前的money[i]
cost=money[i];
}
else
cost+=money[i];
}
cnt++;//最后一个cost值也要占一天
if(cnt<=m)
right=mid;
else
left=mid+1;
}
cout<<right<<endl;
return 0;
}
[ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)的更多相关文章
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
- POJ 3273 Monthly Expense(二分查找+边界条件)
POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...
- POJ 3273 Monthly Expense(二分答案)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...
- POJ 3273 Monthly Expense 二分枚举
题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...
- poj 3273 Monthly Expense (二分)
//最大值最小 //天数的a[i]值是固定的 不能改变顺序 # include <algorithm> # include <string.h> # include <s ...
- 二分搜索 POJ 3273 Monthly Expense
题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...
- 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】
链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- POJ 3273 Monthly Expense(二分搜索)
Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...
随机推荐
- Common Git command and mean (Windows)
Config: git config --system git config --global git config --global merge.tool vimdiff Check config: ...
- JAVA的BIT数组
写个小东西,要去重复数字,用到BIT数组,虽然JAVA已经提供了一个BitSet,不过自己手痒,又写了一个简单的 原理就不写了,网上一大堆 import java.util.Iterator; imp ...
- COB(Chip On Board) 工艺技术
COX(Chip On X) •X 基板: PCB (Printed circuit board) FPC (Flexible Printed Circuit) Glass •导线焊接 球形焊接 ...
- eclipse及Java常用问题及解决办法汇总
junit-test 我觉得这点比idea好用,可以直接选中要测试的方法名,右击run as即可 http://www.cnblogs.com/brolanda/p/4532779.html 打开您的 ...
- Android程序报错 Connection refused 处理
在用Android测试JSON数据解析的时候,报了这样一个异常: java.net.ConnectException: localhost/ - Connection refused 原来模拟器默认把 ...
- PROS Step:只需几分钟即可创建优化的价目表,并发现即时收益机会。
多年来,各个公司一直使用手动流程和电子表格来制定产品和服务定价,而没有真正意义上的方法或策略.在我写这篇文章时仍然如此! 但是,如今的形势已经改变.利用 PROS Step,公司可以将其数据上传到 M ...
- Android 自己主动化測试之------ Monkey工具
尽管 一般公司都有专门的測试人员,可是有时候 免不了 我们既要去开发产品,也要去測试产品,測试产品.有些机械化的 点界面的操作,谷歌已经给我们提供了工具.Monkey, 猴子測试. 什么是Monkey ...
- 在SSIS包中使用 Checkpoint从失败处重新启动包
使用SSIS做ETL的过程中会遇到各种各样的错误,对于一些大数据量的Job失败以后我们不希望重新运行,因为重新运行的时间开销是非常大的,我们只希望从失败的部分开始运行,这样可以省去很多的时间. SSI ...
- 用 rsync 同步本地和服务器的文件
参考 DigitalOcean 安装 For Debian/Ubuntu: sudo apt-get install rsync For OpenSUSE/Fedora: sudo yum insta ...
- POJ 2142:The Balance_扩展欧几里得(多组解)
先做出两个函数的图像,然后求|x|+|y|的最小值.|x|+|y|=|x0+b/d *t |+|y0-a/d *t| 这个关于t的函数的最小值应该在t零点附近(在斜率大的那条折线的零点附近,可以观察出 ...