描述


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. 利用 css 制作简单的提示框

    在网页开发中,为了提高用户体验,经常会用到一些提示框来引导用户,这里分享下一些简单的提示框的制作 1.首先类似一个长方形右上角一个关闭按钮 这里用到的主要是一些定位的知识,运用relative和abs ...

  2. OpenJudge/Poj 1159 Palindrome

    1.链接地址: http://bailian.openjudge.cn/practice/1159/ http://poj.org/problem?id=1159 2.题目: Palindrome T ...

  3. ssh: connect to host xxx.xxx.xxx.xxx port 22: Connection refused

    工具/原料:Ubuntu 在采用scp在不同机器之间进行文件拷贝时出现标题所示的错误,原因可能是: 1.sshd 未启动 2.sshd 未安装 3.防火墙 4需重新启动ssh 服务 查看sshd进程方 ...

  4. 简单重置Centos服务器中Mysql的root密码

    1.编辑MySQL配置文件my.cnf vi /etc/my.cnf #编辑文件,找到[mysqld],在下面添加一行skip-grant-tables [mysqld] skip-grant-tab ...

  5. javascripct数组

    定义数组 数组对象用来在单独的变量名中存储一系列的值. 我们使用关键词 new 来创建数组对象.下面的代码定义了一个名为 myArray 的数组对象: var myArray=new Array() ...

  6. Cassandra1.2文档学习(6)—— 客户端数据请求

    参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/architecture/a ...

  7. 用Cocos2d-x实现2D光线效果

    2015.3.23优化修改,现在已经能达到稳定60帧了.. 本博客地址:http://www.cnblogs.com/wolfred7464/ 创意来自于:http://ncase.me/sight- ...

  8. 深入浅出分析C#接口的作用

    1.C#接口的作用 :C#接口是一个让很多初学C#者容易迷糊的东西,用起来好像很简单,定义接口,里面包含方法,但没有方法具体实现的代码,然后在继承该接口的类里面要实现接口的所有方法的代码,但没有真正认 ...

  9. 一步步学习ASP.NET MVC3 (2)——入门程序

    请注明转载地址:http://www.cnblogs.com/arhat 在上一节中,我们只是简单的介绍了什么是MVC及MVC的运行原理.而本节呢,主要来实现下一ASP.NET MVC3的开发流程,并 ...

  10. 一步步学习ASP.NET MVC3 (13)——HTML辅助方法

    请注明转载地址:http://www.cnblogs.com/arhat 今天老魏是在十分郁闷,我的一个U盘丢了,心疼里面的资料啊,全部是老魏辛辛苦苦积攒的Linux资料,太心疼,到现在心情还不是很爽 ...