D. Daydreaming Stockbroker

本文探讨了一位著名股票经纪人在幻想中回到过去利用已知的股价历史来最大化收益的投资策略。通过模拟购买和出售某公司股票的过程,计算出在特定规则下所能获得的最大利润。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

编辑代码
  •  1000ms
  •  65536K
 

 

Gina Reed, the famous stockbroker, is having a slow day at work, and between rounds of solitaire she is day-dreaming. 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 RollercoasterInc. The total number of shares in Rollercoaster Inc. is 100 000, so Gina can not own more than100 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 2^{32}2​32​​. 

 

 

 

样例输入

6
100
200
100
150
125
300

样例输出

650

 

 

 

 

 

智商已废 = = 

 

感觉代码还挺漂亮 存下。

ans记录当前总价值,s记录有多少个r价值的股票。每次升值的时候  ,r升值,每次可以买更多股的时候 买更多股 。价格10万的限制就好 -- 

智商已废 - -  找感觉ing

 

贪心思路。。股票当然是越过股越好了 。因为如果卖的多的话就卖的更多 。

然后总价值用ans记录  ,刚刚好 股数就可以非常方便的计算出来 ,

然后 升值的时候,把ans的差价升值上去。把股数不变,价值变高 ,然后 就动态起来了 = = 好神奇

贪心思路出的动态规划吧

 

#include<iostream>
#include<functional>
#include<cmath>
#include<queue>
using namespace std;
int main()
{
    long long n;
    while(cin>>n)
    {
        long long x,ans=100,s=0,r=100;
        for(long long i=0;i<n;i++)
        {
            cin>>x;
            if(x<=r)
            {
                s=ans/x;
                r=x;
            }
            if(s>100000) s=100000; 
            if(x>r)
            {
                ans+=s*x-r*s;
                r=x;
            }
            //cout<<ans<< ' '<<s<< ' '<<r<<endl;
        }
        cout<<ans<<endl;
    }
}

 

2021.6.3 更新 

 

-----------------------------------

更新更新 当年题解写的太差

就是以总价值为核心,当总价值升高的时候,就记录下最高的总价值,后续都以总价值为核心,让自己的股数越多越好。所以只记录当前最大的总价值,以及当前最大可持有的股数

当时写了俩特判 可能当时脑子比较好吧 其实更容易看懂的写法是下面的

 更符合逻辑的写法是这样 

#include <bits/stdc++.h>
using namespace std;
int main(){
    long long n , x ,  total = 100 , max_num = 0 , unit_cost = 1e9  ;
    cin>>n;
    for(int i = 0 ; i < n; ++i){
        cin>>x;
        long long this_num = total /x;
        //如果可以获取更多的数量 就直接买
        if (this_num > max_num && this_num < 1e5 ){
            max_num = this_num;
            // 更新单价
            unit_cost = x;
        }
        //数量限制的特判
        if(this_num >= 1e5 || this_num >= max_num){
            max_num = min((long long)1e5 , this_num);
            //如果可以使单价更低 更新单价
            unit_cost = min(unit_cost , x) ;
        }
        long long this_total = max_num * x  -  max_num * unit_cost +total;
        //如果可以获得更大价值  更新价值 更新单价
        if(this_total > total){
            total = this_total;
            //更新单价
            unit_cost = x;
        }
    }
    cout<<total   <<endl;
}

 

优化一下 

#include <bits/stdc++.h>
using namespace std;
int main(){
    long long n , x ,  total = 100 , max_num = 0 , unit_cost = 1e9  ;
    cin>>n;
    for(int i = 0 ; i < n; ++i){
        cin>>x;
        long long this_num = total /x;
        //数量限制的特判
        if(this_num >= 1e5 || this_num >= max_num){
            max_num = min((long long)1e5 , this_num);
            //如果可以使单价更低 更新单价
            unit_cost = min(unit_cost , x) ;
        }
        long long this_total = max_num * x  -  max_num * unit_cost +total;
        //如果可以获得更大价值  更新价值 更新单价
        if(this_total > total){
            total = this_total;
            //更新单价
            unit_cost = x;
        }
    }
    cout<<total   <<endl;
}

 

 

 

 

