Daydreaming Stockbroker

题目描述

Gina Reed, the famous stockbroker, is having a slow day at work, and between rounds of solitaire she is daydreaming. Foretelling the future is hard, but imagine if you could just go back in time and use your knowledge of stock price history in order to maximize your profits!

Now Gina starts to wonder: if she were to go back in time a few days and bring a measly $100 with her, how much money could she make by just buying and selling stock in Rollercoaster Inc. (the most volatile stock in existence) at the right times? Would she earn enough to retire comfortably in a mansion on Tenerife?
Note that Gina can not buy fractional shares, she must buy whole shares in Rollercoaster Inc. The total number of shares in Rollercoaster Inc. is 100 000, so Gina can not own more than 100 000 shares at any time. In Gina’s daydream, the world is nice and simple: there are no fees for buying and selling stocks, stock prices change only once per day, and her trading does not influence the valuation of the stock.

输入

The first line of input contains an integer d (1 ≤ d ≤ 365), the number of days that Gina goes back in time in her daydream. Then follow d lines, the i’th of which contains an integer pi (1 ≤ p i ≤ 500) giving the price at which Gina can buy or sell stock in Rollercoaster Inc. on day i. Days are ordered from oldest to newest.

 

输出

Output the maximum possible amount of money Gina can have on the last day. Note that the answer may exceed 232 .

样例输入

6
100
200
100
150
125
300

 

样例输出

650

 

[提交][状态]

Gina Reed是一个炒股人,刚开始有100美金,输入一个整数n表示接下来n天的股价变化情况,输出Gina Reed最后能得到的最大利润。数据类型可能超过int,所以这里我们定义成long long.

坑点:Gina Reed手中拥有的股份最多不能超过100 000.

Gina Reed一定会在股价低的时候买下股票,并在股票高的时候卖出。所以我们要看这一天Gina Reed手中是否有股票,若Gina Reed手中没有股票且第二天的股价高于这一天的股价,那么他就要买下今天的股票(这样就算是第二天卖出也会赚一笔QAQ);若Gina Reed手中有股票且第二天的股价低于这一天的股价,那么他就要卖出手中的股票(这样赚的钱可以买第二天的股票呀,至少股份变多了,不过是否要买还要看第二天的和第二天的下一天的股价涨跌情况)。

AC代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
const ll maxx=100000;
int n,i;
ll p[500];
ll sum,num;
void solve()
{
    int f=0;//0表示手中没有股份,1表示手中有股份
    num=0;sum=100;
    for(i=0;i<n;i++)
    {
        if(f==0&&p[i+1]>p[i])
        {
            num=min(sum/p[i],maxx);
            sum-=num*p[i];
            f=1;
        }
        if(f==1&&p[i+1]<p[i])
        {
            sum+=num*p[i];
            num=0;
            f=0;
        }
    }
    cout<<sum<<endl;
    return ;
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0);
    cin>>n;
    for(i=0;i<n;i++)
        cin>>p[i];
    solve();
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值