/* Monthly Expense
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10757 Accepted: 4390
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
*/
/*坑啊,二分法绝对不能用递归来做,应该用非递归while循环做*/
#include <iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define maxn 100100
int a[maxn],N,M,x,y,minx;
void binary(int x1)
{
if(x>y)
return ;
int sum=;
int t=;
for(int i=; i<N;)
{
if(sum+a[i]>x1)
{
t++;
sum=;
}
else
{
sum+=a[i];
if(i==N-)
t++;
i++;
}
}
int mid;
if(t<=M)
{
minx=x1;
//minx= min(x1,minx);
//if(x1>minx)
//return ;
y=x1-;
mid=(x+y)>>;
binary(mid);
}
if(t>M)
{
x=x1+;
mid=(x+y)>>;
}
binary(mid);
}
int main()
{
while(~scanf("%d%d",&N,&M))
{ y=,x=;
for(int i=; i<N; i++)
{
scanf("%d",&a[i]);
if(a[i]>x)
x=a[i];
y+=a[i];
}
minx=;
binary((x+y)>>);
printf("%d\n",minx);
}
return ;
}
//非递归做的
#include <iostream>
#include <algorithm>
using namespace std;
int money[];
int main()
{
int N;//总共的天数
int M;//分成fajomonths月数
cin>>N>>M;
int low=;
int high=;
for(int i=;i<=N;i++)
{
cin>>money[i];
high+=money[i];
if(money[i]>low)
low=money[i];
}
int mid;
while(low<=high)
{
mid=(low+high)/;
int sum=;
int duanshu=;
for(int i=;i<=N;i++)
{
sum+=money[i]; if(sum<=mid)
;//计算在月最大消费控制在mid下时能分的段数
else
{
sum=money[i];
duanshu++;
}
}
if(duanshu>M)//分的段数比需要的大,说明标准高了
low=mid+;
else if(duanshu<M)//分的段数比需要的小,说明标准低了
high=mid-;
else if(duanshu==M)//分的段数和需要的相同,这时还有可能再严格一点,即使每月的最大支出更小一点
high=mid-;
} cout<<low<<endl;
return ;
}

POJ-3273 Monthly Expense (最大值最小化问题)的更多相关文章

  1. poj 3273"Monthly Expense"(二分搜索+最小化最大值)

    传送门 https://www.cnblogs.com/violet-acmer/p/9793209.html 题意: 有 N 天,第 i 天会有 a[ i ] 的花费: 将这 N 天分成 M 份,每 ...

  2. Monthly Expense(最大值最小化问题)

                                                                                POJ-3273                 ...

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

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

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

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

  5. 二分搜索 POJ 3273 Monthly Expense

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

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

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

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

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

  8. OJ 21658::Monthly Expense(二分搜索+最小化最大值)

        Description Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的.他已经计算了他以后N(1<=N< ...

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

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

随机推荐

  1. Shell 命令行实现将一个站点页面全部下载到本地并替换其中链接的脚本

    Shell 命令行实现将一个站点页面全部下载到本地并替换其中链接的脚本 不知道为什么,我总想用 Shell 脚本来实现把一个站点内容给下载下来.但是下载什么站点我确不知道.今天尝试了一下利用 curl ...

  2. 使用kibana来进行ElasticSearch的信息查询检索

    大家经常会听到使用ELK搭建日志管理平台.完成日志聚合检索的功能,那么这个平台到底是个什么概念,怎么搭建,怎么使用呢? ELK包括ElasticSearch(数据存储.快速查询).logstash(日 ...

  3. Node 抓取非utf-8编码页面

    代码示例 Nodejs抓取非utf8字符编码的页面 -- Ruby's Louvre var http = require('http'); var iconv = require('iconv-li ...

  4. JAVA怎么在函数内改变传入的值

    public class TestInt { public int aa(int i) { return i+4; } public static void main(String [] args) ...

  5. EP-N8530S USB WIFI 驱动移植

    /*********************************************************************** * EP-N8530S USB WIFI 驱动移植 * ...

  6. learn Linux sed command

    learn Linux sed command 一.参考文档: . sed命令详解 http://qifuguang.me/2015/09/21/sed%E5%91%BD%E4%BB%A4%E8%AF ...

  7. storyboard出口回退问题

    问题 直接在Main.storyboard拖动添加到出口的时候总是出一些不知名的错误.猜想可能是swift4又TM换了新特性(不过好喜欢啊哈哈哈哈) 解决 其实可以先拖动添加@IBAction函数到代 ...

  8. Mysql按照字段值做分组行转列查询

    今天做个后台服务,有个需求是批量生成一批表的数据,如果用BulkInsert会提升很大一截提交效率,但是如果用循环构造提交的Datable,则算法开销太高,所以用这种查询批量查出符合格式的DataTa ...

  9. js 值和引用

    js对值和引用的赋值/传递在语法上没有区别,完全根据值得类型决定 简单值(即标量基本类型值),总是通过值复制的方式来赋值/传递,包括null,undefined,字符串,数字,布尔值和ES6中的sym ...

  10. 每天一个linux命令(目录文件操作):【转载】Linux 目录结构

    对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统至关重要,下面 ...