### 关于Python实现玻尔兹曼机 #### 使用GitHub资源中的Boltzmann Machine实例 对于希望了解如何利用Python来构建基于Boltzmann Machines的推荐系统的开发者来说,可以访问特定的GitHub仓库。此仓库包含了三个不同类型的Boltzmann Machines实验模型[^1]。 ```python import numpy as np class RBM: def __init__(self, num_visible, num_hidden): self.num_hidden = num_hidden self.num_visible = num_visible self.debug_print = True # Initialize a weight matrix, of dimensions (num_visible x num_hidden), using # a uniform distribution between -sqrt(6. / (num_hidden + num_visible)) # and sqrt(6. / (num_hidden + num_visible)). One could vary the factor 6 here. np_rng = np.random.RandomState(1234) self.weights = np.asarray(np_rng.uniform( low=-0.1 * np.sqrt(6. / (num_hidden + num_visible)), high=0.1 * np.sqrt(6. / (num_hidden + num_visible)), size=(num_visible, num_hidden))) # Insert weights for the bias units into the first row and first column. self.weights = np.insert(self.weights, 0, 0, axis = 0) self.weights = np.insert(self.weights, 0, 0, axis = 1) def train(self, data, max_epochs = 1000, learning_rate=0.1): """ Train the machine. Parameters ---------- data : A matrix where each row is a training example consisting of the states of visible units. """ num_examples = data.shape[0] # Insert bias units of 1 into the first column. data = np.insert(data, 0, 1, axis = 1) for epoch in range(max_epochs): # Clamp to the data and sample from the hidden units. # (This is the "positive CD phase", aka the reality phase.) pos_hidden_activations = np.dot(data, self.weights) pos_hidden_probs = self._logistic(pos_hidden_activations) pos_hidden_states = pos_hidden_probs > np.random.rand(num_examples, self.num_hidden + 1) # Note that we're using the activation *probabilities* of the hidden states, not the hidden states # themselves, when computing associations. We could also use the states; see section 3 of Hinton's # "A Practical Guide to Training Restricted Boltzmann Machines" for more. pos_associations = np.dot(data.T, pos_hidden_probs) # Reconstruct the visible units and sample again from the hidden units. # (This is the "negative CD phase", aka the daydreaming phase.) neg_visible_activations = np.dot(pos_hidden_states, self.weights.T) neg_visible_probs = self._logistic(neg_visible_activations) neg_visible_probs[:,0] = 1 # Fix the bias unit. neg_hidden_activations = np.dot(neg_visible_probs, self.weights) neg_hidden_probs = self._logistic(neg_hidden_activations) neg_associations = np.dot(neg_visible_probs.T, neg_hidden_probs) # Update weights. self.weights += learning_rate * ((pos_associations - neg_associations) / num_examples) error = np.sum((data - neg_visible_probs) ** 2) if self.debug_print: print("Epoch %s: error is %s" % (epoch, error)) def run_visible(self, data): """Assuming the RBM has been trained (so that weights for the network have been learned), run the network on a set of visible units, to get a sample of the hidden units.""" num_examples = data.shape[0] # Create a matrix, where each row is to be the hidden units (plus a bias unit) # sampled from a training example. hidden_states = np.ones((num_examples, self.num_hidden + 1)) # Insert bias units of 1 into the first column of data. data = np.insert(data, 0, 1, axis = 1) # Calculate the activations of the hidden units. hidden_activations = np.dot(data, self.weights) # Calculate the probabilities of turning the hidden units on. hidden_probs = self._logistic(hidden_activations) # Turn the hidden units on with their specified probabilities. hidden_states[:,:] = hidden_probs > np.random.rand(num_examples, self.num_hidden + 1) # Always fix the bias unit to 1. # hidden_states[:,0] = 1 # Ignore the bias units. hidden_states = hidden_states[:,1:] return hidden_states # TODO: Remove the code duplication between this method and `run_visible`? def run_hidden(self, data): """Assuming the RBM has been trained (so that weights for the network have been learned), run the network on a set of hidden units, to get a sample of the visible units.""" num_examples = data.shape[0] # Create a matrix, where each row is to be the visible units (plus a bias unit) # sampled from a training example. visible_states = np.ones((num_examples, self.num_visible + 1)) # Insert bias units of 1 into the first column of data. data = np.insert(data, 0, 1, axis = 1) # Calculate the activations of the visible units.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值