简单二分,下限为每天花费的最大值,上限是所有天数花费的总和.
#include<iostream>
#include<cstdio>
using namespace std ;
int mon[100005] ;
int i, j, n, m ;
int jud(int mid){
int s=0, count=1 ;
for (i=0; i<n; i++){
s += mon[i] ;
if(s>mid){
s = mon[i] ;
count ++ ;
}
}
if(count>m) return true ;
else return false ;
}
int main(){
int ans ;
int max, sum ;
while(scanf("%d%d", &n, &m)!=EOF){
max = -1 ;
sum = 0 ;
for(i=0; i<n; i++){
scanf("%d", &mon[i]) ;
if(mon[i]>max) max = mon[i] ;
sum += mon[i] ;
}
int l = max, r = sum, mid ;
while(r>l){
mid = (l+r) / 2 ;
if(jud(mid))
l = mid + 1 ;
else
r = mid - 1 ;
}
cout << l << endl ;
}
#include<cstdio>
using namespace std ;
int mon[100005] ;
int i, j, n, m ;
int jud(int mid){
int s=0, count=1 ;
for (i=0; i<n; i++){
s += mon[i] ;
if(s>mid){
s = mon[i] ;
count ++ ;
}
}
if(count>m) return true ;
else return false ;
}
int main(){
int ans ;
int max, sum ;
while(scanf("%d%d", &n, &m)!=EOF){
max = -1 ;
sum = 0 ;
for(i=0; i<n; i++){
scanf("%d", &mon[i]) ;
if(mon[i]>max) max = mon[i] ;
sum += mon[i] ;
}
int l = max, r = sum, mid ;
while(r>l){
mid = (l+r) / 2 ;
if(jud(mid))
l = mid + 1 ;
else
r = mid - 1 ;
}
cout << l << endl ;
}
}