洛谷—— P2884 [USACO07MAR]每月的费用Monthly Expense

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了洛谷—— P2884 [USACO07MAR]每月的费用Monthly Expense相关的知识,希望对你有一定的参考价值。

https://www.luogu.org/problemnew/show/P2884

题目描述

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.

给出农夫在n天中每天的花费,要求把这n天分作m组,每组的天数必然是连续的,要求分得各组的花费之和应该尽可能地小,最后输出各组花费之和中的最大值

输入输出格式

输入格式:

 

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.

 

输入输出样例

输入样例#1: 复制
7 5
100
400
300
100
500
101
400
输出样例#1: 复制
500

说明

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.

 

二分一个最大连续钱数,每次增加一个月份的钱,判断一次。注意统计最后的一组

 1 #include <cstdio>
 2 
 3 inline void read(int &x)
 4 {
 5     x=0; register char ch=getchar();
 6     for(; ch>9||ch<0; ) ch=getchar();
 7     for(; ch>=0&&ch<=9; ch=getchar()) x=x*10+ch-0;
 8 }
 9 
10 const int N(1e5+5);
11 
12 int n,m,w[N];
13 
14 int L,R,Mid,ans;
15 inline bool check(int x)
16 {
17     int cnt=0,now=w[1];
18     for(int i=2; i<=n; ++i)
19     {
20         if(now>x) return 0;
21         if(now+w[i]>x)
22         {
23             cnt++;
24             now=w[i];
25         }
26         else now+=w[i];
27     }
28     if(now&&now<=x) cnt++;
29     return cnt<=m;
30 }
31 
32 int Presist()
33 {
34     read(n),read(m);
35     for(int i=1; i<=n; ++i)
36         read(w[i]),R+=w[i];
37     for(; L<=R; )
38     {
39         Mid=L+R>>1;
40         if(check(Mid))
41         {
42             ans=Mid;
43             R=Mid-1;
44         }
45         else L=Mid+1;
46     }
47     printf("%d\n",ans);
48     return 0;
49 }
50 
51 int Aptal=Presist();
52 int main(int argc,char**argv){;}

 

以上是关于洛谷—— P2884 [USACO07MAR]每月的费用Monthly Expense的主要内容,如果未能解决你的问题,请参考以下文章

[USACO07MAR]黄金阵容均衡Gold Balanced L…(洛谷 1360)

洛谷P2882 [USACO07MAR]面对正确的方式Face The Right Way

洛谷 P2904 [USACO08MAR]跨河River Crossing

洛谷 P2945 [USACO09MAR]沙堡Sand Castle

洛谷P2947 [USACO09MAR]仰望Look Up

洛谷2115 [USACO14MAR]破坏Sabotage