Love to play warcraft ltl time limit: 2000 MS | memory limit: 65535 KB difficulty: 3
-
Description
-
LtlHe is very fond of playing with warcraft, because warcraft pays great attention to the overall strength of the team, and he is working hard to upgrade the team without dragging down the team.
He now has many locations to choose to upgrade, but he needs to buy enough supplies and appropriate items at each location, so as not to be killed by monsters during the refresh process, the monsters in every location will be lost after they have been beaten (and even money and equipment will not be lost ), in addition, as long as the location is selected, all the monsters in the location will be refreshed, and the corresponding experience will be obtained. NowLtlYou can give money for each location to buy supplies and items, and the experience that all monsters can gain, but the money he has is certain. So he wants to know how to select a location to make him have the most experience.
-
Input
-
The first line is an integer T, indicating that the number of test data groups is 0. Two integers N, M, 0 in the second row The next N rows have two integers, ci, vi, (0 Output
-
An integer in one row indicates the maximum experience value that ltl can obtain.
-
Sample Input
-
23 107 72 33 52 53 52 1
-
Sample output
-
Max experience: 12Max experience: 6
-
After reading the question, I immediately decided on the 01 backpack problem and wrote the code directly. Unfortunately, TLE arrived unexpectedly!
-
Timeout code:
-
#include
struct node{int c;int w;}num[105];int dp[1000005];int Max(int a,int b){return a>b?a:b;}int main(){int T,n,m;int i,j;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);for(i=0;i
=num[i].c;j--){dp[j]=Max(dp[j],dp[j-num[i].c]+num[i].w);}}printf("Max experience: %d\n",dp[m]);}return 0;}
The optimization code is as follows:
-
#include
struct node{int c;int w;}num[105];int dp[1000005];int Max(int a,int b){return a>b?a:b;}int main(){int T,n,m;int i,j,s,count;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);s=0;for(i=0;i
=count;j--){dp[j]=Max(dp[j],dp[j-num[i].c]+num[i].w);}}printf("Max experience: %d\n",dp[m]);}return 0;}