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 ...
随机推荐
- Android程序员学WEB前端(2)-HTML(2)-锚点链接列表表单-Sublime
转载请注明出处:http://blog.csdn.net/iwanghang/article/details/76522417觉得博文有用,请点赞,请评论,请关注,谢谢!~锚点 链接 列表 表单 &l ...
- Java基础拾遗(一)
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/76358391冷血之心的博客) 马上就要秋招了,新的一轮笔试面试马上 ...
- 基于视觉的 SLAM/Visual Odometry (VO) 开源资料、博客和论文列表
基于视觉的 SLAM/Visual Odometry (VO) 开源资料.博客和论文列表 以下为机器翻译,具体参考原文: https://github.com/tzutalin/awesome-vis ...
- 基于EasyNVR实现RTSP/Onvif监控摄像头Web无插件化直播监控
前言 随着互联网的发展,尤其是移动互联网,基于H5.微信的应用越来越多,企业也更多地想基于H5.微信公众号来快速开发和运营自己的产品,那么传统的安防IPC所输出的各种RTSP.GB28181.SDK视 ...
- UE4中类自动生成代码解析
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/73189272 作者:car ...
- ss-libev 源码解析udp篇 (3)
本篇分析server_recv_cb,这个是udp转发中最重要的函数. server_recv_cb: 当ss-local或ss-server接收到来自前端的udp数据包时调用.这个函数代码比较多,除 ...
- [GitHub] git push的时候报错 fatal: unable to access 'http://github.com/xxx/xxx.git/': Recv failure: Connection reset by peer
参考了两种方法: 1. 解决fatal: unable to connect to github.com问题 http://blog.csdn.net/greenqingqingws/article/ ...
- PHP中 public、protected 和 privare的区别
权限:public>protected>private public:谁都可以调用 protected:只有自己和派生类可以调用 privare:只有自己可以调用
- 【剑指offer】数组中出现次数超过数组长度一半的数字,C++实现
原创博文,转载请注明出处! # 题目 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过 ...
- VS 工具箱 Dev控件显示
工具箱修复Dev控件显示 用VS2010新打开一个项目,居然发现工具箱里边没了Dev控件. 命令提示符定位到Dev控件的安装目录下的\Components\Tools文件夹下,执行如下命令 添加:To ...