hiho一下 第195周 奖券兑换[C solution][Accepted]

时间限制:20000ms
单点时限:1000ms
内存限制:256MB

描述

小Hi在游乐园中获得了M张奖券,这些奖券可以用来兑换奖品。

可供兑换的奖品一共有N件。第i件奖品需要Wi张奖券才能兑换到,其价值是Pi。  

小Hi使用不超过M张奖券所能兑换到的最大奖品总价值是多少?

输入

第一行两个整数N,M。  

接下来N行,每行两个整数Wi,Pi。  

对于 50%的数据: 1≤N,M≤1000  

对于 100%的数据: 1≤N,M≤105,1≤Pi,Wi≤10。  

输出

一行一个整数,表示最大的价值。

样例输入
3 10  
2 3  
8 8   
10 10
样例输出
11 
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 4 int main(void){
 5     int n = 0, m = 0;
 6     int ret = 0;
 7     scanf("%d %d", &n, &m);
 8     int **array = (int**)malloc((n+1)*sizeof(int*));
 9     array[0] = (int*)malloc((m+1)*sizeof(int));
10     for (int j = 0; j < m+1; j++){
11         array[0][j] = 0;
12     }
13     int **map = (int**)malloc(11*sizeof(int*));
14     for (int i = 0; i < 11; i++){
15         map[i] = (int*)calloc(11,sizeof(int));
16     }
17     int value = 0, weight = 0;
18     for (int i = 1; i < n + 1; i++){
19         scanf("%d %d", &weight, &value);
20         map[weight][value]++;
21     }
22     int count = 1;
23     for (int temp_weight = 1; temp_weight < 11; temp_weight++){
24         for (int temp_value = 1; temp_value < 11; temp_value++){
25             if (map[temp_weight][temp_value] != 0){
26                 int temp = 1;
27                 int temp_sum = 0;
28                 while (temp_sum + temp <= map[temp_weight][temp_value]){
29                     temp_sum = temp_sum + temp;
30                     array[count] = (int*)malloc((m + 1)*sizeof(int));
31                     array[count][0] = 0;
32                     weight = temp*temp_weight;
33                     value = temp*temp_value;
34                     for (int j = 1; j < m + 1; j++){
35                         if (j - weight<0){
36                             array[count][j] = array[count - 1][j];
37                         }
38                         else{
39                             array[count][j] = array[count - 1][j]>array[count - 1][j - weight] + value ? array[count - 1][j] : array[count - 1][j - weight] + value;
40                         }
41                     }
42                     free(array[count - 1]);
43                     count++;
44                     temp = temp * 2;
45                 }
46                 if (map[temp_weight][temp_value] - temp_sum >0){
47                     temp = map[temp_weight][temp_value] - temp_sum;
48                     array[count] = (int*)malloc((m + 1)*sizeof(int));
49                     array[count][0] = 0;
50                     weight = temp*temp_weight;
51                     value = temp*temp_value;
52                     for (int j = 1; j < m + 1; j++){
53                         if (j - weight<0){
54                             array[count][j] = array[count - 1][j];
55                         }
56                         else{
57                             array[count][j] = array[count - 1][j]>array[count - 1][j - weight] + value ? array[count - 1][j] : array[count - 1][j - weight] + value;
58                         }
59                     }
60                     free(array[count - 1]);
61                     count++;
62                 }
63             }
64         }
65     }
66     printf("%d", array[count-1][m]);
67     return 0;
68 }

参考:https://hihocoder.com/discuss/question/5199

posted @ 2018-03-30 17:41  Blue_Keroro  阅读(227)  评论(0编辑  收藏  举报