- 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}232.
样例输入
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;
}