[简单DP] uva12951 Stock Market

 题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4830

A beginner investor wants to learn how to invest in the stock market. As he does not have anyexperience, he selected one company and followed daily the value of the stock during N days. At theend, he wondered how much money he would have won if he had invested during the time he followedthe stock value. To be honest, the investor is multi billionaire and has a lot of money, enough to buyany amount of stock actions of the company. However, as he is very careful with his investments, hedecided that he would never have more than one stock of the company.

To cover his costs, the stockbroker charges a fixed rate of C dollars for every stock purchase.

You have to calculate the maximum profit that the investor could have won investing during the Ndays, having also the option of not to invest any money.

Input

The input consists of several test cases. The first line of a test case contains two integers, N and C(1 ≤ N ≤ 2 × 105, 0 ≤ C ≤ 30). The second line contains the N prices P1, P2, . . . , PN of the days 1,2, . . . , N, respectively. Every price Pi satisfies 1 ≤ Pi ≤ 1000.

Output

For each test case in the input your program must produce exactly one line, containing exactly oneinteger, the maximum profit of the investor, in dollars.Sample Input6 10100 120 130 80 50 405 1070 80 50 40 5013 3010 80 20 40 30 50 40 60 50 70 60 10 200Sample Output200220


----------------------------------------------------我是分割线-------------------------------------------------------------------------------------

题目的意思就是买股票。提供的是一家公司的股票的日价格。

然后有以下两个要求:

每次买需要C的额外花费。

手上最多只有一股。

问最大的收获是多少。

----------------------------------------------------我是分割线--------------------------------------------------------------------------------------

思路简单明了,就是DP。

dp[i][1/0]表示前几天的最大收获,第二维的1表示手上还有一只股票,0表示手上没有股票。

那么我们可以容易想到转移。

1.对于dp[i][0]来说。那么它是怎么来的呢?

1).第i-1天,有股票,卖了;

2).第i-1天没有股票,但是我第i天也不买股票;

也就是dp[i][0]=max(dp[i-1][1]+cost[i],dp[i-1][0]);

2.对于dp[i][1]来说。它是怎么来的呢?

1).第i-1天有股票,但是我第i天还是不卖;

2).第i-1天没有股票,我第i天买了股票;

也就是dp[i][1]=max(dp[i-1][1],dp[i-1][0]-cost[i]-C);

----------------------------------------------------我是分割线--------------------------------------------------------------------------------------

到这里的话应该没什么问题了。接下来就是贴代码的时候了。


#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define MAXN 200000
#define INF 100000000000000
long long dp[MAXN+10][3];
long long cost[MAXN+10];
int main()
{
	int n,fixc;
	while(scanf("%d%d",&n,&fixc)!=EOF) {
		for(int i=1; i<=n; i++)
			scanf("%lld",&cost[i]);
		memset(dp,0,sizeof(dp));
		for(int i=0;i<=n;i++)
			dp[i][1]=-INF;
		for(int i=1; i<=n; i++) {
			dp[i][0]=max(dp[i-1][0],dp[i-1][1]+cost[i]);
			dp[i][1]=max(dp[i-1][1],dp[i-1][0]-cost[i]-fixc);
		}
		printf("%lld\n",max(dp[n][0],dp[n][1]));
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值