POJ 3273 Monthly Expense

解题思路:

开始下限去最大值,上限去开始的连续N-M+1数之和

二分逼近

代码
   
     
#include < iostream >
using namespace std;
int main()
{
int i, n, m, p, q, mid, t,s, d[ 100000 ];
scanf(
" %d %d " , & n, & m);
for (q = p = i = 0 ;i < n;i ++ )
{
scanf(
" %d " ,d + i);
if (i <= n - m)q += d[i];
if (p < d[i])p = d[i];
}
while (p < q)
{
mid
= (p + q) / 2 ;
for (s = i = 0 ,t = 1 ;i < n;i ++ )
if (s + d[i] > mid){t ++ ,s = d[i];}
else s += d[i];
if (t <= m)q = mid;
else p = mid + 1 ;
}
printf(
" %d\n " , p);
return 0 ;
}

 

你可能感兴趣的:(exp)