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. elasticsearch下载与安装

    目录 安装之前 下载 安装 测试 安装之前 必须注意的是:安装路径不允许有中文及空格和非法字符,尤其是中文 下载 打开elasticsearch官网.选择免费试用. 选择对应产品与版本(选择6.5.4 ...

  2. 【转载】使用driver.findElement(By.id("txtPhoneNum")).getText();获取文本

    今天在写自动化测试脚本的时候要获取一个输入框中的文本写了如下脚本: getAndSwitch("http://cas.minshengnet.com:14080/register/eRegi ...

  3. UVA - 12186 Another Crisis(工人的请愿书)(树形dp)

    题意:某公司有1个老板和n(n<=105)个员工组成树状结构,除了老板之外每个员工都有唯一的直属上司.老板的编号为0,员工编号为1~n.无下属的员工(叶子)打算签署一项请愿书递给老板,但不能跨级 ...

  4. Java虚拟机之内存模型

    一.java并发基础 在并发编程中存在两个关键问题①线程之间如何通信 ②线程之间如何同步. 通信 通信是指线程之间以何种机制来交换信息.在命令式编程中,线程之间的通信机制有两种:共享内存和消息传递. ...

  5. PHP常用的数学函数和字符串函数

    PHP常用函数总结 数学函数 1.abs(): 求绝对值 $abs = abs(-4.2); //4.2 数字绝对值数字 2.ceil(): 进一法取整 echo ceil(9.999); // 10 ...

  6. 201909-2 小明种苹果(续) Java

    思路: 待补充 import java.util.*; public class Main { public static void main(String[] args) { Scanner sc ...

  7. MessageBox.Show的使用

    MessageBox.Show("内容","标题") // 摘要:// 使用指定的帮助文件.HelpNavigator 和帮助主题显示一个具有指定文本.标题.按 ...

  8. POJ 1836:Alignment

    Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14492   Accepted: 4698 Descri ...

  9. go 的参数传递

    再go语言中没有引用传递,所有都是按照值拷贝的方式传递的. 数组:实际就是堆栈上的一段连续内存,和c类似.(可以更加反编译代码推断 go tool compile -S main.go > ma ...

  10. Oracle 基础1

    oracle基础 表空间: Oracle数据库对数据的管理是基于表空间的概念来的, 各种数据的以及存储数据的优化, 实际上也是通过优化表空间来实现的 表空间分类: 永久表空间 用来存放表的数据, 视图 ...