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 ≤ MN) 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二分查找[最小化最大值问题]的更多相关文章

  1. poj 3273 Monthly Expense (二分搜索,最小化最大值)

    题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...

  2. POJ 3273 Monthly Expense(二分查找+边界条件)

    POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...

  3. POJ 3273 Monthly Expense(二分答案)

    Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...

  4. POJ 3273 Monthly Expense 二分枚举

    题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...

  5. poj 3273 Monthly Expense (二分)

    //最大值最小 //天数的a[i]值是固定的 不能改变顺序 # include <algorithm> # include <string.h> # include <s ...

  6. 二分搜索 POJ 3273 Monthly Expense

    题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...

  7. 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】

    链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  8. [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)

    Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14158   Accepted: 5697 ...

  9. Monthly Expense(二分查找)

    Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...

随机推荐

  1. 算法实战(六)Z 字形变换

    一.前言 之前因为第五题最长回文字符串需要使用到dp解法,所以我花了很长的时间来研究dp(因为每天又要上班,加上这段时间事情比较多,所以花了三个星期才搞定),好不容易算入了个门,有兴趣的同学可以看看我 ...

  2. Yota Phone宣告破产

    作为双面屏手机的开山鼻祖,Yota Phone已经消失在大家的视线中. 据外媒报道称,开曼群岛最高法院裁定在开曼群岛注册的YotaPhone手机生产商Yota Devices公司破产.法院的相关裁定被 ...

  3. POJ 2632:Crashing Robots

    Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8424   Accepted: 3648 D ...

  4. 16 react 发送异步请求获取数据 和 使用Redux-thunk中间件进行 ajax 请求发送

    1.发送异步请求获取数据 1.引入 axios ( 使用 yarn add axios 进行安装 ) import axios from 'axios'; 2. 模拟 在元素完成挂载后加载数据 并初始 ...

  5. PageHelper使用

    之前我们整合过SSM框架,可以查询数据库数据,项目中一般不会全部查询所有数据,为了美观和性能,都是采用分页形式查询数据 一:pom.xml导入pagehelper.jar <!-- https: ...

  6. Golang---BASE64编码原理

    BASE64编码概念 Base64 是一种基于64个可打印字符来表示二进制数据的表示方法.在 Base64中可打印字符包括字母 A-Z, a-z, 数字 0-9,这样共有 62 个字符,另外两个可打印 ...

  7. Nginx系列p2:重载,热部署,日志分割

    今天我们来学习 nginx 的 重载.热部署.日志分割功能 重载:当我们需要修改配置文件中的一些值,我们可以直接修改该配置文件,然后重新启动 nginx 服务,就可以实现在 nginx 不停止服务的情 ...

  8. POJ 1651:Multiplication Puzzle 矩阵相乘式DP

    Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7118   Accepted:  ...

  9. pytorch安装及基本用法

    20180425更新  安装pytorch0.4.0: conda uninstall pytorch # 如果是CUDA版本的话 conda uninstall cuda80 cuda90 # 如果 ...

  10. ..\OBJ\LED.axf: Error: L6218E: Undefined symbol EXTI_Init (referred from exti.o). 错误修改

    今天在移植野火的程序到元子的开发平台上时候,发现自己在中断初话中断函数的时候出现了:..\OBJ\LED.axf: Error: L6218E: Undefined symbol EXTI_Init ...