Uva 714 - Copying Books

Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so calledscribers. The scriber had been given a book and after several months he finished its copy. One of the most famous scribers lived in the 15th century and his name was Xaverius Endricus Remius Ontius Xendrianus (Xerox). Anyway, the work was very annoying and boring. And the only way to speed it up was to hire more scribers.


Once upon a time, there was a theater ensemble that wanted to play famous Antique Tragedies. The scripts of these plays were divided into many books and actors needed more copies of them, of course. So they hired many scribers to make copies of these books. Imagine you have m books (numbered $1, 2, \dots, m$) that may have different number of pages ( $p_1, p_2, \dots, p_m$) and you want to make one copy of each of them. Your task is to divide these books among k scribes, $k \le m$. Each book can be assigned to a single scriber only, and every scriber must get a continuous sequence of books. That means, there exists an increasing succession of numbers $0 = b_0 <b_1 < b_2, \dots < b_{k-1} \le b_k = m$ such that i-th scriber gets a sequence of books with numbers between bi-1+1 and bi. The time needed to make a copy of all the books is determined by the scriber who was assigned the most work. Therefore, our goal is to minimize the maximum number of pages assigned to a single scriber. Your task is to find the optimal assignment.

Input 

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly two lines. At the first line, there are two integers m and k$1 \le k \le m \le 500$. At the second line, there are integers $p_1, p_2, \dots p_m$ separated by spaces. All these values are positive and less than 10000000.

Output 

For each case, print exactly one line. The line must contain the input succession $p_1, p_2, \dots p_m$ divided into exactly k parts such that the maximum sum of a single part should be as small as possible. Use the slash character (`/') to separate the parts. There must be exactly one space character between any two successive numbers and between the number and the slash.


If there is more than one solution, print the one that minimizes the work assigned to the first scriber, then to the second scriber etc. But each scriber must be assigned at least one book.

Sample Input 

2
9 3
100 200 300 400 500 600 700 800 900
5 4
100 100 100 100 100

Sample Output 

100 200 300 400 500 / 600 700 / 800 900
100 / 100 / 100 / 100 100
题意:将有n个数的序列分成K个区间,使得区间和的最大值,最小。且当有多种结果时,使靠后的区间之和大。如第二组样例,一定要注意控制啊!!!否则会WA的。
分析:最大值最小化问题。利用二分枚举区间和的最大值。静下心来分析,可知,区间之和定在序列最大值和总和之间,对此区域中的数进行二分枚举即可。
代码如下:
#include<stdio.h>
#include<string.h>
int n,k;
long long  a[600];
int b[600];
long long sum,m,max;

int judge(int m)
{
    int tem=0,t=0;
    for(int i=0;i<n;i++)
    {
        if(tem+a[i]>m)
        {
            tem=a[i];
            t++;
            if(t==k)
                return 0;
        }
        else
            tem+=a[i];
    }
    return 1;
}
int main()
{
    int t,i;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&k);
        memset(b,0,sizeof(b));
        max=0,sum=0;
        for(i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
            sum+=a[i];
            if(max<a[i])
                max=a[i];
        }
        while(max<sum)//二分枚举,求区间和最大的最小值;
        {
            m=(max+sum)/2;
            if(judge(m))
                sum=m;
            else
                max=m+1;
        }
        int s;
        int tmp = n;
        s = 0;int t=0;
        for(i=n-1;i>=0;i--)//倒着判断,使后面区间的值较大
        {
            s+=a[i];
            if(s>sum||i+1<k-t)//i+1〈k-t一定不能不写,否则会WA的
            {
                b[i]=1;
                s=a[i];
                t++;
            }
        }
        printf("%d",a[0]);
        if(b[0]==1)
            printf(" /");
        for(i=1;i<n;i++)
        {
            printf(" %d",a[i]);
            if(b[i]==1)
                printf(" /");
        }
        printf("\n");
    }
    return 0;
}
有大神用区间dp,不管比会不会,反正我是不会;
献上代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int dp[505][505],sum[505],a[505];

int main()
{
    int t,n,m,i,j,x,v,cas;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d%d",&n,&m);
        memset(sum,0,sizeof(sum));
        memset(dp,-1,sizeof(dp));
        for(i = 1; i<=n; i++)
        {
            scanf("%d",&x);
            sum[i] = sum[i-1]+x;
        }
        dp[0][0] = 0;
        for(i = 1; i<=n; i++)
        {
            for(j = 1; j<=i && j<=m; j++)
            {
                if(j == 1)
                    dp[i][j] = sum[i];
                else
                {
                    for(v = j-1; v<=i-1; v++)//
                    {
                        int t = max(dp[v][j-1],sum[i]-sum[v]);
                        if(dp[i][j] == -1 || t<dp[i][j])
                            dp[i][j] = t;
                    }
                }
            }
        }
        j = m-1;
        x = 0;
        for(i = n; i>=1; i--)
        {
            x+=sum[i]-sum[i-1];
            if(x>dp[n][m] || i<=j)
            {
                a[j--] = i+1;
                x = sum[i]-sum[i-1];
            }
        }
        int cnt = 1;
        for(i = 1; i<=n; i++)
        {
            if(i>1)
                printf(" ");
            if(cnt<m && a[cnt]==i)
            {
                printf("/ ");
                cnt++;
            }
            printf("%d",sum[i]-sum[i-1]);
        }
        printf("\n");
    }

    return 0;
}




                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值