抄书(UVa714)

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 ) that may have different number of pages ( ) and you want to make one copy of each of them. Your task is to divide these books among k scribes, . 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  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. At the second line, there are integers  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  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本书,编号为1,2,3...N, 每本书有1<=x<=10000000页, 把这些书分配给K个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的。每个抄写员的速度是相同的,求所有书抄完所用的最少时间的分配方案。如果有多种分配方案,尽可能的往前面划分

解题思路:

1.要求最少时间,取决于抄书最多的人。所以只有抄书最多的人,抄的尽可能少,时间才最少。所以这题就变成了使最大值尽量小的问题了。

下面考虑下一个问题,哪个值是最大序列的最小值呢?我们不妨考虑下最大序列的范围,不难想到它是   最小页数min——整个序列之和sum 。我们要从中找到他的最大序列的最小值。因为每本书有有1<=x<=10000000页,所以sum绝逼会大于int,查找这个范围也是巨大的。为了不超时,所以就用二分法.......(其实为什么用2分法,我也不知道,自己想的一个牵强的理由。反正书上是说用二分法....)

2. 然后就是用二分法缩小范围直到找到那个最大序列的最小值,这里用x表示.....   肿么找x呢!你可以用二分法,先求x范围的中点,然后从序列a[0]开始求和,如果加到第i个数

发现它大于中点,说明x在后半段,变左端点,反之,说明在在前半段,变右端点。最后找到x。

3. 最后就简单了,只需要标记,然后输出就好了....

这里给两个代码,第一个是答案代码,第二个是我测试加的一些输出.....  也许对理解有些帮助........

1.正确代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define LL long long
 4 int min=0,m,k,a[505],ans[505];
 5 LL sum=0;
 6 bool juge(LL x)
 7 {
 8     LL sum2=0;
 9     int t=k;
10     for(int i=0;i<m;i++){
11         sum2+=a[i];
12         if(sum2>x){
13             t--;    //记录划得次数
14             i--;
15             sum2=0;
16         }
17         if(!t){    // 如果划完了
18             if(i!=m-1) return false;  //划完了,但是没有划到最后一个,说明最大序列的最小值比现在的x大.....
19             else return true;
20         }
21     }
22     return true;
23 }
24
25 void zhaox()
26 {
27     memset(ans,0,sizeof(ans));
28     LL l=min,r=sum,mid;
29     while(l<r){       //当左端点等于右端点就确定了一个数x
30         mid=(r+l)/2;    //中点
31         if(juge(mid))   //判断是在中点的哪边
32             r=mid;
33         else
34             l=mid+1;
35     }
36     LL sum3=0;
37     for(int i=m-1;i>=0;i--){
38         sum3+=a[i];
39         if(sum3>r){
40             sum3=0;
41             k--;
42             ans[++i]=1;   //标记
43         }
44     }
45
46     while(k>1){     //如果没有划完,接着划,这里按照题意,需要使前面的尽可能小,所以从前面开始划..(从1开始是因为输出的关系吧,我猜的,还不是很明白...)
47         for(int i=1;i<m;i++){
48             if(!ans[i]) {
49                 ans[i]=1;    //标记
50                 k--;
51                 break;
52             }
53         }
54     }
55     //print();
56     printf("%d",a[0]);
57     for(int i=1;i<m;i++){
58         if(ans[i]) printf(" /");
59         printf(" %d",a[i]);
60     }
61     printf("\n");
62 }
63 int main()
64 {
65     int T;
66     scanf("%d",&T);
67     while(T--){
68         scanf("%d%d",&m,&k);
69         for(int i=0;i<m;i++){
70             scanf("%d",&a[i]);
71             if(min>a[i]) min=a[i];  //计算范围
72             sum+=a[i];            //计算范围
73         }
74         zhaox();    // 找x的函数加上输出的部分
75     }
76     return 0;
77 }

实验代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define LL long long
 4 int min=0,m,k,a[505],ans[505];
 5 LL sum=0,j=1;
 6 bool juge(LL x)
 7 {
 8     printf("\n第%d次\n",j++);
 9     LL sum2=0;
10     int t=k;
11     for(int i=0;i<m;i++){
12         sum2+=a[i];
13         if(sum2>x){
14             sum2=0;
15             t--;
16             i--;
17             printf("%d ",a[i]);
18         }
19
20         if(!t){
21             if(i!=m-1) {printf("\n%d\n",0); printf("%d %d\n",a[i],a[m-1]); return false;}
22             else {printf("\n%d\n",1); return true;}
23         }
24     }
25     printf("\n%d\n",1);
26     return true;
27 }
28
29 void zhaox()
30 {
31     memset(ans,0,sizeof(ans));
32     LL l=min,r=sum,mid;
33     while(l<r){
34         mid=(r+l)/2;
35         printf("\nx=%d\n",r);
36         if(juge(mid))
37             r=mid;
38         else
39             l=mid+1;
40     }
41     LL sum3=0;
42     for(int i=m-1;i>=0;i--){
43         sum3+=a[i];
44         if(sum3>r){
45             sum3=0;
46             k--;
47             ans[++i]=1;
48         }
49     }
50     while(k>1){
51         for(int i=1;i<m;i++){
52             if(!ans[i]) {
53                 ans[i]=1;
54                 k--;
55                 break;
56             }
57         }
58     }
59     //print();
60     printf("%d",a[0]);
61     for(int i=1;i<m;i++){
62         if(ans[i]) printf(" /");
63         printf(" %d",a[i]);
64     }
65     printf("\n");
66 }
67 int main()
68 {
69     int T;
70     scanf("%d",&T);
71     while(T--){
72         scanf("%d%d",&m,&k);
73         for(int i=0;i<m;i++){
74             scanf("%d",&a[i]);
75             if(min>a[i]) min=a[i];
76             sum+=a[i];
77         }
78         zhaox();
79     }
80     return 0;
81 }
时间: 2024-08-06 02:54:06

