版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Kwong_young/article/details/80660328
描述
小Hi最近在关注股票,为了计算股票可能的盈利,他获取了一只股票最近N天的价格A1~AN。
在小Hi的策略中,每天可以在下列三种操作中选取一种:
1.什么也不做;
2.按照当天的价格买进一个单位的股票;
3.按照当天的价格卖出部分或所有股票。
现在小Hi希望能够知道,如果在N天前手中持有的股票数为0,并且假设拥有无限的金钱,在这N天结束能够获得的最大利润是多少?
输入
第一行包含一个整数N。
第二行包含N个整数,A1, A2, ... AN。
对于30%的数据, 1 ≤ N ≤ 103
对于100%的数据,1 ≤ N ≤ 106, 1 ≤ Ai ≤ 100
输出
输出这N天结束能够获得的最大利润。
5 1 2 3 4 5样例输出
10
超简单的一个枚举题目。。 想了一个多小时,越来越蔡了
#include <iostream>
#include <stack>
using namespace std;
int main() {
// freopen("a.txt","r",stdin);
long long out = 0;
stack<int> s;
int count = 0;
int start = 0;
long long sum = 0;
int a,b;
cin>>b;
for(int i =0;i<b;i++){
cin >> a;
s.push(a);
}
while(!s.empty()){
a = s.top();
s.pop();
if(a >= start){
out += count*start - sum;
start = a;
count = 0;
sum = 0;
}else{
sum += a;
count ++;
if(s.size()==0){
out += count*start - sum;
}
}
}
cout << out<<endl;
return 0;
}