EOJ:Stock Market

Stock Market

Time Limit: 4000MS Memory Limit: 65536K
Total Submits: 134 Accepted: 29

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.
                   Today's price
| Tomorrow's price
| | Two days hence
Stock | | |
1 10 15 15
2 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

* 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

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

 

Sample Input

2 3 1010 15 1513 11 20

Sample Output

24

原题地址:http://www.cn210.com/onlinejudge/problemshow.php?pro_id=193

___________________________________________________________________________________________________

 

题解:

若一只股票持有N天,相当于第一天买进,第二天卖出,第二天买进,第三天卖出,第三天卖出……第N天买进,第N天卖出。

因此,每一天的收益只与前一天有关。每一天可看成无限背包,背包的容量为前一天的最大收益,物品重量为当天价格,物品收益为两天价格之差。

 

 

由于恶心的OJ卡时间,所以加了个优化应付特殊数据,当重量相同时,只保留价值最高的。(注:这个优化只能用于无限背包,01背包不能使用。)

代码
1 #include<stdio.h>
2  int stock[15][55],dp[100000000],temp[15][55],flag[15][55],min[15];
3  int s,d,m,t,v,i,j,k,a,b;
4  int max(int a,int b)
5 {
6 if (a>b) return a;
7 return b;
8 }
9  void swap(int i,int j,int t)
10 {
11 int x;
12 x=temp[t][i];
13 temp[t][i]=temp[t][j];
14 temp[t][j]=x;
15 }
16  void qsort(int l,int r,int t)
17 {
18 int i,j,a;
19 long long x;
20 x=stock[t+1][temp[t][r]]-stock[t][temp[t][r]];
21 i=l-1;
22 for (j=l;j<r;j++)
23 {
24 a=stock[t+1][temp[t][j]]-stock[t][temp[t][j]];
25 if (a>x||a==x&&stock[t][temp[t][j]]<=stock[t][temp[t][r]])
26 {
27 i++;
28 swap(i,j,t);
29 }
30 }
31 swap(i+1,r,t);
32 if (i+2<r) qsort(i+2,r,t);
33 if (i>l) qsort(l,i,t);
34 }
35  int main()
36 {
37 scanf("%d%d%d",&s,&d,&m);
38 for (i=1;i<=d;i++)
39 min[i]=1000000000;
40 for (j=1;j<=s;j++)
41 for (i=1;i<=d;i++)
42 {
43 scanf("%d",&stock[i][j]);
44 if (stock[i][j]<min[i])
45 min[i]=stock[i][j];
46 temp[i][j]=j;
47 }
48 for (i=1;i<=d;i++)
49 {
50 qsort(1,s,i);
51 t=1000000000;
52 a=stock[i+1][temp[i][1]]-stock[i][temp[i][1]];
53 b=stock[i][temp[i][1]];
54 for (j=1;j<=s;j++)
55 {
56 if (t==stock[i+1][temp[i][j]]-stock[i][temp[i][j]]||stock[i+1][temp[i][j]]-stock[i][temp[i][j]]<a&&stock[i][temp[i][j]]>b)
57 flag[i][temp[i][j]]=1;
58 else t=stock[i+1][temp[i][j]]-stock[i][temp[i][j]];
59 }
60 }
61 v=m;
62 for (i=1;i<=d;i++)
63 {
64 t=0;
65 for (j=0;j<=v;j++)
66 dp[j]=0;
67 for (j=1;j<=s;j++)
68 if (flag[i][j]==0)
69 for (k=min[i];k<=v;k++)
70 {
71 if (k-stock[i][j]<0) continue;
72 dp[k]=max(dp[k],dp[k-stock[i][j]]+stock[i+1][j]-stock[i][j]);
73 if (dp[k]>t)
74 t=dp[k];
75 }
76 v=t+v;
77 }
78 printf("%d\n",v);
79 return 0;
80 }
81  

 

posted on 2010-07-28 14:01  风也轻云也淡  阅读(215)  评论(0编辑  收藏  举报