Ultraviolet A 714-copying books (Greedy maximum minimization of two points)

Source: Internet
Author: User

I have read the story carefully for a long time .. In fact, the last two sentences are useful. This means that N books are given and then divided into k parts. the maximum number of pages for each book is the minimum. Ask you about the allocation scheme. Suppose there are multiple allocation schemes under the same minimum value, and the number of output parts is small, just as the meaning of the lexicographic output from small to large.

The greedy method is used here. The condition that f (x) is true is that X is the maximum value to divide n books into k portions. Then, the minimum value of X is obtained. How can we determine that X is a binary method, where X must be greater than 0 and smaller than the sum of all values, and then we can infer whether X is true. If it is true, we can take the left half edge, if it is not true, the right half side is taken as a small value. During the writing, the binary method is not fully understood. I am afraid that the value will be lost and I will save it specially, in fact, the X or Y (two numbers are equal) obtained from the end of the Bipartite method is the maximum value of each part. And how to determine whether the maximum value is true is to use the greedy method, as far as possible to expand to the right until the previous one is greater than the maximum value. Assuming that the number of copies is the last one before the split, it must be true. On the contrary, it is not true if the number of parts has not reached the last one.

When outputting data, you must note that you have to move from the back to the back. Because the number of copies in the front is small, you have to move from the back to get greedy. When the remaining parts are the same as the number of copies, you cannot get greedy, every one must be separated. Let's take a look at the simulation data. The plus and minus one have to be related to the previous writing.

Note that the sum will exceed the int range because a maximum of 1x10 ^ 8 and a maximum of 500 are exceeded by 4x10 ^ 9. Therefore, use long. Another point is that at the time of output, the last number cannot be followed by one more space. Otherwise, PE will be reported.

AC code:

#include<cstdio>#include<ctype.h>#include<algorithm>#include<iostream>#include<cstring>#include<vector>#include<stack>#include<cmath>#include<queue>#include<set>#include<ctime>using namespace std;#define NMAX 505#define ll long longint a[NMAX];int ans[NMAX];int solve(ll Max,int n,int k){    int i=0,j=0,nct=0;    ll sum=0;    while(nct < k)    {        sum+=a[j];        if(sum > Max && i == j) return 0;        if(sum > Max)        {            nct++;            i = j;            sum = 0;        }        else j++;        if(j == n) return 1;    }    return 0;}void path(ll Max,int n,int k){    int nct = 0,i=n-1;    ll sum=0;    while(i>0)    {        sum+=a[i];        if(sum > Max)        {            sum = 0;            ans[i] = 1;            nct++;        }        else i--;        if(i == k-nct-2)        {            for(int j = 0; j <= i; j++)                ans[j] = 1;            break;        }    }    for(int i = 0; i < n; i++)    {        if(i == n-1) printf("%d",a[i]);        else printf("%d ",a[i]);        if(ans[i]) printf("/ ");    }    printf("\n");}int main(){    int i,n,k,m;    scanf("%d",&n);    while(n--)    {        memset(ans,0,sizeof(ans));        scanf("%d%d",&m,&k);        ll sum = 0;        for(i = 0; i < m; i++)        {            scanf("%d",&a[i]);            sum += a[i];        }        ll x=0,y=sum,z;        while(x<y)        {            z = x+(y-x)/2;            if(solve(z,m,k))                y = z;            else x = z+1;        }        path(x,m,k);    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.