Monthly Expense

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 17982 Accepted: 7190

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

USACO 2007 March Silver

#include <set>
#include <map>
#include <list>
#include <stack>
#include <cmath>
#include <queue>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define PI cos(-1.0)
#define RR freopen("input.txt","r",stdin) using namespace std; typedef long long LL; const int MAX=100010; int n,m; int a[MAX]; bool Judge(int s)//二分查找
{
int sum=0;
int ans=1;
for(int i=1;i<=n;i++)
{
if(sum+a[i]<=s)
{
sum+=a[i];
}
else
{
sum=a[i];
ans++;
}
if(ans>m)
{
return false;
}
} if(ans<=m)
{
return true;
}
else
{
return false;
}
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
int L=0,R=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
R+=a[i];
L=max(L,a[i]);
}
int ans=R;
while(L<=R)
{
int mid=(L+R)>>1;
if(Judge(mid))
{
R=mid-1;
ans=mid;
}
else
{
L=mid+1;
}
}
cout<<ans<<endl;
}
return 0; }

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 (二分)

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

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

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

  5. POJ3273 Monthly Expense —— 二分

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

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

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

  7. POJ3273:Monthly Expense(二分)

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

  8. POJ 3273 Monthly Expense 二分枚举

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

  9. Monthly Expense(二分)

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

随机推荐

  1. Java基础之读文件——从文件中读取文本(ReadAString)

    控制台程序,使用通道从缓冲区获取数据,读取Java基础之写文件(BufferStateTrace)写入的charData.txt import java.nio.file.*; import java ...

  2. Java基础之读文件——使用输入流读取二进制文件(StreamInputFromFile)

    控制台程序,读取Java基础之读文件部分(StreamOutputToFile)写入的50个fibonacci数字. import java.nio.file.*; import java.nio.* ...

  3. Static Const

    Static 内部的 Const 不可变的 一般写法 在.m文件中, static NSString *const ID = @"shop"; static const CGFlo ...

  4. 最简单的Tcp Udp的例子

    //1.WinSock包含2个主要的版本,即WinSock1和WinSock2.前者需要包含头文件WinSock.h和库文件wsock32.lib,后者需要包含头文件WinSock2.h和库文件ws2 ...

  5. window.cookie

    本地测试cookie用火狐来测试 首先cookie是document上的一个属性. 先弹出一个cookie alert(document.cookie); //弹出是空的 设置cookie,格式是有一 ...

  6. JAVA-语法-运算符

    1.赋值运算符  =  (优先级较低) 2.算数运算符  +  —  *  /  % 3.字符串连接运算   +  (把其他类型转成字符串并和字符串类型进行连接) 4.扩展赋值运算符   +=   — ...

  7. 找区间连续值(HDU5247)

    找连续数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. 20145207《Java程序设计》第6周学习总结

    教材学习内容总结 一.输入/输出 InputStream与Outputstream • 串流设计的概念 从应用程序角度看,将数据从来源取出,可以使用输入串流,将数据写入目的地,可以使用输出串流:在Ja ...

  9. javascript 判断浏览器的ie版本,替换html标签

    /* var browser=navigator.appName var b_version=navigator.appVersion var version=b_version.split(&quo ...

  10. 夺命雷公狗---2016-linux---3之目录一般作用

    最好文件还是存放在自己的home目录下 建议:在很多时候如果需要系统备份最好去备份boot和etc这两个目录