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 recorded the exact amount of money (1 ≤moneyi ≤ 10,000) that he will need to spend each day over the nextN (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
Lines 2.. N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Sample Input
7 5100400300100500101400
Sample Output
500
Hint
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int a[100005];int main(){ int n,m; int sum,maxn,i,j,s,cnt,mid; while(~scanf("%d%d",&n,&m)) { sum = 0,maxn = 0; for(i = 0; i<n; i++) { scanf("%d",&a[i]); sum+=a[i]; maxn = max(maxn,a[i]); } while(maxn<sum) { mid = (sum+maxn)>>1; s = 0,cnt = 0; for(i = 0; i<n; i++) { s+=a[i]; if(s>mid) { s = a[i]; cnt++; } } if(cnt<m) sum = mid; else maxn = mid+1; } printf("%d\n",maxn); } return 0;}
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow
本文探讨了如何通过二分搜索算法解决预算划分问题,旨在最小化最大预算支出,适用于农场主约翰的资金管理场景。文章提供了详细的算法实现,并附有样例输入输出,帮助读者理解并实践这一优化策略。
376

被折叠的 条评论
为什么被折叠?



