POJ3273⭐⭐

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 ≤ M ≤ N) 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.


FJ是一个惊人的会计奇才,他意识到他可能会花光经营农场的钱。他已经计算并记录的金额(1≤money i≤10000),他将每天需要花在接下来的N(1≤N≤100000)天。

FJ想要创建一个精确为M(1≤M≤N)个财政周期的序列集合,称为“fajomonths”。每个fajomonths都包含一个多个连续的天数。每一天都包含在一个fajomonth中

FJ的目标是安排fajmonth,使支出最高的fajmonth的支出最小化,从而确定他的每月支出限额。


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


第1行:两个用空格分隔的整数:N和M
行2 . .N+1:第i+1行是农民约翰在第i天花费的美元数 


Output

Line 1: The smallest possible monthly limit Farmer John can afford to live with.

第1行:农场主约翰能负担得起的最低每月限额。

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.


如果农夫约翰安排月份,前两天是一个月,第三和第四天是一个月,最后三天是他们自己的月,他在任何一个月最多花费500美元。任何其他的排班方法都会给出一个更大的每月最低限额。


题目摘要 (最小化最大值问题)

N天 [1,1e5]  

N天每天花的钱money[i]  [1,1e5]

将这N天的money分为M组,找出使得 当前分组下钱数和的最大值的一组 钱数和最小 的分组方式

解题思路

用二分法来找出答案。(二分范围明确)

对钱进行二分,可以找到一个ans,<ans的钱一定至少可以分为M组,>ans的钱不够分为M组。

那么如何找到找到这个ans?\rightarrow判断条件为int P (int m)

int P(int m)
{
	int sum_sep=0;
	int group=1;
	for(int i=1;i<=N;i++)
	{
		if((sum_sep+dollars[i])<=m)    //sum_sep表示当前一组的钱数和
			sum_sep+=dollars[i];
		else
		{
			sum_sep=dollars[i];    //若sum_sep+dollars[i])>钱数和m,则当前dollars[i]要新开一组
			group++;    
		}	
	}
	return group>=M;//如果group>=M,说明m<=ans,则L=mid;如果group<M,说明m>ans,则R=mid

} 

注意点

连续

上代码 

#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 1e5+10;

int N,M;
int dollars[MAXN];

int P(int m)
{
	int sum_sep=0;
	int group=1;
	for(int i=1;i<=N;i++)
	{
		if((sum_sep+dollars[i])<=m)
			sum_sep+=dollars[i];
		else
		{
			sum_sep=dollars[i];
			group++;
		}	
	}
	return group>=M;
} 

int main()
{
	int total=0;
	int max=0;
	scanf("%d%d",&N,&M);
	for(int i=1;i<=N;i++)
	{
		scanf("%d",&dollars[i]);
		total+=dollars[i];
		if(max<dollars[i])
			max=dollars[i]; 
	}

	int l=max-1;
	int r=total+1;
	int mid;
	while(l+1!=r)
	{
		mid=(l+r)/2;
		if(P(mid))
			l=mid;
		else
			r=mid;	
	}
	printf("%d",l);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值