USACO Stock Market

Luo Gu P2938 [USACO09FEB] Stock Market Stock Market

Luo Gu Portal

JDOJ 2625: USACO 2009 Feb Gold 2.Stock Market

JDOJ Portal

Title Description

Despite their innate prudence, the cows took a beating in the home mortgage market and now are trying their hand at stocks. Happily, Bessie is prescient and knows not only today's S (2 <= S <= 50) stock prices but also the future stock prices for a total of D days (2 <= D <= 10).

Given the matrix of current and future stock prices on various days (1 <= PR_sd <= 1,000) and an initial M (1 <= M <= 200,000) units of money, determine an optimal buying and selling strategy in order to maximize the gain realized by selling stock on the final day. Shares must be purchased in integer multiples, and you need not spend all the money (or any money). It is guaranteed that you will not be able to earn a profit of more than 500,000 units of money.

Consider the example below of a bull (i.e., improving) market, the kind Bessie likes most. In this case, S=2 stocks and D=3 days. The cows have 10 units of money to invest.

|Stock|Today's price| Tomorrow's price| | Two days hence Stock | | | :-----------: | :-----------: | :-----------: | :-----------: | | AA | 10 | 15 | 15 | |BB | 13| 11|20 |

If money is to be made, the cows must purchase stock 1 on day 1. Selling stock 1 on day 2 and quickly buying stock 2 yields 4 money in the bank and one share of 2. Selling stock 2 on the final day brings in 20 money for a total of 24 money when the 20 is added to the bank.

Input Format

* Line 1: Three space-separated integers: S, D, and M

* Lines 2..S+1: Line s+1 contains the D prices for stock s on days 1..D: PR_sd

Output Format

* Line 1: The maximum amount of money possible to have after selling on day D.

Translation of the meaning of problems

Title Description

Although cows born cautious, they are still dealt a heavy blow in the mortgage credit market, and now they are ready to try their luck in the stock market. Bessie had inside information, she knows S S stocks in the next D D price days.

Assuming that in the beginning, she raised M M dollars, then how she can earn the most money operating it? Bessie every day to buy and sell many stocks, you can buy and sell the same stocks, trading unit must be an integer, not the number. For example, a bull:

Bessie assume there are 10 yuan principal, stock prices are as follows:

stock Today's price Tomorrow price Acquired prices
AA 10 15 15
B B 13 11 20

The most profitable approach is: buy today A A -share one, tomorrow to sell it and buy a B share, sell B shares acquired, so Bessie to have 24 yuan.

Input Format

First row: three integer S, D and M, 2 ≤ S ≤ 502≤ S ≦ 50; 102≤ ≤ D 2 ≤ D ≦ 10; 2000001≤. 1 ≤ M ≤ M ≤200000

The second line to the first line S + 1: the first line has a D i + 1 integers: of P_ {I;}. 1 P I ; {. 1 to I of P_;} D P I ; D , I represents I stocks at the the last day-to-day price for all ≤ J ≤ D1≤. 1 JD , * P ** Pi1≤. 1 ≤ I ; ≤ 1000 J J * ≤1000

Output Format

Single integer: Indicates the maximum number of cows can get the money to ensure that this number is not more than 500,000,500,000

Sample input and output

Input # 1 copy

Output # 1 copy

answer:

A weird knapsack problem.

First, we can be clear, that is the decision of DP:

In the first \ (i \) days, there are so few decisions are made:

The first is: do not buy.

The second: After buying the next day to sell.

Third: After buying and then selling days in the hands of the deposit.

But the third decision can be converted into a second decision, the principle is this:

For a stock, our first \ (i \) day to buy the first \ (j \) days sell, the effect can be seen as the first \ (i \) days of purchase, the \ (k \) days sell ( \ (i \ Le k \ Le J \) ), buy back the same day, the first \ (j \) days sell.

In this case, our third decision-making can become: After buying the next day to sell, buy back the next day. This solves no after-effect of DP issues. We can begin to design the DP process.

According to the above analysis, we found that buying stocks into a neighbor or two days. So, each day for each item are only two options: to buy or not to buy.

Eh? It seems to be completely backpack Hey!

So, for every day we do a complete Backpack: This backpack is the current volume of funds, the volume of each item is the value of the day, the previous day's value to the value of the day's value minus.

So we can run \ (d-1 \) times completely backpack, select an answer biggest.

code show as below:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int s,d,m,maxx;
int map[60][20],dp[500001];
int main()
{
    scanf("%d%d%d",&s,&d,&m);
    for(int i=1;i<=s;i++)
        for(int j=1;j<=d;j++)
            scanf("%d",&map[i][j]);
    for(int i=2;i<=d;i++)
    {
        maxx=-1;
        memset(dp,0,sizeof(dp));
        for(int j=1;j<=s;j++)
            for(int k=map[j][i-1];k<=m;k++)
            {
                dp[k]=max(dp[k],dp[k-map[j][i-1]]+map[j][i]-map[j][i-1]);
                maxx=max(maxx,dp[k]);
            }
        m+=maxx;
    }
    printf("%d",m);
    return 0;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11609733.html