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 element-ui 绑定@keyup事件无效
解决办法: <el-input @keyup.native="ajax"></el-input> 加上.native覆盖原有封装的keyup事件即可
- Python turtle绘制阴阳太极图代码解析
本文详细分析如何使用Python turtle绘制阴阳太极图,先来分解这个图形,图片中有四种颜色,每条曲线上的箭头表示乌龟移动的方向,首先从中心画一个半圆(红线),以红线所示圆的直径作半径画一个校园, ...
- 一、.NET Core MVC 项目结构模板
一.图文描述,开口干 二.文件结构: wwwroot 首先,Razor Pages项目中多了一个wwwroot的文件夹,这个文件夹中,主要存放网站的静态资源,如css,网站图片资源文件,js文件,三 ...
- 解析$(this).data('type');
html: <button type="button" class="layui-btn layui-btn-sm" data-type="ad ...
- linux按时间查询日志
在系统应用集中部署的时候,很多日志因为太多难以定位,获取某段时间的日志是对运维人员非常关键的事情. 一.sed查看某时间段到现在的系统日志: sed -n '/May 20 17/,$p' / ...
- 【python练习题】程序8
#题目:输出 9*9 乘法口诀表. for i in range(1,10): k = '' for j in range(1,i+1): k += '%s * %s = %s '%(i,j,i*j) ...
- Data Science With R In Visual Studio
R Projects Similar to Python, when we installed the data science tools we get an “R” section in our ...
- Idea中JavaWeb项目部署
1. 添加应用服务器tomcat 2. 将tomcat配置添加到项目中 artifacts配置:添加deploy, 添加artifacts,选择Web Application: Exploded &g ...
- BZOJ4477[Jsoi2015]字符串树——可持久化trie树
题目描述 萌萌买了一颗字符串树的种子,春天种下去以后夏天就能长出一棵很大的字符串树.字符串树很奇特,树枝上都密密麻麻写满了字符串,看上去很复杂的样子.[问题描述]字符串树本质上还是一棵树,即N个节点N ...
- 洛谷P1360 [USACO07MAR]黄金阵容均衡题解
题目 不得不说这个题非常毒瘤. 简化题意 这个题的暴力还是非常好想的,完全可以过\(50\%\)的数据.但是\(100\%\)就很难想了. 因为数据很大,所以我们需要用\(O(\sqrt n)\)的时 ...