股票价格3
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
小Hi最近在关注股票,为了计算股票可能的盈利,他获取了一只股票最近N天的价格A1~AN。
小Hi想知道,对于第i天的股票价格Ai,几天之后股价会第一次超过Ai。
假设A=[69, 73, 68, 81, 82],则对于A1=69,1天之后的股票价格就超过了A1;对于A2=73,则是2天之后股票价格才超过A2。
输入
第一行包含一个整数N。
以下N行每行包含一个整数Ai。
对于50%的数据,1 ≤ N ≤ 1000
对于100%的数据,1 ≤ N ≤ 100000, 1 ≤ Ai ≤ 1000000
输出
输出N行,其中第i行代表对于第i天的股票价格Ai,几天之后股价会第一次超过Ai。
如果Ai+1~AN之内没有超过Ai,输出-1。
- 样例输入
-
5
69
73
68
81
82 - 样例输出
-
1
2
1
1
-1
之前做过很多这样的题,但是不知道归类与单调队列,还是DP
具体的可以看看----这几道题。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int a[maxn],r[maxn];
int main()
{
int i,j,n;
scanf("%d",&n);
for(i=;i<=n;i++) scanf("%d",&a[i]);
r[n]=-;
for(i=n-;i>=;i--){
int tmp=i+;
while(true){
if(tmp==-){ r[i]=-; break; }
if(a[tmp]>a[i]){ r[i]=tmp; break; }
tmp=r[tmp];
}
}
for(i=;i<=n;i++) printf("%d\n",r[i]>?r[i]-i:-);
return ;
}