描述


http://poj.org/problem?id=3273

共n个月,给出每个月的开销.将n个月划分成m个时间段,求m个时间段中开销最大的时间段的最小开销值.

Monthly Expense
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21446   Accepted: 8429

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.

Source

分析


二分.

最小化最大值.

和"河中跳房子","Agressive Cows"等最大化最小值问题正好相反的最小化最大值问题,同样用二分解决,原理基本相同,差别主要在C条件的判断上.

1.最大化最小值:

相当于n个东西分给m个人,使得每个人至少拿x个,那么每个人拿够了就走,给后面的人多留一点,只要能分够>=m个人就是true,多的全扔给最后一个人就是了.

2.最小化最大值:

相当于n个东西分给m个人,每个人至多能拿x个,那么每个人尽可能多拿一点,给后面的人少留一点,只要能使<=m个人分完这n个东西就是true,之后每个人随便拿一点给没有拿到的人就是了.

注意:

1.如上所述两种问题的区别.

2.关于初始值.有些题直接给前缀和(如之前提到的两题),那样最小值的最大值就是平均值,最大值的最小值也是平均值,但像这道题,如果算sum可能会炸,而且不知道会炸到啥程度,所以不能瞎搞,只能乖乖设定y为0x7fffffff,x为单点最大值.

对于可以计算sum的题有:

<1>.最大化最小值:

x为单点最小值,y为平均值.

<2>.最小化最大值:

x为平均值与单点最大值两者中大的那一个,y为sum.其实直接令x=0,y=0x7fffffff即可,不过循环32次而已...

p.s.看懂别人的代码不重要,重要的是要理解算法的思想,不同的代码只是实现算法的方式不同与个人喜好问题而已,要真正想明白,把东西转化成自己的.

  1. #include<cstdio>
  2. #include<algorithm>
  3. using std :: max;
  4.  
  5. const int maxn=;
  6. int n,m,maxa=;
  7. int a[maxn];
  8.  
  9. void init()
  10. {
  11. scanf("%d%d",&n,&m);
  12. for(int i=;i<=n;i++)
  13. {
  14. scanf("%d",&a[i]);
  15. maxa=max(maxa,a[i]);
  16. }
  17. }
  18.  
  19. bool C(int x)
  20. {
  21. int sum=,ans=;
  22. for(int i=;i<=n;i++)
  23. {
  24. sum+=a[i];
  25. if(sum>x)
  26. {
  27. sum=a[i];
  28. ans++;
  29. }
  30. }
  31. if(ans<=m) return true;
  32. return false;
  33. }
  34.  
  35. void solve()
  36. {
  37. int x=maxa,y=0x7fffffff;
  38. while(x<y)
  39. {
  40. int m=x+(y-x)/;
  41. if(C(m)) y=m;
  42. else x=m+;
  43. }
  44. printf("%d\n",x);
  45. }
  46.  
  47. int main()
  48. {
  49. freopen("monthly.in","r",stdin);
  50. freopen("monthly.out","w",stdout);
  51. init();
  52. solve();
  53. fclose(stdin);
  54. fclose(stdout);
  55. return ;
  56. }

POJ_3273_Monthly_Expense_(二分,最小化最大值)的更多相关文章

  1. POJ_3104_Drying_(二分,最小化最大值)

    描述 http://poj.org/problem?id=3104 n件衣服,第i件衣服里面有水a[i],自然风干每分钟干1个水,用吹风机每分钟干k个水,但是同时只能对一件衣服使用吹风机,求干完所有衣 ...

  2. Monthly Expense(二分--最小化最大值)

    Farmer John is an astounding accounting wizard and has realized he might run out of money to run the ...

  3. POJ3273-Monthly Expense (最小化最大值)

    题目链接:cid=80117#problem/E">click here~~ [题目大意] 农夫JF在n天中每天的花费,要求把这n天分作m组.每组的天数必定是连续的.要求分得各组的花费 ...

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

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

  5. OJ 21658::Monthly Expense(二分搜索+最小化最大值)

        Description Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的.他已经计算了他以后N(1<=N< ...

  6. POJ 3273 Monthly Expense二分查找[最小化最大值问题]

    POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...

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

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

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

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

  9. 洛谷 P1462 通往奥格瑞玛的道路 Label: 最小化最大值 && spfa (存多条边示例)

    题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描述 在艾泽拉斯, ...

随机推荐

  1. POJ 1276 Cash Machine -- 动态规划(背包问题)

    题目地址:http://poj.org/problem?id=1276 Description A Bank plans to install a machine for cash withdrawa ...

  2. 在Mac OS X中使用VIM开发STM32(1)

       本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重!     在我先前的博文⎣在Mac OS X中搭建STM32开发环境⎤中,我们在Mac中DIY出了最 ...

  3. Json概述以及python对json的相关操作《转》

    什么是json: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programm ...

  4. 一套帮助你理解C语言的测试题(转)

    前言 原文链接:http://www.nowamagic.net/librarys/veda/detail/775 内容 在这个网站(http://stevenkobes.com/ctest.html ...

  5. ajax、json一些整理(1)

    1.请求text数据,在success事件中手动解析 前台:                 $.ajax({                     type: "post", ...

  6. PYTHON代码摘录

    文件处理 #典型的读取文件代码 row_data = {} with open('PaceData.csv') as paces: column_heading = paces.readline(). ...

  7. Android开发第1篇 - Android开发环境搭建

    归结一下,需要进行Android开发所需要的工具或软件: Eclipse - Android是基于JAVA的开发,所以选用目前来说使用较高的Eclipse作为IDE. ADT (Android Dev ...

  8. WPF的ScrollViewer鼠标的滚动

    在C# 中,两个ScrollViewer嵌套在一起或者ScrollViewer里面嵌套一个ListBox.Listview(控件本身有scrollviewer)的时候,我们本想要的效果是鼠标滚动整个S ...

  9. JAVA String 类

    java String类中的常用方法:public char charAt(int index)返回字符串中第index个字符:public int length()返回字符串的长度:public i ...

  10. 2016041601 - linux上安装maven

    在linux系统中安装maven,个人目前使用ubuntu15.1系统. 要想使用maven,前提条件必须配置好java. 1.检查java信息. 命令:echo $JAVA_HOME 和java - ...