zoukankan      html  css  js  c++  java
  • 抄书 Copying Books UVa 714

    Copying  Books

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/B

    题目:

    Description

    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

    题意:
    把一个包含m个正整数的序列划分成k个非空的连续子序列,使得这k个子序列中和的最大值最小。
    如果有多解和最大的尽量在后面。(序列中的元素位置不能发生改变)

    分析:
    用二分法求出最小值,
    把0设为l,sum设为r,mid=(l+r)/2;
    如果以mid为最小值可以划分成k个或者小于k个子序列,说明最小值比mid小或者等于mid,相反最小值比mid大。
    注意值的数据类型

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 const int maxn=510;
     6 long long a[maxn],b[maxn],sum;
     7 int m,k;
     8 int bj( long long x) 
     9 {  
    10     long long sum = 0;
    11     int t = k;  
    12     for(int i = 0; i < m; i ++)
    13     {  
    14         sum += a[i];  
    15         if(sum > x)
    16         {  
    17             i --;  
    18             sum = 0;  
    19             t --;  
    20         }  
    21         if(!t) 
    22         {  
    23             if(i != m - 1) return 0;  
    24                else return 1;  
    25         }  
    26     }  
    27     return 1;  
    28 }  
    29 void slove()
    30 {
    31     int i;
    32     memset(b,0,sizeof(b));
    33    long long l=0,r=sum,mid;
    34    while(r>l)
    35    {
    36        mid=(l+r)/2;
    37      if(bj(mid))   r=mid;
    38       else  l=mid+1;
    39    }
    40    int sum1=0;
    41      for( i=m-1;i>=0;i--)              //标记需要划分的地方,由于和大的尽量往后所以从序列后面开始划分
    42      {
    43       sum1+=a[i];
    44           if(sum1>r)
    45           {   i++;
    46                sum1=0;
    47                b[i]=1;
    48                  k--;
    49                  
    50           }
    51      }
    52 
    53     while(k >1)                 //如果子序列小于k就在还没划分的地方划分直到等于k
    54     {  
    55         for(i = 1;i < m; i ++ ) 
    56         {  
    57             if(!b[i]) 
    58             {  
    59                 b[i] = 1;  
    60                 k --;  
    61                 break;  
    62             } 
    63         }
    64     }
    65     return;
    66 }
    67  int main()
    68  {
    69   int n,i;
    70   cin>>n;
    71   while(n--)
    72   {
    73      sum=0;
    74      cin>>m>>k;
    75    for(i=0;i<m;i++)
    76    {
    77     cin>>a[i];
    78    sum+=a[i];
    79    }
    80    slove();
    81    printf("%lld",a[0]);  
    82     for( i = 1; i < m; i ++) 
    83     {  
    84         if(b[i]) printf(" /");  
    85         printf(" %lld", a[i]);  
    86     }  
    87     printf("
    ");  
    88   }
    89 return 0;
    90 }





     

     

  • 相关阅读:
    Windows 2003/2008更改远程桌面端口脚本
    如何修改远程桌面连接3389端口
    关于百度地图(离线)使用过程报“Cannot read property 'jb' of undefined ”错误的解决办法
    IIS 错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
    IIS 错误:由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。
    IIS7错误:不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的(overrideModeDefault="Deny")......
    Jqgrid pager 关于“local” dataType 动态加载数据分页的研究(没好用的研究结果)
    JQGrid导出Excel文件
    Oracle以15分钟为界,统计一天内各时间段的数据笔数
    ORA-01438: 值大于为此列指定的允许精度
  • 原文地址:https://www.cnblogs.com/fenhong/p/4702564.html
Copyright © 2011-2022 走看看