UVa 714

D&C problem, do a binary search on range [0, sum of pages]
make a guess on the maximum pages assigned to one scriber each time
as for calculating the real maximum pages, j<k-1 ensures there are enough
scribers left, and if r+b[i]>mid, we should split here, or if i<k-j-1, means books
left are now enough, so have to assigned 1 book to 1 scriber for left books
/* UVas */
/*****************************************************************************/
int n, m, k;
int v[500];
long long b[500];
long long bin(long long mid) {
    long long r=0, MAX=0;
    for (int i=m-1, j=0; i>=0; i--) {
        if (j<k-1&&(i<k-j-1||r+b[i]>mid)) {
            MAX=max(MAX, r);
            r=b[i];
            v[i]=1;
            j++;
        }
        else {
            r+=b[i];
        }
    }
    return max(MAX, r);
}
int main(void) { 
    cin>>n;
    while (n--) {
        cin>>m>>k;
        long long l=0, h=0, mid, r;
        for (int i=0; i<m; i++) {
            cin>>b[i];
            h+=b[i];
        }
        while (l<h) {
            mid=(l+h)>>1;
            r=bin(mid);
            r>mid?l=mid+1:h=mid;
        }
        memset(v, 0, sizeof(v));
        bin(l);
        for (int i=0; i<m; i++) {
            cout<<(i==0?"":" ")<<b[i]<<(v[i]?" /":"")<<(i==m-1?"\n":"");
        }
    }
    return 0;
}
/*****************************************************************************/

No comments :

Post a Comment