[HDU 2126] buy the souvenirs (Dynamic Planning)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2126

Question: give you n items, m yuan, and ask you how many items you can buy and how many solutions are available.

 

At first, I thought that it was a greedy algorithm to solve the problem of giving m yuan money because I could purchase enough items because I spent less money.

The maximum number of purchased items is C.

Then, the design status DP [I] [J] indicates how many solutions I had to buy C items when I spent J yuan in my previous items.

Later I found that the state dimension was not enough. I had to think about it again.

So I thought:

Design status DP [I] [J] [k] indicates that I bought J items from my previous items, and the amount of money spent cannot exceed K yuan.

State transition equation: DP [I] [J] [k] = DP [I-1] [J-1] [k-A [I] + dp [I-1] [J] [k]

That is, J items were bought from the previous I items, the amount of money spent does not exceed K yuan of the number of items in the former I-1 to buy J spend less than K Yuan and the former I-1 bought a J-1, the sum of the number of K-A [I] solutions.

Code:

 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <cmath> 5 #include <map> 6 #include <iterator> 7 #include <vector> 8 using namespace std; 9 typedef long long LL;10 11 int T,n,m;12 const int MAX_N = 33;13 int a[MAX_N];14 int dp[MAX_N][MAX_N][555];15 16 int main(){17     scanf("%d",&T);18     while(T--){19         scanf("%d%d",&n,&m);20         for(int i=1;i<=n;i++){21             scanf("%d",&a[i]);22         }23         memset(dp,0,sizeof(dp));24 //        dp[0][0][0] = 1;25         for(int i=0;i<=m;i++) dp[0][0][i] = 1;26         for(int i=1;i<=n;i++){27             for(int j=0;j<=i;j++){28                 for(int k=m;k>=0;k--){29                     dp[i][j][k] += dp[i-1][j][k];30                     if( k-a[i]>=0 ) dp[i][j][k] += dp[i-1][j-1][k-a[i]];31                 }32             }33         }34         int maxn = 0;35         for(int i=0;i<=n;i++){36             if( dp[n][i][m] ) maxn = i;37         }38         if( maxn ) printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",dp[n][maxn][m],maxn);39         else puts("Sorry, you can‘t buy anything.");40     }41     return 0;42 }

 

[HDU 2126] buy the souvenirs (Dynamic Planning)

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.