bzoj1639[usaco2007 mar]monthly Expense Monthly expenditure
Test instructions
N days, there is a cost per day, and now it is necessary to divide them into successive m segments, requiring the total cost of all segments to be minimized. To find this value. n,m≤100000
Exercises
Two cents spent, less than two points of the day as a paragraph. Note that the lower bound of the dichotomy should be the maximum value spent per day.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineMAXN 1001005 #defineInc (I,J,K) for (int i=j;i<=k;i++)6 using namespacestd;7 8InlineintRead () {9 CharCh=getchar ();intf=1, x=0;Ten while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();} One while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); A returnf*x; - } - intN,m,mon[maxn],l,r,ans; the BOOLCheckintx) { - inty=1, z=0; -Inc (I,1, N) { - if(z+mon[i]>x) {y++; z=mon[i];if(y>m)return 0;}Elsez+=Mon[i]; + } - return 1; + } A intMain () { atN=read (); M=read (); Inc (I,1, n) mon[i]=read (), L=max (L,mon[i]), r+=Mon[i]; - while(l<=R) { - intMid= (l+r) >>1;if(Check (mid)) ans=mid,r=mid-1;ElseL=mid+1; - } -printf"%d", ans);return 0; -}
20160731
bzoj1639[usaco2007 mar]monthly Expense Monthly Expenses *