洛谷—— 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){;}

 

转载于:https://www.cnblogs.com/Shy-key/p/7899895.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值