首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【POJ】3273 - Monthly Expense(二分)

【POJ】3273 - Monthly Expense(二分)

作者头像
FishWang
发布2025-08-26 19:49:20
发布2025-08-26 19:49:20
8700
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

Monthly Expense

Time Limit: 2000MS

Memory Limit: 65536K

Total Submissions: 21764

Accepted: 8553

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

代码语言:javascript
代码运行次数:0
运行
复制
7 5
100
400
300
100
500
101
400

Sample Output

代码语言:javascript
代码运行次数:0
运行
复制
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

USACO 2007 March Silver

在最小的支出和总支出中间二分即可。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <algorithm>
using namespace std;
int sum[100000+11];
int n,m;
bool check (int x)
{
	if ((double)sum[n-1] / m > x)		//特判一下 
		return false;
	int pos;
	int t;
	pos = upper_bound (sum , sum + n , x) - 1 - sum;
	for (int i = 1 ; i < m ; i++)
	{
		t = upper_bound (sum , sum + n , sum[pos] + x) - 1 - sum;
		if (t == n - 1)
			return true;
		if (t == pos)
			return false;
		pos = t;
	}
	return false;
}
int main()
{
	int l,r,mid;
	while (~scanf ("%d %d",&n,&m))
	{
		scanf ("%d",&sum[0]);
		l = sum[0];
		for (int i = 1 ; i < n ; i++)
		{
			scanf ("%d",&sum[i]);
			sum[i] += sum[i-1];
			l = min (l , sum[i]);
		}
		r = sum[n-1];
		while (r >= l)
		{
			mid = (l + r) >> 1;
			if (check(mid))
				r = mid - 1;
			else
				l = mid + 1;
		}
		printf ("%d\n",l);
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-07-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档