POJ 3273 Monthly Expense(二分答案)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 36628 | Accepted: 13620 |
Description
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
Lines 2..N+1: Line i+1 contains the number of dollars Farmer
John spends on the ith day
Output
with.
Sample Input
7 5
100
400
300
100
500
101
400
Sample Output
500
Hint
Source
【题意】
给出农夫在n天中每天的花费,要求把这n天分作m组,每组的天数必然是连续的,要求分得各组的花费之和应该尽可能地小,最后输出各组花费之和中的最大值
【分析】
套路性二分
【代码】
#include<cstdio>
#include<algorithm>
#include<iostream>
#define debug(x) cerr<<#x<<" "<<x<<'\n';
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int N=1e5+5;
int n,m,d[N];
inline bool check(int now){
int res=1,sum=0;
for(int i=1;i<=n;i++){
if(sum+d[i]<=now){
sum+=d[i];
}else{
sum=d[i];
res++;
}
}
return res<=m;
}
int main(){
n=read();m=read();
int l=0,r=0,mid=0,ans=0;
for(int i=1;i<=n;i++) d[i]=read(),r+=d[i],l=max(l,d[i]);
while(l<=r){
mid=l+r>>1;
if(check(mid)){
ans=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
printf("%d\n",ans);
return 0;
}
POJ 3273 Monthly Expense(二分答案)的更多相关文章
- 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 二分枚举
题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...
- poj 3273 Monthly Expense (二分)
//最大值最小 //天数的a[i]值是固定的 不能改变顺序 # include <algorithm> # include <string.h> # include <s ...
- 二分搜索 POJ 3273 Monthly Expense
题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...
- POJ 3273 Monthly Expense 【二分答案】
题意:给出n天的花费,需要将这n天的花费分成m组,使得每份的和尽量小,求出这个最小的和 看题目看了好久不懂题意,最后还是看了题解 二分答案,上界为这n天花费的总和,下界为这n天里面花费最多的那一天 如 ...
- [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14158 Accepted: 5697 ...
- poj 3273 Monthly Expense(贪心+二分)
题目:http://poj.org/problem?id=3273 题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和. 思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max) ...
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
随机推荐
- DShow + OpenGL播放视屏
#include <DShow.h> #pragma include_alias( "dxtrans.h", "qedit.h" ) #define ...
- Nginx访问日志 Nginx日志切割 静态文件不记录日志和过期时间
- 动态为页面添加CSS样式文件引用
动态为页面添加CSS样式文件引用: if (document.createStyleSheet) { //IE document.createStyleSheet("./Themes/Def ...
- vue-cli打包之后的项目在nginx的部署
vue-cli执行 npm run build 进行打包,生成dist文件夹,把该文件夹下的文件直接复制到nginx服务器目录下,就可打开项目,但是只有首页是可以看到的,再刷新一下就404了,原因是v ...
- Nine-patch
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch This NinePatch defi ...
- 对转换公式为LaTeX代码要注意什么
mathtype是一款专业的数学公式编辑工具,理科生专用的工具.mathtype公式编辑器能够帮助用户在各种文档中插入复杂的数学公式和符号.可以轻松的将数学公式转换成LaTex代码,但是转换LaTeX ...
- MVC使用 Elmah 日志记录组件
在后台管理中,有一些操作是需要增加操作日志的,尤其是对一些比较敏感的金额类的操作,比如商城类的修改商品金额.删除商品.赠送金额等人工的操作.日志中记录着相关操作人的操作信息,这样,出了问题也容易排查. ...
- jQuery时间格式转换
http://www.cnblogs.com/ShaYeBlog/p/4129301.html
- SQL 语句判断记录是否存在(最简洁简单性能最优)
今天查了下,发现网上的没有一个sql语句写的好的. 判断记录是否存在,要不是语句不够简洁,要不就是性能有很大问题. 我进行了优化后,最简洁简单性能最优的的sql语句,用来判断表中的记录是否存在: se ...
- mysql 日志操作
https://www.cnblogs.com/dreamworlds/p/5478293.html http://www.jb51.net/article/76886.htm