From: http://blog.csdn.net/a601025382s/article/details/12308193
Question:
Enter the volume n of the backpack, the volume S1 of the green gem, the value V1, the volume S2 of the Sapphire, the value V2, and the number of the precious stones are infinite. What is the maximum value of the backpack?
Question:
It looks like a full backpack, but the data is very big (although not given, you can also guess, or it is too watery), so you cannot use a backpack. There are only two types of items. When we think of greed, we give priority to the value-to-volume ratio (known as the value ratio. But the size limit is not enough. You also need to enumerate the number of gems with a smaller value ratio and determine whether the desired value can be increased. We can also know that for a backpack with a volume of M = lcm (S1, S2), the value ratio is definitely greater. So we only need to enumerate the volume of N-N/m + m at most. If the value is smaller than this value, there is more than m free space, this free space must be placed with a large value.
Note:
1. When there is not a public multiple, be careful when calculating .. I got an error ..
2. During enumeration, select max (S1, S2) for the span. This is an optimization. If not, it will be TLE.
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 #include<set> 10 #include<string> 11 //#include<pair> 12 13 #define N 10000005 14 #define M 1005 15 #define mod 1000000007 16 //#define p 10000007 17 #define inf 0x3f3f3f3f 18 #define mod2 100000000 19 #define ll long long 20 #define LL long long 21 #define maxi(a,b) (a)>(b)? (a) : (b) 22 #define mini(a,b) (a)<(b)? (a) : (b) 23 24 using namespace std; 25 26 ll n,s1,v1,s2,v2; 27 ll ans; 28 ll ma; 29 ll g; 30 ll c1,c2; 31 ll ans1,ans2; 32 33 ll gcd(ll a,ll b) 34 { 35 return b==0?a:gcd(b,a%b); 36 } 37 ll lcm(ll a,ll b) 38 { 39 return a/gcd(a,b)*b; 40 } 41 42 void ini() 43 { 44 ans=0; 45 ans1=ans2=0; 46 scanf("%I64d%I64d%I64d%I64d%I64d",&n,&s1,&v1,&s2,&v2); 47 g=lcm(s1,s2); 48 c1=g/s1; 49 c2=g/s2; 50 } 51 52 53 54 void solve() 55 { 56 if(n/g>=1) 57 ans1=(n/g-1)*max(c1*v1,c2*v2); 58 ll i; 59 if(s1<s2){ 60 swap(c1,c2); 61 swap(s1,s2); 62 swap(v1,v2); 63 } 64 ll left=n%g; 65 if(n/g>=1) 66 left+=g; 67 ll en=left/s1; 68 ll re; 69 for(i=0;i<=en;i++){ 70 re=i*v1+(left-i*s1)/s2*v2; 71 ans2=max(ans2,re); 72 } 73 ans=ans1+ans2; 74 } 75 76 void out() 77 { 78 printf("%I64d\n",ans); 79 } 80 81 int main() 82 { 83 int T; 84 //freopen("data.in","r",stdin); 85 //freopen("data.out","w",stdout); 86 scanf("%d",&T); 87 for(int ccnt=1;ccnt<=T;ccnt++) 88 // while(T--) 89 // while(scanf("")) 90 { 91 //if(n==0 && k==0 ) break; 92 93 ini(); 94 solve(); 95 printf("Case #%d: ",ccnt); 96 out(); 97 } 98 99 return 0;100 }
HDU 4091 zombie's treasure chest greedy + Enumeration