Chinaunix首页 | 论坛 | 博客
  • 博客访问: 65399
  • 博文数量: 115
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-08 19:09
文章分类
文章存档

2015年(115)

我的朋友

分类: Python/Ruby

2015-08-06 16:52:04

原文地址:抄写员 作者:runningdark

这个代码的控件写的太不好了, 注释有点符号格式就乱了, 真无语。。

〖题目描述〗
假设有M本书(编号为12,…M),想将每本复制一份,M本书的页数可能不同(分别是P
1
P2,…PM)。任务是将这M本书分给K个抄写员(K=M〉,每本书只能分配给一个抄写
员进行复制,而每个抄写员所分配到的书必须是连续顺序的。
意思是说,存在一个连续升序数列 0 = bo < b1 < b2 < < bk-1 < bk = m,这样,第
I
号抄写员得到的书稿是从bi-1+1到第bi本书。复制工作是同时开始进行的,并且每个抄写
员复制的速度都是一样的。所以,复制完所有书稿所需时间取决于分配得到最多工作的那
个抄写员的复制时间。试找一个最优分配方案,使分配给每一个抄写员的页数的最大值尽
可能小(如存在多个最优方案,只输出其中一种)。
(GDOI\'\'99 Books).

〖输入格式〗
第一行两个数m,k:表示书本数目与人数;第二行m个由空格分隔的整数,m本书每本的页数.


〖输出格式〗
k行。每行两个整数:第I行表示第I抄写员抄的书本编号起止。

〖输入输出样例〗

输入样例
9 3
1 2 3 4 5 6 7 8 9

 

输出样例  

1 5
6 7
8 9


不好做的一题。

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define MAX 1000

  4. int split(int *books, int count, int group){
  5.     if(books == NULL) return -1;
  6.     int gcount = 0;
  7.     int record[MAX][MAX] = {{0}};
  8.     record[0][0] = books[0];
  9.     int i = 1;
  10.     for(;i<count;i++){
  11.         record[gcount][i] = record[gcount][i-1]+books[i];
  12.     }
  13.         gcount++;
  14.     for(;gcount<group;gcount++){
  15.         i = gcount;
  16.         for(;i<count; i++){
  17.             //printf("i = %d \n", i);
  18.             //get value;
  19.             int max = books[i]>record[gcount-1][i-1]? books[i]:record[gcount-1][i-1];
  20.             int t;
  21.             for(t = gcount-1; t<i; t++){
  22.                 int right = record[0][i] - record[0][t];
  23.                 int left = record[gcount-1][t];
  24.                 int v = right>left?right:left;
  25.                 max = max<v?max:v;
  26.                 //printf("left = %d, right = %d \n", left, right);
  27.             }
  28.             record[gcount][i] = max;
  29.             printf("fill (%d,%d) = %d \n", gcount, i, record[gcount][i]);
  30.         }
  31.     }
  32. }
  33. int main(){
  34.     int books[] = {1,2,3,4,5,6,7,8,9};
  35.     int count = sizeof(books)/sizeof(int);
  36.     int group = 3;
  37.     printf("%d \n", split(books, count, group));
  38. }


 

 

 

曾经的回溯法备份。

点击(此处)折叠或打开

  1. my $m = 9;
  2. my $k = 3;
  3. my @books = (9,8,7,6,5,4,3,2,1);

  4. #for array, sp can get the index of number. If you split the array @a at the index, you can get 2 arrays, whose max sum is the smallest
  5. #this is just for optmize the last step. can be skipped and just enumrate all possiblities in the recursive function.
  6. sub sp{
  7.     my @a = @_;
  8.     my ($value, $index);
  9.     for(1..(@a-1)){
  10.         my ($l, $r) = (0, 0);
  11.         for(0..$_-1){
  12.             $l+=$a[$_]
  13.         }
  14.         for($_..@a-1){
  15.             $r+=$a[$_];
  16.         }
  17.         my $t = $l;
  18.         $t = $r if($r>$l);
  19.         if($t<$value or $value == 0){
  20.             $value = $t;
  21.             $index = $_;
  22.         }
  23.     }
  24.     return $value, $index-1;
  25. }

  26. #$max is the sum restrict
  27. #$starti means the start index of @books, where we try to create a new group from. This is determined by the last group's end index
  28. #$count means the amount of left group need to fill
  29. #@is is use to store the book start index and end index of every group.
  30. sub f{
  31.     my $max = shift;
  32.     my $starti = shift;
  33.     my $count = shift;
  34.     my @is = @_;

  35.     #if current sum restrict is large than success group's sum, it is not a good choice. So we drop it and return directly.
  36.     #If $maxs(best choice's sum) is 0, it means there is no success group until now, so we continue
  37.     if($max>$maxs and $maxs!=0){
  38.         #print "$max > $maxs No Value !!! Drop it!!!\n";
  39.         return;
  40.     }

  41.     #if only 2 group left, we use sp to get be best split of the left array
  42.     if($count <= 2){

  43.         #the books left, from $books[$starti] to end
  44.         my @tbooks = @books;
  45.         for(0..$starti-1){
  46.             shift(@tbooks);
  47.         }

  48.         my ($v, $in) = sp(@tbooks);

  49.         #if the max sum of the left 2 group is larger than the finished group, we place the max sum value.
  50.         $max = $v if($v>$max);

  51.      #push the end index of the last second group
  52.         push(@is, $in+$starti);
  53.         #push the start index of the last group
  54.         push(@is, $in+$starti +1);
  55.         #push the end index of the last group
  56.         push(@is, $m-1);

  57.         #if current groups's max sum is small than the current record, we replace, or we drop it.
  58.         if($max<$maxs or $maxs == 0){
  59.             $maxs = $max;
  60.             @res = @is;
  61.         }
  62.         return;
  63.     }

  64.     #try all possible amount (from 1 book to $m-$starti-$count books) for current group !!!!!!!!!!!!!!!!!!!!!!!!!!that's wrong!!!!!!
  65.     #why $m -$count? No group is empty, so we have $count groups need to fill, we should left $count books for other group.
  66.     #$m-$starti means the amount of books left, which need to assign to $count worker.
  67.     for my $c (1..$m-$starti - $count +1 ){
  68.         my $t = 0;

  69.         #create dumy array to store current choice.
  70.         my @tis = @is;

  71.         #calculate the sum of current group
  72.         for(1.. $c){
  73.             $t = $t + $books[$starti+$_-1];
  74.         }

  75.         #push the start index 0 if the groups are empty
  76.         if(@tis == 0){
  77.             push(@tis, 0);
  78.         }
  79.         #push the end index of the group.
  80.         push(@tis, $starti+$c-1);

  81.         #push the start index of next group;
  82.         push(@tis, $starti+$c);

  83.         #modify the max sum of current groups if current group's sum is max.
  84.         $max = $t if($t>$max);

  85.         #we assign $c book from the start index $starti, so the next group


 

阅读(258) | 评论(0) | 转发(0) |
0

上一篇:采果子(fruit.pas)

下一篇:多米诺骨牌

给主人留下些什么吧!~~