SDUT ACM 2408 Pick apples 贪心+完全背包

 

Pick apples

Time Limit: 1000MS Memory limit: 165536K

题目描述

Once ago, there is a mystery yard which only produces three kinds of apples. The number of each kind is infinite. A girl carrying a big bag comes into the yard. She is so surprised because she has never seen so many apples before. Each kind of apple has a size and a price to be sold. Now the little girl wants to gain more profits, but she does not know how. So she asks you for help, and tell she the most profits she can gain.

输入

In the first line there is an integer T (T <= 50), indicates the number of test cases.
In each case, there are four lines. In the first three lines, there are two integers S and P in each line, which indicates the size (1 <= S<= 100) and the price (1 <= P <= 10000) of this kind of apple.

In the fourth line there is an integer V,(1 <= V <= 100,000,000)indicates the volume of the girl's bag.

输出

For each case, first output the case number then follow the most profits she can gain.

示例输入

1
1 1
2 1
3 1
6

示例输出

Case 1: 6

来源

2012年"浪潮杯"山东省第三届ACM大学生程序设计竞赛
 
 
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 long long dp[1110];
 5 int main()
 6 {
 7     int t, sum;
 8     long long s[5], p[5]; //这里必须用long long,不知道原因。。。
 9     double val, max;
10     scanf("%d", &t);
11     for(int k = 1; k <= t; k++)
12     {
13         int x = 0;
14         double val, max = 0;
15         memset(dp, 0, sizeof(dp));
16         for(int i = 0; i < 3; i++)
17         {
18             scanf("%lld %lld", &s[i], &p[i]);
19             val = 1.0 * p[i] / s[i];
20             if(val > max)
21             {
22                 max = val;
23                 x = i; //记录性价比最高的苹果的下标
24             }
25         }
26         scanf("%d", &sum); //背包总容量
27         if(sum <= 1000) //1000以下用完全背包,超出就贪心加背包
28         {
29             for(int i = 0; i < 3; i++)
30                 for(int v = s[i]; v <= sum; v++)
31                     dp[v] = std::max(dp[v], dp[v-s[i]]+p[i]);
32             printf("Case %d: %lld\n", k, dp[sum]);
33         }
34         else
35         {
36             int lem = sum - 1000; //用于贪心的背包容量
37             int n = lem / s[x]; //能装的性价比最高的苹果的数量
38             long long w = n * p[x]; //贪心所得的利润
39             int cur = sum - n * s[x]; //剩余的背包容量,剩余的用完全背包
40             for(int i = 0; i < 3; i++)
41                 for(int v = s[i]; v <= cur; v++)
42                     dp[v] = std::max(dp[v], dp[v-s[i]]+p[i]);
43             printf("Case %d: %lld\n", k, dp[cur] + w);
44         }
45     }
46     return 0;
47 }

 

posted @ 2013-04-20 18:07  Anti-Magic  阅读(202)  评论(0编辑  收藏  举报