[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 ...
随机推荐
- Struts学习之模型驱动
* 要从页面中获取表单元素的值,需要在动作类中声明与页面元素同名的属性.导致动作类中既有javabean又有业务方法. * 将javabean和业务方法进行分离: * 将重新创建一 ...
- hdu 4454 Stealing a Cake (三分)
Stealing a Cake Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- tomcat无法正常启动的一个原因
简要报错信息: java.lang.IllegalArgumentException: Document base E:\apache-tomcat-7.0.65\webapps\springmvc0 ...
- php框架
使用composer构建的php框架 github: https://github.com/Ev2le0/LeoFramework 实现功能: 1)路由 2)ORM 3)视图
- label 和 legend标签的用法
label 和 legend标签的用法 label标准用法: 一般浏览器都支持 一般而言,label标签位于表单元素的前面或者后面,为控件提供说明文字 <label for="user ...
- Qt Style Sheet实践(一):按钮及关联菜单(24K纯开源,一共四篇)
导读 正如web前端开发中CSS(Cascade Style Sheet)的作用一样,Qt开发中也可以使用修改版的QSS将逻辑业务和用户界面进行隔离.这样,美工设计人员和逻辑实现者可以各司其职而不受干 ...
- Struts2 一张图片引发的bug
今天如常的打开项目开放.写了一会保存测试.在登录时出了个错误当不影响正常使用.丫的昨天还好好的.行下手上的工作 开始找bug 错误核心代码如下: 10:34:46,442 WARN OgnlValu ...
- http://www.swoole.com/
Swoole:重新定义PHP PHP语言的高性能网络通信框架,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,数据库连接池,AsyncTask,消息队列,毫秒定时器, ...
- 《火球——UML大战需求分析》(0.2)——目录
说明: <火球——UML大战需求分析>是我撰写的一本关于需求分析及UML方面的书,我将会在CSDN上为大家分享前面几章的内容,总字数在几万以上,图片有数十张.欢迎你按文章的序号顺序阅读,谢 ...
- java的抽象类
现实世界中,人们表征世界时,会把现实世界中的很多类具有相同特征的事物归为一个抽象类.比如水果是许多植物果实的总称,我们可以定义一个苹果类.定义一个西瓜类,可以实例化一个苹果对象,可以实例化一个西瓜对象 ...