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. iOS UI控件继承关系图

    闲来无事,把UI控件的继承关系图整理下来,供自己和大家使用.

  2. Sql Server 中事务(begin tran/commit tran/rollback tran)的用法

    ALTER PROCEDURE [dbo].[Proc_Test_commit1]     @result int output, --成功 1; 失败 0     @message nvarchar ...

  3. Maven Integration for Eclipse 正确地址

    m2eclipse has moved from sonatype to eclipse. The correct update site is http://download.eclipse.org ...

  4. jquery parseInt()的问题

    对于parseInt("01")到parseInt("07");都能得到正确的结果,但如果是parseInt("08") 或parseInt ...

  5. HTML5媒体播放说明

    HTML5中video标签播放m3u8整理 http://www.xue163.com/588880/39097/390970871.html 移动端HTML5<video>视频播放优化实 ...

  6. copy file using FileReader/Writer.

    The code below demonstates copying file using 'FileReader' and 'FileWriter'. class CopyV2 extends Ti ...

  7. Wireshark提示没有一个可以抓包的接口

    这是由于win下默认NPF服务是关闭的,需要以管理员的身份开启这个服务 Windows上安装wireshark时,会遇到NPF驱动无法启动的情况,一般如果采用管理员的方式就可以正常启动,或者是将NPF ...

  8. PHP 数组转JSON数据(convert array to JSON object);

    <?php header('Content-type: appliction/json; charset=shift-JIS'); $data =array(); class Test { pu ...

  9. Ubuntu14.04+CUDA6.5环境下神经网络工具包Deepnet配置

    deepnet是多伦多大学计算机系机器学习组开发的一个神经网络工具包,可以进行以下计算: 1.  Feed-forward Neural Nets 2.  Restricted Boltzmann M ...

  10. POJ1611-The Suspects-ACM

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 23002   Accepted: 11171 De ...