描述


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

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.

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

 #include<cstdio>
#include<algorithm>
using std :: max; const int maxn=;
int n,m,maxa=;
int a[maxn]; void init()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
maxa=max(maxa,a[i]);
}
} bool C(int x)
{
int sum=,ans=;
for(int i=;i<=n;i++)
{
sum+=a[i];
if(sum>x)
{
sum=a[i];
ans++;
}
}
if(ans<=m) return true;
return false;
} void solve()
{
int x=maxa,y=0x7fffffff;
while(x<y)
{
int m=x+(y-x)/;
if(C(m)) y=m;
else x=m+;
}
printf("%d\n",x);
} int main()
{
freopen("monthly.in","r",stdin);
freopen("monthly.out","w",stdout);
init();
solve();
fclose(stdin);
fclose(stdout);
return ;
}

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. [.Net MVC] Win7下IIS部署

    这里简单的分三步实现网站的部署. 一.发布 VS2013中有发布选项,在需要发布的工程项目上(就是设置为启动项目的那个)右键,点“发布”选项: 然后会弹出一个窗口: 选择自定义,随便输入一个名字,然后 ...

  2. fgetc, getchar(), fscanf的问题

    1.漫谈:为什么 函数fscanf(FILE stream, const char format, ...)的 第3个参数中 总是用变量的地址 或者是用指针. 这个问题涉及到 传值 和 传指针.一般情 ...

  3. spring常用的连接池属性文件配置

    (一) DBCP 引入jar文件 commons-dbcp-1.2.1.jar commons-pool-1.3.jar二个包. spring配置文件 <bean id="dataSo ...

  4. Python3 网络编程

    虽然大家现在对互联网很熟悉,但是计算机网络的出现比互联网要早很多. 计算机为了联网,就必须规定通信协议,早期的计算机网络,都是由各厂商自己规定一套协议,IBM.Apple和Microsoft都有各自的 ...

  5. php获取汉字首字母的函数

    本文介绍用php实现汉字转化为首字母的方法,主要功能是:功能明确,易于修改维护和扩展: 英文的字串:不变返回(包括数字):中文字符串:返回拼音首字符: 中英混合串: 返回拼音首字符和英文. 网上的方法 ...

  6. 初探—KMP模式匹配算法

    KMP算法思想: 普通的字符串匹配算法S主串必须要回溯.但回溯就影响了效率. 改进的地方也就是这里,我们从P 串本身出发,事先就找准了T自身前后部分匹配的位置,那就可以改进算法. next数组的含义: ...

  7. hdu 5648 DZY Loves Math 组合数+深搜(子集法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5648 题意:给定n,m(1<= n,m <= 15,000),求Σgcd(i|j,i&am ...

  8. 【BZOJ】1012: [JSOI2008]最大数maxnumber 树状数组求区间最值

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1012 题意:维护一个数列,开始时没有数值,之后会有两种操作, Q L :查询数列末 ...

  9. org.springframework.web.servlet.view.InternalResourceViewResolver

    http://blog.csdn.net/superdog007/article/details/28857495 我们在controller里面经常这样return一个ModelAndView: r ...

  10. windows store app 拷贝文件到pc目录

    (function () { "use strict"; WinJS.Binding.optimizeBindingReferences = true; var app = Win ...