第一场-D-Daydreaming Stockbroker

题目链接:点击打开链接

(一)题面:

Description

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?

Photo by liz west on flickr

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.

Input

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 ≤ pi ≤ 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

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

Sample Input

6
100
200
100
150
125
300

Sample Output

650

(二)题目大意:

已知你初始时有100金币,你要去买股票,给出n天中每一天股票的单价格,即每一股的价格,你可以以这个价格买入股票,也可以以该价格卖出你已经有的股票。并且任意时刻你拥有的股票数不能超过10000股。问:n天后你最多拥有多少金币。


(三)解题思路:

  1. 虑贪心的想法:低价的时候买入出股票,高价的时候卖出股票。
  2. 低价是指当天的价格低于前一天的价格以及后一天的价格,而高价是指当天的价格高于前一天以及后一天的价格。
(四)具体代码(队友写的代码):
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long

using namespace std;
int main()
{
    #ifdef AFei
    freopen("in.txt", "r", stdin);
    #endif // AFei
    int d;
    int a[400], f[400];
    scanf("%d", &d);
    a[0] = 1000;

    for(int i = 1; i <= d; ++ i)
    {
        scanf("%d", &a[i]);
        if(a[i] == a[i-1])
        {
            i --;
            d --;
        }
    }
    a[d+1] = -1;
    for(int i = 0; i <= d; ++ i)
    {
        if(a[i+1] > a[i])
            f[i] = 1;
        else if(a[i+1] < a[i])
            f[i] = -1;
        else
            f[i] = 0;
    }
    ll money = 100;
    int cpnum = 0;
    for(int i = 1; i <= d; ++ i)
    {
        if(a[i] <= money && f[i] == 1 && f[i-1] == -1)
        {
            ll t = money/a[i];
            if(t >= 1e5)    t = 1e5;
            cpnum = t;
            money -= t*a[i];
        }
        else if(f[i] == -1 && f[i-1] == 1)
        {
            money += cpnum * a[i];
            cpnum = 0;
        }
    }
    cout << money << endl;
    return 0;
}



猜你喜欢

转载自blog.csdn.net/xbb224007/article/details/79983678