POJ 3273 Monthly Expense 最大值最小化问题

Monthly Expense
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 33426   Accepted: 12527

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

/**
题意:将n个数进行划分为m个区域  使得区域和最小;
经典最大值最小化问题 n*lgn 
二分答案x  看答案是否满足条件;
*/

#include<cstdio>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;

const int maxn=1e5+7;
int a[maxn],n,m;

bool judge(ll x){
	ll sum=0;
	int num=0;
	for(int i=1;i<=n;i++){
		if(sum+a[i]<=x) sum+=a[i];
		else  sum=a[i],num++;
	}
	return num>=m;
}

int main (){
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	ll l=0,r=0;
	for(int i=1;i<=n;i++){
		l=max(l,a[i]*1ll);
		r+=a[i];
	}
	while(l<r){
		ll mid=(l+r)/2;
		if(judge(mid)) l=mid+1;
		else r=mid;
	}
	printf("%lld\n",l);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hypHuangYanPing/article/details/80962892