poj Monthly Expense
http://poj.org/problem?id=3273
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 #define maxn 100010 6 using namespace std; 7 8 int n,s; 9 int a[maxn]; 10 11 int main() 12 { 13 while(scanf("%d%d",&n,&s)!=EOF){ 14 int high=0,low=0; 15 for(int i=0; i<n; i++) 16 { 17 scanf("%d",&a[i]); 18 high+=a[i]; 19 low=max(low,a[i]); 20 } 21 int mid; 22 while(low<=high) 23 { 24 mid=(high+low)/2; 25 int ans=0,sum=0; 26 for(int i=0; i<n; i++) 27 { 28 sum+=a[i]; 29 if(sum>mid) 30 { 31 ans++; 32 sum=a[i]; 33 } 34 } 35 if(sum!=0) ans++; 36 if(ans>s) 37 { 38 low=mid+1; 39 } 40 else high=mid-1; 41 } 42 printf("%d\n",mid); 43 } 44 return 0; 45 }