NYOJ 654 enjoys playing with warcraft ltl

Source: Internet
Author: User

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;}
       
      


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.