zoukankan      html  css  js  c++  java
  • 高效算法——B 抄书 copying books,uva714

    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
    Submit Status

    Description

    Download as PDF
     

    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个数,将这N个数分成连续的M段,使得这M段每段的和中的最大值最小,输出最小值(1<=N<=100000,1<=M<=N,每个数在1到10000之间),如果有多种可能的话,尽量在前面进行划分。

    思路:

    1、由于函数具有单调性的特征,因此可以用二分枚举的办法去实现它,但这里不需要排序。

    2、输出的时候需要用到贪心的思想,既尽量往前划分。

    3、大概的思路就是二分枚举求得满足题意的最大值之后,然后以这个最大值通过从后往前的方式划分成段,如果剩余可划分段与i+1的值相等(尽量靠前),则将剩余的段往前划分,具体实现可以用一个标记数组表示是否划分。

    5、注意要用long long 来存。

    解题思路借鉴了大神的,感觉这个思路比较清晰、易懂,希望能够帮助到博友们

    程序代码:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxm = 500 + 5;
    int m, k, p[maxm];
    int solve(long long maxp)
    {
      long long done = 0;
      int ans = 1;
      for(int i = 0; i < m; i++)
        {
        if(done + p[i] <= maxp) done += p[i];
        else { ans++; done = p[i]; }
        }
      return ans;
    }
    int last[maxm];
    void print(long long ans)
    {
      long long done = 0;
      memset(last, 0, sizeof(last));
      int remain = k;
        for(int i = m-1; i >= 0; i--)
        {
        if(done + p[i] > ans || i+1 < remain)
           {    last[i] = 1; remain--; done = p[i];    }
        else
          done += p[i];
        }
        for(int i = 0; i < m-1; i++)
        {
        printf("%d ", p[i]);
        if(last[i]) printf("/ ");
        }
        printf("%d
    ", p[m-1]);
    }
    int main()
    {
      int T;
      scanf("%d", &T);
      while(T--)
      {
        scanf("%d%d", &m, &k);
        long long tot = 0;
        int maxp = -1;
        for(int i = 0; i < m; i++)
        {
          scanf("%d", &p[i]);
          tot += p[i];
          maxp = max(maxp, p[i]);
        }
        long long L = maxp, R = tot;
        while(L < R)
        {
          long long M = L + (R-L)/2;
          if(solve(M) <= k) R = M; else L = M+1;
        }
        print(L);
      }
      return 0;
    }
    View Code
    版权声明:此代码归属博主, 请务侵权!
  • 相关阅读:
    vs中如何统计整个项目的代码行数
    EasyDarwin手机直播转发快速显示问题之音频处理过程
    EasyDarwin手机直播转发快速显示问题之音频处理过程
    RTSPClient工具EasyRTSPClient支持H.265,支持海思等各种芯片平台
    RTSPClient工具EasyRTSPClient支持H.265,支持海思等各种芯片平台
    EasyDarwin手机直播是如何实现的快速显示视频的方法
    EasyDarwin手机直播是如何实现的快速显示视频的方法
    EasyPusher RTSP直播之RTP数据包格式解析
    EasyPusher RTSP直播之RTP数据包格式解析
    EasyPusher安卓Android手机直播推送之RTSP流媒体协议流程
  • 原文地址:https://www.cnblogs.com/www-cnxcy-com/p/4702312.html
Copyright © 2011-2022 走看看