Monthly expense (two points)

And yet another is half of it
daft peaches here again

Topic links: Monthly Expense

题目描述:
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.

输入:
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

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

There is the cost of n days, which should be divided into m months, seeking the minimum cost maximum.

This question and river hopscotch that question a lot like it
in the river hopscotch Problem Solution

note

1.low should spend the maximum daily, high equal to the total cost.
2. Total spend days with a statistical variable, when the cost is greater than mid, num ++, if num <time = m, whichever is greater mid described, this time to change the high value (here, we must pay attention!num little time was mid big! ! ! Draw focus!

Code

#include<stdio.h>
int main()
{
	int n,m,i,low=0,high=0,mid,a[100010];
	scanf("%d %d",&n,&m);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		if(low<a[i]) low=a[i];
		high+=a[i]; 
	}
	while(low<high){
		mid=(low+high)/2;
		int num=1,sum=0;
		for(i=0;i<n;i++)
		{	
			if(sum+a[i]<=mid) sum+=a[i];
			else{
				sum=a[i];
				num++;
			}
		}
		if(num<=m) high=mid-1;
		else low=mid+1;
	}
	printf("%d\n",high);
	return 0;
}

Published 22 original articles · won praise 1 · views 1054

Guess you like

Origin blog.csdn.net/Doro_/article/details/104066829