POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题)
题目:Monthly Expense
Description
Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.
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
Line 1: Two space-separated integers: N and M
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Line 1: The smallest possible monthly limit Farmer John can afford to live with.
Sample Input
7 5
100
400
300
100
500
101
400
Sample Output
500
Hint
If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.
思路:
1.先找出要二分的上下限,上限为所有天数花费的总和,下限为其中的最大话费,所要查找的最优解就在上限和下限之间
2.在上限下限之间进行二分,若得到的堆数大于题目所给的堆数,则应将mid+1赋值为下限,否则应将mid赋值为上限。
3.二分时得到堆数的判断,当总和大于mid时,堆数进行加一,而将总和重新进行赋值,否则总和一直相加,直到大于mid。
4.得到的下限或者上限即为最优解。
#include<stdio.h>
#include<iostream>
using namespace std;
int ans[100005];
int main()
{
int N,M;
scanf("%d%d",&N,&M);
int sum=0,MAX=0;
for(int i=0;i<N;i++)
{
scanf("%d",&ans[i]);
sum+=ans[i];
MAX=max(MAX,ans[i]);
}
while(MAX<sum)
{
int mid=(MAX+sum)/2;
int s=0,cnt=0;
for(int i=0;i<N;i++)
{
s+=ans[i];
if(s>mid)//此时的最优解为mid,因为MAX!=sum
//if(s>MAX)
{
cnt+=1;s=ans[i];
}
}
if(cnt<M) sum=mid;
else MAX=mid+1;
}
//跳出循环后 则MAX==sum
printf("%d\n",sum);
return 0;
}
POJ 3273 Monthly Expense二分查找[最小化最大值问题]的更多相关文章
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
- POJ 3273 Monthly Expense(二分查找+边界条件)
POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...
- POJ 3273 Monthly Expense(二分答案)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...
- 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 ...
- 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】
链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14158 Accepted: 5697 ...
- Monthly Expense(二分查找)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...
随机推荐
- apt-get install oracle-java8-installer时Err:7 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial/main amd64 Packages 404 not found
所有其他网址都有效,而不是amd64端点. 然后,当运行apt-get install oracle-java8-installer时,出现以下错误: Package oracle-java8-ins ...
- 方便快捷组织页面 DOM 的 js 引模板擎 —— doT.js 的使用
—————————————————————————————————————————— ——————————————————————————————————————————
- Hadoop家族系统学习路线
本文主要介绍Hadoop家族产品,常用的项目包括Hadoop,Hive,Pig,HBase,Sqoop,Mahout,Zookeeper,Avro,Ambari,Chukwa,新增加的项目包括,YAR ...
- Python logging模块 控制台、文件输出
步骤 导入logging模块 设置level(此处是DEBUG) 添加文件handler和流handler import logging logger=logging.getLogger(__name ...
- JavaWeb之搭建自己的MVC框架(三)
1. 前言 在前两节的内容中,我们完成了一个基本的框架搭建.但是如果我们在前端请求中增加参数,我们要怎么传递到后台方法呢?接下来我们就来研讨这部分内容. 2. 实现 ( ...
- CCCC 正整数A+B
题意: 本题的目标很简单,就是求两个正整数A和B的和,其中A和B都在区间[1,1000].稍微有点麻烦的是,输入并不保证是两个正整数. 输入格式: 输入在一行给出A和B,其间以空格分开.问题是A和B不 ...
- Java基础知识点简记
此篇主要记录(搬运)的是Java中一些常见概念的理解,大致内容如下 final.finally.finalize的区别 Java的传值或者传引用的理解 重写Override和重载Overload的理解 ...
- shell脚本案例
1.MySQL数据库备份脚本,下面的脚本是Mysql全量备份+异地备份 一般Mysql数据库备份会采用在MYSQL从库上执行全量备份+增量备份方式.在从库备份避免Mysql主库备份的时候锁表造成业务影 ...
- 干货 | 用Serverless快速在APP中构建调研问卷
Serverless 计算将会成为云时代默认的计算范式,并取代 Serverful (传统云)计算模式,因此也就意味着服务器 -- 客户端模式的终结. ------<简化云端编程:伯克利视角下的 ...
- Neo4j图形数据库备份
Neo4j图形数据库备份 backup.sh文件 nowtime=`date +"%Y-%m-%d_%H_%M"` #原文件路径 sourcepath='/home/neo4j/n ...