POJ - 3273 :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 ≤ 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.

将输入的n组数据分为k组,使得k组中每组的和最大值最小。

#include<iostream>
#include<algorithm>

using namespace std;

int a[100005];

int main()
{
	int M,N,n=0,s,x;
	cin>>M>>N;
	for(int i=0;i<M;i++)
	{
		cin>>a[i];
		n+=a[i];
	}
	int low=0,hign=n,mid;
	while(hign-low>1)
	{
		x=0;
		s=0;
		mid=(low+hign)/2;
		int i=0;
		while(i<M&&s<=N)
		{

			if(x+a[i]>mid)
			{
				x=0;
				s++;

			}
			else
			{
				x+=a[i];
				i++;
			}
		}
		if(s>=N)
			low=mid;
		else
			hign=mid;
	}
	cout<<hign<<endl;

	return 0;
}
时间: 2024-11-05 00:02:39

POJ - 3273 :Monthly Expense的相关文章

POJ 3273 :Monthly Expense【二分】

用力戳我直达virtual judge~ 题意:有N个farm,每个farm花销farm[i]元.要求分成M块,尽可能让每个块花销少,求M个块中的最大值.(最大化最小值) 做法:二分结果(左界),每次判断mid能形成几个块,如果cur > M,说明钱必须更大,让块变少(low = mid + 1):否则满足条件,继续寻找更小的值. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

二分搜索 POJ 3273 Monthly Expense

题目传送门 1 /* 2 题意:分成m个集合,使最大的集合值(求和)最小 3 二分搜索:二分集合大小,判断能否有m个集合. 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 1e5 + 10; 12 const int INF = 0x3f3f

[ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)

Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14158   Accepted: 5697 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 r

POJ 3273 Monthly Expense (二分枚举)

Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15822   Accepted: 6321 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 r

POJ 3273 Monthly Expense 二分答案

Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13281   Accepted: 5362 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 r

POJ 3273 Monthly Expense(二分)

Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17465   Accepted: 6961 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 r

Divide and conquer:Monthly Expense(POJ 3273)

Monthly Expense 题目大意:不废话,最小化最大值 还是直接套模板,不过这次要注意,是最小化最大值,而不是最大化最小值,判断的时候要注意 联动3258 1 #include <iostream> 2 #include <functional> 3 #include <algorithm> 4 5 using namespace std; 6 7 static int money_set[100010]; 8 9 void Search(const int,

poj 3273 Monthly Expence 简单二分

1 /** 2 大意: 有连续的n天,每一天有一定的花费,将其分成m份,每一份占一天或者连续的几天,求这m份中的最大值 3 思路: 二分其最大上限,看在此最大上线,能分成多少份,若大于m份,说明上限过小,需要扩大上限 4 若小于m份,则说明,下限过大,需要缩小上限. 5 **/ 6 #include <iostream> 7 8 using namespace std; 9 int c[100010]; // 记录,每天的花费 10 int main() 11 { 12 int n,m; 13

poj Monthly Expense(最大值最小化)

Monthly Expense 题目链接:Click Here~ 题目分析: 给出N个数,要求你合并连续的数,使其合并在满足不差过M个合并后的集合的时候,不超过M个集合的和的最大值最小. 思路分析: 1.二分集合的和的最小值. 2.C:判断是否满足集合不超过M个? #include <iostream> #include <algorithm> #include <cstdio> using namespace std; const int MAXN = 100000