题目背景 在金融市场上,经常需要统计一只股票的最大回撤。最大回撤是指投资者在某天买入,在之后的某天卖出,可能造成的最大亏损,它可以反应一只股票在历史上的最坏表现。
表示同一只股票的在某一天的价格,请计算这只股票的最大回撤。即寻找两个下标满足
输入格式 第一行:单个整数表示 n, 第二行: n 个整数表示 1 , 2 , 3 , ⋯ , a 1 ,a 2 ,a 3 ,⋯,a n 。
输出格式 单个整数:表示这只股票的最大回撤,如果价格一直上涨,输出 0。
另外,我知道有一个和这个类似的题但他的代码过不了这道题
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
long long maxx=-158454554541,minn=1410215454,n,a[1000000];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
maxx=max(a[i],maxx);
minn=min(a[i],minn);
}
if(minn==a[1])
{
cout<<0<<endl;
return 0;
}
cout<<maxx-minn<<endl;
return 0;
}