Monthly Expense
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 36628   Accepted: 13620

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.

Source

【题意】

给出农夫在n天中每天的花费,要求把这n天分作m组,每组的天数必然是连续的,要求分得各组的花费之和应该尽可能地小,最后输出各组花费之和中的最大值

【分析】

套路性二分

【代码】

Select Code

#include<cstdio>
#include<algorithm>
#include<iostream>
#define debug(x) cerr<<#x<<" "<<x<<'\n';
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int N=1e5+5;
int n,m,d[N];
inline bool check(int now){
int res=1,sum=0;
for(int i=1;i<=n;i++){
if(sum+d[i]<=now){
sum+=d[i];
}else{
sum=d[i];
res++;
}
}
return res<=m;
}
int main(){
n=read();m=read();
int l=0,r=0,mid=0,ans=0;
for(int i=1;i<=n;i++) d[i]=read(),r+=d[i],l=max(l,d[i]);
while(l<=r){
mid=l+r>>1;
if(check(mid)){
ans=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
printf("%d\n",ans);
return 0;
}
 

 

 

POJ 3273 Monthly Expense(二分答案)的更多相关文章

  1. POJ 3273 Monthly Expense二分查找[最小化最大值问题]

    POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...

  2. POJ 3273 Monthly Expense(二分查找+边界条件)

    POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...

  3. POJ 3273 Monthly Expense 二分枚举

    题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...

  4. poj 3273 Monthly Expense (二分)

    //最大值最小 //天数的a[i]值是固定的 不能改变顺序 # include <algorithm> # include <string.h> # include <s ...

  5. 二分搜索 POJ 3273 Monthly Expense

    题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...

  6. POJ 3273 Monthly Expense 【二分答案】

    题意:给出n天的花费,需要将这n天的花费分成m组,使得每份的和尽量小,求出这个最小的和 看题目看了好久不懂题意,最后还是看了题解 二分答案,上界为这n天花费的总和,下界为这n天里面花费最多的那一天 如 ...

  7. [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)

    Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14158   Accepted: 5697 ...

  8. poj 3273 Monthly Expense(贪心+二分)

    题目:http://poj.org/problem?id=3273 题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和. 思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max) ...

  9. poj 3273 Monthly Expense (二分搜索,最小化最大值)

    题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...

随机推荐

  1. Node.js安装和入门 - 2行代码让你能够启动一个Server

    转自:http://josh-persistence.iteye.com/blog/1979552  备忘 Node.js是一个轻松构建快速,可扩展的网络应用平台建立在Chrome的JavaScrip ...

  2. DEFINE_PER_CPU,如何实现“数组”

    引述自:http://www.unixresources.net/linux/clf/linuxK/archive/00/00/47/91/479165.html Kevin.Liu 的<调度器 ...

  3. 【Spark】session 代替 SparkConf、SparkContext和SQLContext

    http://www.raincent.com/content-85-7196-1.html

  4. 【DeepLearning】汉字手写体识别

    http://blog.topspeedsnail.com/archives/10897 两个双卷积池化 + dropout + 2个全链接 + softmax

  5. EF跨库查询,DataBaseFirst下的解决方案

    出于各种原因,有时需要跨数据库访问某些数据表,有同学已经给出了解决方案,比如  http://blog.csdn.net/hanjun0612/article/details/50475800 已经解 ...

  6. 浅谈java中"&&"和"&"的区别

    “&&”和”&”都是java中的逻辑运算符,并且它们都表示“逻辑与”即“同真则真,有一假则假”,它们的区别在于”&&”具有短路功能,即如果左边是false,则右 ...

  7. 19 Go的全能ORM简单入门

    gorm 昨天我的ldap账户改了以后,openfalcon(v2.1)-dashboard竟然无法登陆了!显然,没有把我的密码同步到本地数据库里面,怎么办?只能改openfalcon用户认证的源码了 ...

  8. 源码分析五(HashSet的内部实现)

    一:首先来看看Hashset的继承体系 public class HashSet<E> extends AbstractSet<E> implements Set<E&g ...

  9. [原] unity3d动态加载脚本

    本文记录如何通过unity3d进行脚本资源打包加载 1.创建TestDll.cs文件 public class TestDll : MonoBehaviour {    void Start () { ...

  10. nuget类库xml说明以及类库说明文件添加到包中

    1.nuget包制作添加xml操作:项目右键属性,生成配置输出xml文档文件,debug,release都配置一下,项目右键 yesway.redis.csproj 文件增加: 添加类库说明文件con ...