码迷,mamicode.com
首页 > 其他好文 > 详细

Monthly Expense(二分--最小化最大值)

时间:2018-08-05 20:20:57      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:live   read   class   call   typedef   nim   eterm   预算   already   

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.
题意:农场主john为了节省开支自己做了一个预算。他已经计算好了接下来几天每天要花的钱,他将这些天分为m个时间段,要求分得各组的花费之和应该尽可能地小,最后输出各组花费之和中的最大值。
分析:这是一个最小化最大值的问题,二分枚举即可。
 1 #include <iostream>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<cstdio>
 5 #include<algorithm>
 6 using namespace std;
 7 typedef long long ll;
 8 const int MAXN=1e5+10;
 9 const int Inf=0x3f3f3f3f;
10 int m,n;
11 int str[MAXN];
12 int solve(int mid)
13 {
14     int sum=0,cnt=1;
15     for(int i=0;i<m;i++)
16     {
17         if(sum+str[i]<=mid)
18         {
19             sum+=str[i];
20         }
21         else
22         {
23             sum=str[i];
24             cnt++;
25         }
26     }
27     if(cnt>n)
28         return 0;
29     return 1;
30 }
31 int main()
32 {
33     while(scanf("%d%d",&m,&n)!=-1)
34     { int left=0,right=0,mid;
35     for(int i=0;i<m;i++)
36     {
37         scanf("%d",&str[i]);
38         right+=str[i];
39         left=max(left,str[i]);
40     }
41     while(left<=right)
42     {
43         mid=(right+left)/2;
44         if(solve(mid))
45         {
46             right=mid-1;
47         }
48         else
49         {
50             left=mid+1;
51         }
52     }
53     printf("%d\n",mid);}
54     return 0;
55 }

 

Monthly Expense(二分--最小化最大值)

标签:live   read   class   call   typedef   nim   eterm   预算   already   

原文地址:https://www.cnblogs.com/moomcake/p/9426856.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!