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

2015年(115)

我的朋友

分类: Python/Ruby

2015-08-06 16:52:00

原文地址:采果子(fruit.pas) 作者:runningdark

 

【描述】

萌萌今天心情不错,于是走到郊外旅游。他边走边向四周望望,发现周围有许多果树。萌萌数出了这些果树上果子的个数,并且估了估每个的价格(真是个奇怪的人(lz:我也觉得……))。萌萌规定了一种采摘方案,就是对于第i棵树来说,它有a[i]个果子,且这棵树上的所有果子的价格为s[i],摘取时间为c[i]。那么,当你摘了第i个树上的果子后,后面你所选择去摘的果树上的果子数必须大于第i棵树上的果子数目,说白了就是一个上升序列,而且他不可以往回走。他只有k小时,那么,在萌萌所拥有的限定时间内,他想知道,这样摘取得最大值是多少?

【输入格式】

第一行2个数:n(表示这条路上的大树数),k(总共时间)

接下来第n+1行,每行三个数:

a[i](每棵树上的果子),s[i](这棵树上的所有果子的价格),c[i](在每棵树上花费的时间)

【输出格式】

一个数,按这样方法摘取的最大值:m

输入样例

4 3

2 2 1

2 3 2 

1 7 1

4 6 2

输出样例

13

(先摘第3棵树上的,再摘第4棵树上的)

数据范围

N,k<=10000;a[i],s[i]<=maxint;

 

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX 10000

  4. int getmaxValue(int input[][3], int count, int time){
  5.     if(input == NULL ) return 0;
  6.     int i = 0;
  7.     int choiceCount = 1;
  8.     //value, time, fruit count restrict
  9.     int choice[MAX][3] = {{0,time,0}};
  10.     int maxValue = 0;
  11.     for(;i<count;i++){
  12.         int t = 0;
  13.         int tmax = choiceCount;
  14.         for(;t<tmax;t++){
  15.             //pick the tree[i]
  16.             if(choice[t][2]<input[i][0] && choice[t][1] - input[i][2]>=0){
  17.                 choice[choiceCount][0] = choice[t][0]+ input[i][1];
  18.                 choice[choiceCount][1] = choice[t][1] - input[i][2];
  19.                 choice[choiceCount][2] = input[i][0];
  20.                 maxValue = choice[choiceCount][0]>maxValue?choice[choiceCount][0] : maxValue;
  21.                 choiceCount++;
  22.             }
  23.         }
  24.     }
  25.     return maxValue;
  26. }

  27. int main(){
  28.     //fruit count/value/time cost
  29.     int input[][3] = {{2,8,1},{2,3,2},{1,7,1}, {4,6,2}};
  30.     int count = 4;
  31.     int time = 3;
  32.     printf("%d \n", getmaxValue(input, count, time));
  33. }


  1. use strict;
  2. #tree infomation . 1st element means apples amount, 2nd means values, 3rd means time cost
  3. my @data =
  4. (2, 8, 1,
  5.     2,3,2,
  6.     1,7,1,
  7.     4,6,2);
  8. #tree amount
  9. my $trees = 4;
  10. #time restriction
  11. my $hours = 3;

  12. #use to store the max values picked
  13. my $rvalues = 0;
  14. #use to store the tree no picked
  15. my @rtr;

  16. #read tree values by index
  17. sub Read{
  18.     my $i = shift;
  19.     #return mount, value, and cost;
  20.     return $data[$i*3], $data[$i*3+1],$data[$i*3+2];
  21. }

  22. sub f{
  23.     my ($index) = @_[3];
  24.     for($index..$trees-1){
  25.         my($tvalue, $trestrict, $tlhours, undef, @tresult) = @_;
  26.         my ($amount,$value, $cost) = Read($_);
  27.         #push the tree into result if the apple amount larger than picked trees.
  28.         if($amount>$trestrict){
  29.             $tvalue += $value;
  30.             $trestrict = $amount;
  31.             $tlhours = $tlhours - $cost;
  32.             if($tlhours>=0){
  33.                 push(@tresult, $_);
  34. #                print "pick apple from tree $_ \n Now the result is @tresult \n";
  35.             }
  36.             else{
  37.                 next;
  38.             }
  39.             if($tvalue >$rvalues){
  40.                 $rvalues = $tvalue;
  41.                 @rtr = @tresult;
  42.             }
  43.             f($tvalue,$trestrict,$tlhours, $_+1, @tresult);
  44.         }
  45.     }

  46. }

  47. my @t;
  48. f(0,0,$hours,0,@t);

  49. print "$rvalues: @rtr\n";

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

上一篇:采蘑菇(fungus.pas)

下一篇:抄写员

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