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 ...
随机推荐
- SQL约束(主键约束、外键约束、自动递增、不允许空值、值唯一、值默认、值限制范围)
NOT NULL 不允许空值约束 NOT NULL 约束强制列不接受 NULL 值(NULL值就是没有值或缺值).NOT NULL 约束强制字段始终包含值,即不向字段添加值,就无法插入新记录或者更新记 ...
- List接口方法
package cn.zhou.com; /* * List?-------是啥? Collection 的一个子接口! * * 集合?容器? * * 区分容器,每个容器的数据结构不一样! * 集合, ...
- Python——Menu控件
一.参数说明: tearoff :分窗,0为在原窗,1为点击分为两个窗口 bg,fg : 背景,前景 borderwidth: 边框宽度 font : 字体 activebackgound ...
- Python——Flask框架——Web表单
一.框架Flask-WTF 安装: pip install flask-wtf 需要程序设置一个密钥 app = Flask(__name__) app.config['SECRET_KEY'] = ...
- Web API2 使用EF Code Migrations to Seed DB
console Enable-Migrations 运行这个命令,添加一个文件夹Migrations,Configuration.cs在这文件夹中 打开 Configuration.cs file. ...
- maven 当两个工程合并后 他的classpath也合并了
maven 当两个工程合并后 他的classpath也合并了 也就是说资源文件环境合并了
- 5G到来,数据中心如何变革?
导读 5G将要到来,除改变人们的工作生活外,其带宽.延时.连接特性也逼迫着数据中心变革,以满足5G时代需求.具体而言,5G将从形状规模.硬件组成及软件规模三面变革数据中心. 5G带来什么 高带宽.低延 ...
- java excel Workbook API
此文摘自:http://blog.sina.com.cn/zenyunhai 1. int getNumberOfSheets() 获得工作薄(Workbook)中工作表(Sheet)的个数,示例: ...
- 微信小程序——demo合集及简单的文档解读【五】
官方Demo https://github.com/wechat-miniprogram/miniprogram-demo 其他Demo https://www.cnblogs.com/ytkah/p ...
- DRF 序列化组件
Serializers 序列化组件 Django的序列化方法 class BooksView(View): def get(self, request): book_list = Book.objec ...