抄书(UVa714)的相关文章

codevs 3162 抄书问题

3162 抄书问题 题目描述 Description 现在要把M本有顺序的书分给K个人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比如不能把第一.第三.第四本数给同一个人抄写.现在请你设计一种方案,使得复制时间最短.复制时间为抄写页数最多的人用去的时间. 输入描述 Input Description 第一行两个整数M.K:(K<=M<=100) 第二行M个整数,第i个整数表示第i本书的页数. 输出描述 Output Descri

codevs3162抄书问题(划分型dp)

3162 抄书问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 现在要把M本有顺序的书分给K个人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比如不能把第一.第三.第四本数给同一个人抄写.现在请你设计一种方案,使得复制时间最短.复制时间为抄写页数最多的人用去的时间. 输入描述 Input Description 第一行两个整数M.K:(K<=M<=100

TCP/IP 网络编程 (抄书笔记 1) -- TCP

TCP/IP 网络编程 (抄书笔记 1) – TCP TCP/IP 网络编程 (抄书笔记 1) – TCP Table of Contents server client 更好的 client 端实现 来源: <TCP/IP 网络编程> 抄书: 通信的双方都各自 拥有 输入缓存和输出缓存 socket 的 write 函数并不是立即传输数据, 而是写到输出缓存区, 到达另一端的输入缓存区 socket 的 read 函数调用的瞬间, 就从输入缓存区中读取数据 TCP 协议中的滑动窗口会保证 数

TCP/IP 网络编程 (抄书笔记 2) -- UDP

TCP/IP 网络编程 (抄书笔记 2) – UDP TCP/IP 网络编程 (抄书笔记 2) – UDP Table of Contents server client connect 来源: <TCP/IP 网络编程> 抄书: TCP 协议若要向 10 个客户端提供服务, 除了需要 listen 套接字外, 还需要 10 个服务器端套接字 (accept), 但是在 UDP 中, 不管是服务器端还是客户端都只需要 1 个套接字 udp 的 client 不需要 bind, 调用 sendt

TCP/IP 网络编程 (抄书笔记 4) -- 管道: 进程间通信

TCP/IP 网络编程 (抄书笔记 4) – 管道: 进程间通信 TCP/IP 网络编程 (抄书笔记 4) – 管道: 进程间通信 int fds[2]; pipe(fds); write(fds[1], buf, strlen(buf)); read(fds[0], buf, BUF_SIZE); 如果两个进程的通信只是 单纯的一方写, 然后另一方读 的情况, 那么 我们的管道操作没有问题, 但是: char str1[] = "str1"; char str2[] = "

TCP/IP 网络编程 (抄书笔记 3) -- 僵尸进程和多任务并发服务器

TCP/IP 网络编程 (抄书笔记 3) – 僵尸进程和多任务并发服务器 TCP/IP 网络编程 (抄书笔记 3) – 僵尸进程和多任务并发服务器 Table of Contents 僵尸进程的产生 避免僵尸进程 信号 多任务的并发服务器 僵尸进程的产生 子进程先退出, 父进程没有退出 ==> 僵尸进程 父进程先退出, 子进程没有退出 ==> 子进程被 0 号进程回收, 不会产生僵尸进程 pid_t pid = fork(); if (pid == 0) { // child printf(&

UVa714 Copying Books (二分法,贪心)

链接:http://vjudge.net/problem/UVA-714 分析:二分枚举最小值,用贪心的思想每段序列尽量往右划分,注意:因为要求字典序最小解,输出时还有一个贪心过程,左起S[i]尽量小,相当于右起S[j]尽量大,还有一种情况是剩下的数之和已经小于等于ans,但是此时剩余要分配的组数还有多(remain>1). 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 usin

3162 抄书问题(划分dp)

3162 抄书问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 现在要把M本有顺序的书分给K个人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比如不能把第一.第三.第四本数给同一个人抄写.现在请你设计一种方案,使得复制时间最短.复制时间为抄写页数最多的人用去的时间. 输入描述 Input Description 第一行两个整数M.K:(

循环冗余校验, 抄书

循环冗余校验, 抄书 在每个数据块 (称之为帧) 中加入一个 FCS (Frame CheckSequence,帧检查序列), 用于发送/接收装置比较帧的正确与否 循环冗余检验: CRC (cycli redundancy check) M 表示传输的数据 k 表示传输的数据的长度 n 位冗余码, 可以用 M * 2^n 得到 (n+k) 位得到的数除以 收发双方事先商定的长度为 (n+1) 位的除数 P, 计算出 余数 (不是数学上的余数) 这个余数成为 帧检验序列 FCS (frame ch