/* 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. Java多线程编程实战指南(核心篇)读书笔记(二)

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/76651408冷血之心的博客) 博主准备恶补一番Java高并发编程相 ...

  2. Kotlin Reference (一) Basic Syntax

    什么是Kotlin Kotlin翻译成中文叫"靠他灵",它是由JetBrains公司发明的一种基于JVM的编程语言,目前Google宣布kotlin为Android开发的官方语言. ...

  3. Happy Swifting!

    Happy Swifting! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...

  4. webpy/flask/bottle性能测试

    这三个都是Python WSGI的web开发框架,到底用哪个呢?单纯从性能的角度而言,可能哪个快就用哪个,但是这也不是绝对的.比如我就比较喜欢webpy的router配置放在一个文件中,而flask和 ...

  5. 【sklearn】性能度量指标之ROC曲线(二分类)

    原创博文,转载请注明出处! 1.ROC曲线介绍 ROC曲线适用场景 二分类任务中,positive和negtive同样重要时,适合用ROC曲线评价 ROC曲线的意义 TPR的增长是以FPR的增长为代价 ...

  6. C++题目(论述类)

    0.面向对象 三大特性:封装性.继承性.多态性 1.static  ①只进行一次初始化,而且保存在静态存储区,是在程序运行时就进行初始化了: ②当我们同时编译多个源文件(.c文件)时,所有未加stat ...

  7. Mysql查询架构信息

    今天想给整个数据库做初始化,也就是清空所有表,然后让索引归零,使用truncate table 就可以,但好多张表,怎么批量搞定呢? 有人说重建表吧,dump一下,然后再重建,但我还是想用trunca ...

  8. SpringDataJpa增删改查

    资料来源网址:http://www.cnblogs.com/hawell/p/SpringDataJpa.html Repository(几个常用的例子) @Repository public int ...

  9. hexo个人博客搭建

    遇见西门的个人博客 https://www.simon96.online/ 内容详细!

  10. Hive中日期处理

    1.日期函数UNIX时间戳转日期函数:from_unixtime() 函数 格式 返回值 说明 from_unixtime from_unixtime(bigint unixtime[, string ...