POJ-3273 Monthly Expense (最大值最小化问题)
/* 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 (最大值最小化问题)的更多相关文章
- poj 3273"Monthly Expense"(二分搜索+最小化最大值)
传送门 https://www.cnblogs.com/violet-acmer/p/9793209.html 题意: 有 N 天,第 i 天会有 a[ i ] 的花费: 将这 N 天分成 M 份,每 ...
- Monthly Expense(最大值最小化问题)
POJ-3273 ...
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
- POJ 3273 Monthly Expense(二分查找+边界条件)
POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...
- 二分搜索 POJ 3273 Monthly Expense
题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
- [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14158 Accepted: 5697 ...
- OJ 21658::Monthly Expense(二分搜索+最小化最大值)
Description Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的.他已经计算了他以后N(1<=N< ...
- POJ 3273 Monthly Expense(二分答案)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...
随机推荐
- PHP常见带有下划线的常量
1.__PHP_Incomplete_Class <?php echo __PHP_Incomplete_Class::class; ?> __PHP_Incomplete_Class 2 ...
- IOS开发 多线程GCD
Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispat ...
- Hello Pythoner!
首先,欢迎你来到pyer的博客,希望你能有所收获! 然后,pyer之前学C#(原博客地址:初行-博客园),后来转的Python,目前从事服务端研发工作. 最后,相逢便是缘,如果看过pyer的博客后有什 ...
- js学习笔记知识点
AJAX用法安全限制JSONPCORS面向对象编程创建对象构造函数原型继承class继承 AJAX 用法 AJAX不是JavaScript的规范,它只是一个哥们“发明”的缩写:Asynchronous ...
- Centos6.x搭建lnmp环境
查看系统版本 #cat /etc/redhat-release CentOS release 6.7 (Final) 配置静态ip #vi /etc/sysconfig/network-scripts ...
- ZOJ3640Help Me Escape(师傅逃亡系列•一)(数学期望||概率DP)
Background If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at ...
- BZOJ5336 TJOI2018 party 【状压DP】*
BZOJ5336 TJOI2018 party Description 小豆参加了NOI的游园会,会场上每完成一个项目就会获得一个奖章,奖章 只会是N, O, I的字样.在会场上他收集到了K个奖章组成 ...
- .NET/C# 使用 Span 为字符串处理提升性能
.NET Core 2.1 和 C# 7.2 带来了 Span 的原生支持,原本需要使用不安全代码操作的内存块现在可以使用安全的方式来完成.此前在性能和稳定性上需要有所取舍,而现在可以兼得了. 简单的 ...
- Hexo+GitHub+Netlify一站式搭建属于自己的博客网站
喜欢的话请关注我的个人博客我在马路边https://hhongwen.cn/,此文为博主原创,转载请标明出处. 更好的阅读体验请点击查看:Hexo+GitHub+Netlify一站式搭建属于自己的博客 ...
- 使用 commander && inquirer 构建专业的node cli
备注: 比较简单就是使用nodejs 的两个类库帮助我们进行开发而已,具体的使用参考类库文档 1. 项目初始化 a. 安装依赖 yarn init -y yarn add commander in ...