Poj 2063 investment (full backpack)

Source: Internet
Author: User
Tags stock prices

Poj 2063 investment (full backpack)

Http://poj.org/problem? Id = 2063

Question:

Initially, M (M <= 10000) is USD. Now there are n stocks, and if each stock is bought, the current profit is also given (you can purchase them infinitely ). suppose you bought a stock in USD m in the first year. At the end of this year, you will sell all the original stock prices and get the profit of all your shares. then you start investing in the stock for another 2nd years... (the stock price is an integer multiple of 1000, and the annual return rate of each stock is <= 10%)

Now you need to buy a t-year (T <= 40) stock continuously and ask you how much your total money is after T-years.

Analysis:

Since there are n stocks and each stock can be bought without limit, this question can regard every year's investment as a complete backpack problem. then, we will continue to invest in the next year with the profit and capital of the previous year (it is also a complete backpack process ).

Since the annual investment principal can still be returned at the end of the year, we only need to find the maximum annual profit.So this question can be changed to the following question:

You have M dollars in your hand, and you can buy n kinds of items without limit. Each item has a different price for cost [I] and value for Val [I]. now you need to maximize the total value of the item sum_val at the cost of <= m usd?

The above is a standard complete backpack problem. however, the m in this question is <= 10000, and the maximum stock income is 10% and the maximum investment is 40 years. Therefore, about 10000 * (1.1) ^ 40 = 4500w. we cannot use the DP [450000000] array for every round.

The topic says that every stock price is a multiple of 1000, and here we only need to find the maximum profit we can get with m usd a year.So we turn the problem above into the following:

Initially, you have m/1000 resources in your hand (each resource represents 1000 USD). You can buy n kinds of items, each item requires cost [I]/1000 resources to buy and you can get the value of Val [I]. How much value can you get?

In this way, we only need to enable DP [450000] for our array. Now we can solve a complete backpack problem.

Make the DP [I] [J] = x table use the first item to obtain the maximum value x in total cost <= J.

Initialization: DP all 0.

State Transfer: DP [I] [J] = max (DP [I-1] [J], DP [I] [J-cost [I] + val [I])

The former indicates that neither the I-th item is bought, and the latter indicates that the I-th item is bought at least.

In the end, we are looking for DP [N] [M]. (This is the biggest profit this year)

At the end of each round of investment, you can add the maximum profit of the current year to the principal m to get the number of principal at the beginning of the next year. After a total of T years, you can obtain the final maximum amount.

AC code:

# Include <algorithm> # include <cstring> # include <cstdio> using namespace STD; const int maxn = 46000 + 5; int N; // a total of N types of funds int m; // initial m usd int t; // The T-year int cost needs to be bought [10 + 5]; // the price of each fund x US $ int Val [10 + 5]; // annual earnings of each fund x USD int DP [maxn]; int main () {int t; scanf ("% d", & T); While (t --) {// read input data scanf ("% d", & M, & T, & N); For (INT I = 1; I <= N; I ++) {scanf ("% d", & cost [I], & Val [I]); cost [I] // = 1000; // cost [I] Thousands of US dollars is required for the I-th stock} // The annual processing process for (INT loop = 1; loop <= T; loop ++) {int max_money = m/1000; // max_money is the current year's usage (thousands of US dollars) memset (DP, 0, sizeof (DP )); // recursive process for (INT I = 1; I <= N; I ++) {for (Int J = cost [I]; j <= max_money; j ++) DP [J] = max (DP [J], DP [J-cost [I] + val [I]);} m + = DP [max_money]; // The current year's profit is superimposed on the total money} // output result printf ("% d \ n", m);} return 0 ;}

Poj 2063 investment (full backpack)

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.