uva 714 Copying Books

题意:小白上的P151页的最大值最小化

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
    int a[505],b[505];
    int k,m,num;
    scanf("%d",&num);
    while (num--)
    {
        long long left = 0,right = 0,ct,to,mid;
        memset(b,0,sizeof(b));
        scanf("%d%d",&m,&k);
        for (int i = 0; i < m; i++)
        {
            scanf("%d",&a[i]);
            right += a[i];
        }
        while (left < right)
        {
            ct = 0,to = 0;
            mid = (left + right) / 2;
            for (int i = 0; i < m; i++)
            {
                to += a[i];
                if (a[i] > mid)
                {
                    ct = k;
                    break;
                }
                if (to >= mid)
                {
                    if (ct >= k)
                        break;
                    ct++;
                    to = a[i];
                }
            }
            if (ct < k)    //如果达不到k部分,就太大了
                right = mid;
            else left = mid + 1;
        }
        for (int cct = 0,tot = 0,i = m - 1; i >= 0; i--)
        {
            tot += a[i];
            if (cct < k - 1 && (tot > mid || i < (k - 1) - cct))
            {
                b[i] = 1;
                tot = a[i];
                cct++;
            }
        }

        for (int i = 0; i < m; i++)
        {
            printf("%d%c",a[i],i!=m-1?' ':'\n');
            if (b[i])
                printf("/ ");
        }
    }
    return 0;
}




你可能感兴趣的:(uva 714 Copying Books)