Pick apples(完全背包+贪心)
Description
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.
Input
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.
Output
For each case, first output the case number then follow the most profits she can gain.
Sample
Input
1
1 1
2 1
3 1
6
Output
Case 1: 6
Hint
Source
#include <stdio.h> #include <vector> #include <string.h> #include <iostream> #include <algorithm> #include <queue> using namespace std; /*小范围完全背包,大范围贪心*/ long long dp[1100009]; struct node { int a,b; double c;//体积/价格 } a[4]; bool cmp(node a,node b) { return a.c<b.c; } long long max1(long long a,long long b) { return a>b?a:b; } int main() { int t,case1=1; scanf("%d",&t); while(t--) { memset(dp,0,sizeof(dp)); long long v; for(int i=1; i<=3; i++) { scanf("%d%d",&a[i].a,&a[i].b); a[i].c=a[i].a*1.0/a[i].b;//计算体积/钱 } cin>>v; sort(a+1,a+4,cmp);//数组a是从1开始被输入的 long long ans =0; if(v>1000000) { ans=(((v-1000000)/a[1].a)*a[1].b);//v-1000000是因为想用贪心的办法把那(v-1000000)个位置填满,免得超时(体积太大) v=1000000+((v-1000000)%a[1].a);//计算(v-1000000)后剩余的空间 } for(int i=1; i<=3; i++) for(long long j=a[i].a; j<=v; j++)//用完全背包计算这个能装多少 dp[j]=max1(dp[j],dp[j-a[i].a]+a[i].b); printf("Case %d: %lld\n",case1++,ans+dp[v]); } return 0; }