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基础之读文件——使用通道读取混合数据2(ReadPrimesMixedData2)

    控制台程序,本例读取Java基础之写文件部分(PrimesToFile2)写入的Primes.txt. 方法二:设置一个任意容量的.大小合适的字节缓冲区并且使用来自文件的字节进行填充.然后整理出缓冲区 ...

  2. ssh tunnel通道

    Secure SHell (SSH) 是一个通过网络登录其他计算机的程序,在远程服务器运行命令,和从一台机器移动文件到另一台.在不安全的网络中,它提供两台主机之间强大认证和安全加密的的通讯,被称为 S ...

  3. 最近兰州的js风格写个插件和一个template engine

    /* *@Product Name: Rational Framework Author: Calos Description: pager !important: pager */ (functio ...

  4. BI (商业智能)

    BI (商业智能) 编辑 商业智能(BI,Business Intelligence). BI(Business Intelligence)即商务智能,它是一套完整的解决方案,用来将企业中现有的数据进 ...

  5. PAT乙级 1005. 继续(3n+1)猜想 (25)

    1005. 继续(3n+1)猜想 (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 卡拉兹(Callatz ...

  6. Headless MSBuild Support for SSDT (*.sqlproj) Projects

    http://sqlproj.com/index.php/2012/03/headless-msbuild-support-for-ssdt-sqlproj-projects/ Update: bre ...

  7. SqlServer中使用Select语句给变量赋值的时候需要注意的一个问题

    我们知道在SqlServer中可以用Select语句给变量赋值,比如如下语句就为int类型的变量@id赋值 ; select @id=id from ( as id union all as id u ...

  8. Chrome常用快捷键

    F2F3 下一个标签 Ctrl+1-9 切换标签 Ctrl+W 关闭 Ctrl+D 保存书签 Ctrl+T 新建书签然后地址栏输入搜索 Ctrl+Shft+T 打开上次关闭 空格 下翻页 Ctrl+J ...

  9. 什么是BI【转】

    产品与服务 - 商务智能 目前,商业智能产品及解决方案大致可分为数据仓库产品.数据抽取产品.OLAP产品.展示产品.和集成以上几种产品的针对某个应用的整体解决方案     商业智能是什么? 简而言之, ...

  10. ActiveMQ实现负载均衡+高可用部署方案(转)

    本文转自:http://www.open-open.com/lib/view/open1400126457817.html%20 一.架构和技术介绍 1.简介 ActiveMQ 是Apache出品,最 ...