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

  1. 7 5
  2. 100
  3. 400
  4. 300
  5. 100
  6. 500
  7. 101
  8. 400

Sample Output

  1. 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.得到的下限或者上限即为最优解。

  1. #include<stdio.h>
  2. #include<iostream>
  3. using namespace std;
  4. int ans[100005];
  5. int main()
  6. {
  7. int N,M;
  8. scanf("%d%d",&N,&M);
  9. int sum=0,MAX=0;
  10. for(int i=0;i<N;i++)
  11. {
  12. scanf("%d",&ans[i]);
  13. sum+=ans[i];
  14. MAX=max(MAX,ans[i]);
  15. }
  16. while(MAX<sum)
  17. {
  18. int mid=(MAX+sum)/2;
  19. int s=0,cnt=0;
  20. for(int i=0;i<N;i++)
  21. {
  22. s+=ans[i];
  23. if(s>mid)//此时的最优解为mid,因为MAX!=sum
  24. //if(s>MAX)
  25. {
  26. cnt+=1;s=ans[i];
  27. }
  28. }
  29. if(cnt<M) sum=mid;
  30. else MAX=mid+1;
  31. }
  32. //跳出循环后 则MAX==sum
  33. printf("%d\n",sum);
  34. return 0;
  35. }

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. HTTP TCP UDP ICMP IP ARP 协议详解(10.15 第二十一天)

    ARP协议 ARP(Address Resolution Protocol)协议 地址解析协议 把网络层的IP地址翻译成在数据链路层寻址的48位硬件地址(MAC地址) 在OSI模型中ARP协议属于链路 ...

  2. 现在购买5G“商用手机”值不值呢?是花冤枉钱还是提前享受?

    刚刚发布的"安卓最强机皇"华为Mate 20系列依然没有支持5G,只是在收割最后的4G红利.相比之下,即将发布的小米MIX 3,看起来亮点还是颇多的.除了滑盖摄像头.屏占比更高的全 ...

  3. Neo4j--节点的增删查改基本用法

    注 node-name 和  label-name node-name 有点句柄的味道. 从面向对象来理解,label-name相当于一个类,node-name相当于这个类的对象. 类比关系型数据库的 ...

  4. POJ 3784 Running Median【维护动态中位数】

    Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...

  5. MySQL的DDL和DML

    SQL语句:结构化查询语句,使用SQL与数据库“沟通”,完成相应的数据库操作. 语句分类 DDL(Data Definition Languages)语句:即数据库定义语句,用来创建数据库中的表.索引 ...

  6. python进阶(三)~~~装饰器和闭包

    一.闭包 满足条件: 1. 函数内嵌套一个函数: 2.外层函数的返回值是内层函数的函数名: 3.内层嵌套函数对外部作用域有一个非全局变量的引用: def func(): print("=== ...

  7. hibernate结果集多种映射方案

    String sql = "select marker_no AS markerNo,name from lv_marker"; String sqlMo = "sele ...

  8. css选择器,选择指定属性的值

    选择属性为href的值: <a class='test' href='www.baidu.com' >test</a> response.css('.test::attr(hr ...

  9. javaweb04 ServletRequest&ServletResponse

    WEB浏览器与WEB服务器之间的一问一答的交互过程必须遵循一定的规则,这个歌规则就是 HTTP协议HTTP协议是超文本传输协议,它是TCP/IP协议集中的一个应用层协议,用于定义WEB浏览器与WEB服 ...

  10. JavaScript—面向对象 贪吃蛇_2 游戏对象

    游戏对象 function Game(map) { this.map = map; this.food = new Food(this.map) this.snake = new Snake(this ...