1578: [Usaco2009 Feb]Stock Market
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 613 Solved: 349
[Submit][Status][Discuss]
Description
Despite their natural prudence, the cows are still taking a beating in the home mortgage market, and now they're starting to get their hands on the stock market. Bessie is very prescient. She not only knows the price of S (2 <= S <= 50) stocks today, but also the next D (2 <= D <= 10) days (including today). Given a stock price matrix for D days (1 <= price <= 1000) and initial capital M (1 <= M <= 200,000), find an optimal buying and selling strategy that maximizes total profit. You must buy multiples of the stock price each time, and you don't need to spend all your money (or even none). It is agreed that your profit cannot exceed 500,000. Consider this bull case example (which is Bessie's favorite). In this example, there are S=2 stocks and D=3 days. The cow has 10 dollars to invest. Today's Price | Tomorrow's Price | | The Day After Tomorrow's Price Stocks | | | 1 10 15 15 2 13 11 20 The following strategies can be used to maximize profits, and buy the first stock on the first day. Sold it the next day and quickly bought a second one with 4 left. Sell the second stock on the last day, at this time a total of 4+20=24 money.
Input
* First line: integers separated by three spaces: S, D, M
* Row 2..S+1: Row s+1 contains the price of the sth stock on days 1..D
Output
* First line: The maximum possible amount of money after selling the stock on the last day.
Sample Input
10 15 15
13 11 20
Sample Output
HINT
Source
answer:
complete backpack
1 #include<cstring> 2 #include<cmath> 3 #include<cstdio> 4 #include<algorithm> 5 #include<iostream> 6 7 #define S 51 8 #define D 11 9 #define M 500010 10 11 #define Wb putchar(' ') 12 #define We putchar('\n') 13 using namespace std; 14 inline int read() 15 { 16 int x=0,f=1;char ch=getchar(); 17 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 18 while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} 19 return x*f; 20 } 21 inline void write(int x) 22 { 23 if(x<0) putchar('-'),x=-x; 24 if (x==0) putchar(48); 25 int num=0;char c[15]; 26 while(x) c[++num]=(x%10)+48,x/=10; 27 while(num) putchar(c[num--]); 28 } 29 30 int p[D][S], best[D], dp[M]; 31 template<class T>T _max(T a,T b) 32 { 33 return (a>b)?a:b; 34 } 35 36 int tmp[S], top; 37 int s,d,m; 38 39 40 int main() 41 { 42 s=read(),d=read(),m=read(); 43 44 for(int i=1;i<=s;i++) 45 for(int j=1;j<=d;j++) 46 p[j][i]=read(); 47 48 best[1]=m; 49 for(int i=2;i<=d;i++) 50 { 51 top=0; 52 for(int j=1;j<=s;j++) 53 if (p[i][j]>p[i-1][j]) 54 tmp[++top]=j; 55 for(int k=1;k<=best[i-1];k++) dp[k]=k; 56 for(int j=1;j<=top;j++) 57 for(int k=p[i-1][tmp[j]];k<=best[i-1];k++) 58 dp[k]=_max(dp[k],dp[k-p[i-1][tmp[j]]]+p[i][tmp[j]]); 59 best[i]= _max(best[i],dp[best[i-1]]); 60 } 61 write(best[d]); 62 }