• UVA1471-Copying Books(二分答案)


    Problem UVA1471-Copying Books

    Accept: 2669  Submit: 22797
    Time Limit: 3000 mSec

    Problem 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 called scribers. 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. Onceuponatime,therewasatheaterensemblethatwantedtoplayfamousAntiqueTragedies. 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,...,m) that may have different number of pages (p1,p2,...,pm) and you want to make one copy of each of them. Your task is to divide these books among k scribes, k ≤ 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 = b0 < b1 < b2,... < bk−1 ≤ bk = 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 ≤ k ≤ m ≤ 500. At the second line, there are integers p1,p2,...,pm 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 p1,p2,...pm 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. Ifthereismorethanonesolution,printtheonethatminimizestheworkassignedtothefirstscriber, 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

    题解:最大化最小值,目前遇到的题不是贪心就是二分,这个题明显二分答案。输出格式需要注意一下,因为要字典序最小所以尽量让后面的数凑在一起,这样前面的数就能尽量分散。如果当前剩余分块数等于剩余数字个数,直接break输出即可。

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 typedef long long LL;
     5 
     6 const int maxn = 500 + 10;
     7 const LL INF = 1e15;
     8 
     9 int n, m;
    10 LL num[maxn];
    11 
    12 int Check(LL x) {
    13     LL tmp = 0;
    14     int cnt = 1;
    15     for (int i = 0; i < n; i++) {
    16         if (tmp + num[i] <= x) tmp += num[i];
    17         else tmp = num[i], cnt++;
    18     }
    19 
    20     return cnt;
    21 }
    22 
    23 bool should_put[maxn];
    24 
    25 void output(LL x) {
    26     memset(should_put, false, sizeof(should_put));
    27     LL tmp = 0;
    28     int i, cnt = 1;
    29     for (i = n - 1; i >= 0; i--) {
    30         if (tmp + num[i] <= x) tmp += num[i];
    31         else {
    32             should_put[i] = true;
    33             tmp = num[i];
    34             cnt++;
    35         }
    36         if (i == m - cnt) break;
    37     }
    38 
    39     if (i != -1) {
    40         for (int j = 0; j < i; j++) {
    41             should_put[j] = true;
    42         }
    43     }
    44     for (int i = 0; i < n - 1; i++) {
    45         printf("%lld ", num[i]);
    46         if (should_put[i]) printf("/ ");
    47     }
    48     printf("%lld
    ", num[n - 1]);
    49 }
    50 
    51 int main()
    52 {
    53     //freopen("input.txt", "r", stdin);
    54     //freopen("output.txt", "w", stdout);
    55     int iCase;
    56     scanf("%d", &iCase);
    57     while (iCase--) {
    58         scanf("%d%d", &n, &m);
    59         LL sum = 0, Max = -INF;
    60         for (int i = 0; i < n; i++) {
    61             scanf("%lld", &num[i]);
    62             sum += num[i];
    63             Max = Max > num[i] ? Max : num[i];
    64         }
    65         LL ans = 0;
    66         LL l = Max, r = sum;
    67         while (l <= r) {
    68             LL mid = (l + r) / 2;
    69             if (Check(mid) <= m) {
    70                 ans = mid;
    71                 r = mid - 1;
    72             }
    73             else l = mid + 1;
    74         }
    75 
    76         //printf("%lld
    ", l);
    77 
    78         output(l);
    79     }
    80     return 0;
    81 }
  • 相关阅读:
    asp.net 发送邮件
    效控制C#中label输出文字的长度,自动换行
    无法连接到WMI 提供程序 请注意,你只能使用SQL Server 配置管理器来管理SQL Server 2005服务器。找不到指定的模块。[0x8007007e]
    查询区分大小写
    ASP.NET母版页引用外部css和js文件的写法
    VS2008 Debugging Breakpoint 补丁
    firefox下获得焦点
    IE打开出现windows找不到文件'(null)'解决方法Vinzipblog文之巴博客
    邪恶的web上下键焦点移动
    jQuery对下拉框Select操作总结
  • 原文地址:https://www.cnblogs.com/npugen/p/9658756.html
Copyright © 2020-2023  润新知