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 ...
随机推荐
- thinkphp5 实现搜索分页能下一页保留搜索条件
正常情况下: 搜索后分页了,点击第二页,进入页面之前的搜索条件没有了. 如代码 $keywords=$this->request->param('keywords'); $this-> ...
- Python——数组模块(array)
一.模块说明 array模块是python中实现的一种高效的数组存储类型.它和list相似,但是所有的数组成员必须是同一种类型,在创建数组的时候,就确定了数组的类型. 二.代码
- Python学习之路——day05
今日内容:1.可变与不可变类型:可变类型:值可以改变,但是id不变,证明就是在改变原值,是可变类型不可变类型:值改变,但是id也跟着改变,证明是残生了新的值,是不可变类型 2.数字类型2.1整型:记录 ...
- Nginx 针对建立TCP连接优化
L:124 sysctl -a | grep file-max //通过命令查看系统最大句柄数 [root@3 ~]# sysctl -a | grep file-max fs.file-max = ...
- Spring MVC 使用介绍(三)—— Controller接口控制器
一.概述 Controller接口类图如下,其中,BaseCommandController已从Spring 4移除 基于继承Controller接口的方式已经不推荐使用,仅供学习参考 二.基于Con ...
- Ubuntu基于zsh自定义设置shell主题
为优化Ubuntu命令行页面效果,Google一番选择github上开源项目zsh,以自定义Shell主题,注:本机系统为Ubuntu 14.04 1.安装zsh sudo apt-get insta ...
- ContOS 常用命令
文件与目录操作 命令 解析 cd /home 进入 ‘/home’ 目录 cd .. 返回上一级目录 cd ../.. 返回上两级目录 cd - 返回上次所在目录 cp file1 file2 将fi ...
- Server socket
用法都一样 区别: self.request TCP self.requsst 代表具体的链接 UDP self.requst 代表一个小元组(元组里面: 第一个元素 客户端发来的数据 ,第 ...
- Python FAQ
1.在函数a中又定义了函数sum,内部函数sum可以引用外部函数a的参数n,不能这样写n=n+1,两个会出错,这样写s=s+n可以 解决: def a(): n = 1 def sum(): nonl ...
- JXOI 2018 简要题解
目录 「JXOI2018」游戏 题意 题解 代码 「JXOI2018」守卫 题意 题解 代码 「JXOI2018」排序问题 题意 题解 代码 总结 「JXOI2018」游戏 题意 可怜公司有 \(n\ ...