poj 3273"Monthly Expense"(二分搜索+最小化最大值)
https://www.cnblogs.com/violet-acmer/p/9793209.html
题意:
有 N 天,第 i 天会有 a[ i ] 的花费;
将这 N 天分成 M 份,每份包含 1 天或连续的多天;
每份的花费为包含的天数花费的加和,求最大花费的最小值。
题解:
二分搜索答案。
AC代码:
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=1e5+; int N,M;
int totMoney;
int a[maxn]; bool Check(int mid)
{
int res=;
int curSum=;
for(int i=;i <= N;++i)
{
if(a[i] > mid)
return false;
if(curSum+a[i] > mid)
{
res++;
curSum=a[i];
}
else
curSum += a[i];
}
res++;//一定要 ++,for()当i == N 时,不会计算在res中
return res <= M ? true:false;
}
int Solve()
{
int l=,r=totMoney+;
while(r-l > )
{
int mid=l+((r-l)>>);
if(Check(mid))
r=mid;
else
l=mid;
}
return r;
}
int main()
{
// freopen("C:\\Users\\lenovo\\Desktop\\in.txt\\poj3273.txt","r",stdin);
scanf("%d%d",&N,&M);
totMoney=;
for(int i=;i <= N;++i)
{
scanf("%d",a+i);
totMoney += a[i];
}
printf("%d\n",Solve());
return ;
}
坑:
(1) : Check()中for( )后res为++
(2) : 题干中给的“the exact amount of money (1 ≤ moneyi ≤ 10,000)”貌似没啥用,需要自己判断二分的右边界
poj 3273"Monthly Expense"(二分搜索+最小化最大值)的更多相关文章
- OJ 21658::Monthly Expense(二分搜索+最小化最大值)
Description Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的.他已经计算了他以后N(1<=N< ...
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
- POJ 3273 Monthly Expense(二分搜索)
Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...
- poj 3273 Monthly Expense(二分搜索之最大化最小值)
Description Farmer John ≤ moneyi ≤ ,) that he will need to spend each day over the next N ( ≤ N ≤ ,) ...
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
- 二分搜索 POJ 3273 Monthly Expense
题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...
- POJ 3273 Monthly Expense(二分查找+边界条件)
POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...
- [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14158 Accepted: 5697 ...
- POJ 3273 Monthly Expense(二分答案)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...
随机推荐
- Vue学习目录
前面的话 近年来,前端框架发展火热,新的框架和名词不停地出现在开发者眼前,而且开发模式也产生了一定的变化.目前来看,前端MVVM框架的出现给开发者带来了不小的便利,其中的代表就有Angular.js. ...
- 【python练习题】程序4
# 题目:输入某年某月某日,判断这一天是这一年的第几天? import time year = input('输入年份: \n') month = input('输入月份: \n') day = in ...
- Asp.Net Core 输出 Word
In one of the ASP.NET Core projects we did in the last year, we created an OutputFormatter to provid ...
- c++ 实现拓扑排序
要简洁大方地实现拓扑排序,首先要了解两个标准模板 std::queue 和 std::vector 1 queue 添加头文件 #include<queue> 定义一个int类型的队列 q ...
- Civil 3D 二次开发 翻转曲面高程分析颜色
不解释,直接上代码及截图. [CommandMethod("RvsSEA")] public void ReverseSurfaceElevationAnalysis() { Ci ...
- BZOJ1430小猴打架——prufer序列
题目描述 一开始森林里面有N只互不相识的小猴子,它们经常打架,但打架的双方都必须不是好朋友.每次打完架后,打架 的双方以及它们的好朋友就会互相认识,成为好朋友.经过N-1次打架之后,整个森林的小猴都会 ...
- PHP——emjoin表情存入数据库
前言 还有一种解决的方法是更改数据库,这里就不写了,这里直接对emoji进行转码 代码 mb_strlen() | strlen() | rawurlencode() | rawurldecode() ...
- springMVC整理05--数据校验、格式化 & 其他注解 & 数据绑定流程
1. 数据校验.数据格式化 参考博客 http://www.importnew.com/19477.html 1.1 数据校验 使用 spring 数据校验,先要导入校验器的 jar: <! ...
- IDEA @Contract annotation
IDEA @Contract annotation ----------------------------------- http://www.jetbrains.com/help/idea/con ...
- LOJ#2019. 「AHOI / HNOI2017」影魔
题意: 在一个序列中 如果有一个子区间 它有一个端点是区间最大值 另一个端点不是这个区间的次大值 就会有p2的贡献 它两个端点分别是最大值次大值 就会有p1的贡献 我们发现这两个条件有一个重合的部分 ...