Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11196   Accepted: 4587

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

题意简化后的意思就是给出N个数,将这N个数分成M份,每份必须是连续的一个或几个数,要求分的各份的数之和尽可能小,求出M份和中的最大值;
注意,也许在low == high 之前已分成m组,但可能不是最优的,当while(low < high)结束后得到的low肯定是最优结果;
 #include<stdio.h>
#include<string.h>
const int N = ;
int n,m;//把n个数分成m组;
int money[N+];
//计算当前mid值能把n个数分成的组数并与m比较;
bool judge(int mid)
{
int sum = money[];
int group = ; for(int i = ; i < n; i++)
{
if(sum + money[i] <= mid)
{
sum += money[i];
}
else
{
group++;
sum = money[i];
}
} if(group > m)
return false;
else return true;
} int main()
{
int low = ,high = ;//low为下界,high为上界;
int mid;
scanf("%d %d",&n,&m);
for(int i = ; i < n; i++)
{
scanf("%d",&money[i]);
high += money[i];//high初始化为n个数的和,相当于把n个数分成一组;
if(low < money[i])
low = money[i];//low初始化为n个数中的最大值,相当于把n个数分成n组;
} mid = (low+high)/;
while(low < high)
{
if(!judge(mid))//如果mid把n个数分得的组数大于m,说明mid偏小,更新low;
{
low = mid+;
}
else high = mid-;//否则说明mid偏大,更新high;
mid = (low+high)/;
}
printf("%d\n",mid);
return ;
}

 

Monthly Expense(二分)的更多相关文章

  1. 【POJ 3273】 Monthly Expense (二分)

    [POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...

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

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

  3. Monthly Expense(二分查找)

    Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...

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

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

  5. POJ 3273 Monthly Expense(二分答案)

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

  6. POJ3273 Monthly Expense —— 二分

    题目链接:http://poj.org/problem?id=3273   Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Tota ...

  7. POJ 3273:Monthly Expense 二分好题啊啊啊啊啊啊

    Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19207   Accepted: 7630 ...

  8. POJ3273:Monthly Expense(二分)

    Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...

  9. POJ 3273 Monthly Expense 二分枚举

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

随机推荐

  1. JAVA正则忽略大小写

    java正则表达式:  (?i)abc  表示abc都忽略大小写  a(?i)bc   表示bc忽略大小写  a((?i)b)c   表示只有b忽略大小写 也可以用Pattern.compile(re ...

  2. css 权威指南笔记(四)选择器

    规则结构 每个规则都有两个基本部分组成:选择器和声明块.声明块由一个或多个声明组成,每个声明则是一个属性-值对. 元素选择器 声明和关键字 关键字一般由空格隔开:有一种情况例外 font属性中的  斜 ...

  3. iOS原生CIFilter创建二维码

    iOS原生CIFilter创建二维码 2016-05-31 未来C iOS原生CIFilter创建二维码 关于二维码生成,网上也是有很多,很早以前的第三方库大多数都是通过C++写,也是有的如zxing ...

  4. PHP错误类型及屏蔽方法

    1. 注意(Notices)这些都是比较小而且不严重的错误,比如去访问一个未被定义的变量.通常,这类的错误是不提示给用户的,但有时这些错误会影响到运行的结果. 2. 警告(Warnings)这就是稍微 ...

  5. CakePHP之控制器

    控制器 控制器是MVC中的“C”. 如果你的网站使用Cake框架制作,一般根据url地址和通过路由,就会找到正确的控制器,然后控制器的动作就会被调用. 一个控制器需要解释请求数据.确保使用正确的模型. ...

  6. 用CSS+Jquery实现一个漂浮的窗体

    一.项目需求: 实现一个用于网站主页面 从窗体左上角开始飘到右下角 之后又飘到右上角 十秒之后消失的效果. 二.需求分析: 首先 应当想要漂浮的内容放在一个容器内,也就是一个DIV,设计它的样式,不管 ...

  7. DOM - nodeType 的取值

    DOM 中,共有 12 中不同类型的节点,nodeType 的取值以数值表示. 节点类型 描述 子节点 1  Element  表示元素.  Element, Text, Comment, Proce ...

  8. C#超时处理(转载)

    /// <summary>    /// 超时处理    ///    ///    /// </summary>    public class TimeoutChecker ...

  9. node 搭建开发框架express

    参考地址: http://www.itnose.net/detail/6095003.html 开发环境 E:\project> node -v v0.10.30 E:\project> ...

  10. 从零开始学java(猜数字游戏)

    练练手不喜勿喷,看到什么学习什么第一次发博客格式就见见谅.....                                            2016-07-21 19:55:02 imp ...