poj3273Monthly Expense 最大值最小化 使最大值尽量小

2017-03-30 88点热度 0人点赞
Monthly Expense
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 25290   Accepted: 9788

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.

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

//最大值最小化,就是给n个数,把它分成m个区间和,求区间和的最小值
//先找最小的值,然后二分找,如果可能那么答案就是小于等于这个值,如果不可能那就是另外一个区间 
//时间复杂度是O(nlogM) ,M为所有数之和
//就是从小到大的去推,用二分优化 
#include
#include
using namespace std;
int n,m,a[100000],s=0;
int midfind(int mid){
	//如果少于等于m份,则答案少于等于mid,反之大于mid
	int t=0,num=1;
	for(int i=0;imid){
			return 1;//答案应该大于mid
		}
		if(t+a[i]<=mid) t+=a[i];
		else{
			t=a[i];
			num++;
			if(num>m) return 1;//答案应该大于mid
		}
	}
	return 0;//答案应该小于等于mid 
}
int main(){
	int Mi=0x3f3f3f3f;
	scanf("%d%d",&n,&m);
	for(int i=0;i

未经允许不得转载!poj3273Monthly Expense 最大值最小化 使最大值尽量小

本文地址:https://ai.52learn.online/640

站长邮箱:ai52learn@foxmail.com