欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Monthly Expense【二分】

发布时间:2025/3/8 编程问答 39 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Monthly Expense【二分】 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

B - Monthly Expense

 POJ - 3273 

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.

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.

AC代码

 

#include <cstdio> #include <iostream> using namespace std; int N,M; const int inf=10000000; int money[105000]; bool judge(int mid) {int sum=0,num=0;for(int i=0;i<N;i++){if(sum+money[i]>mid){sum=money[i];num++;continue;}sum+=money[i];}return num<M; } int main() {while(cin>>N>>M){int max1=0;for(int i=0;i<N;i++){cin>>money[i];if(money[i]>max1)max1=money[i];}int left=max1,right=inf;int mid=0;for(int i=0;i<100;i++){mid=(right+left)/2;if(judge(mid))right=mid;elseleft=mid;}cout<<right<<endl;}return 0; }

 

总结

以上是生活随笔为你收集整理的Monthly Expense【二分】的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。