hihocoder - 股票价格3 - 维护栈 & 线段树
2017-11-05 15:58
260 查看
题目
时间限制: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
题解
单调栈法#include<stdio.h> #include<iostream> #include<cmath> #include<cstring> #include<algorithm> #include<map> #include<vector> #include<queue> #include<stack> #include<deque> using namespace std; typedef long long LL; #define ms(a,b) memset(a,b,sizeof(a)) #define eps 1e-8 const int maxn=1e5+100; int A[maxn]; int B[maxn]; int main() { int n; cin>>n; for(int i=0;i<n;i++) cin>>A[i]; stack<pair<int,int> >s; for(int i=0;i<n;i++){ if(s.empty() || s.top().first>=A[i]){ s.push({A[i],i}); }else{ while(!s.empty() && s.top().first<A[i]){ B[s.top().second]=i-s.top().second; s.pop(); } s.push({A[i],i}); } } while (!s.empty()) { B[s.top().second] = -1; s.pop(); } for(int i=0;i<n;i++) printf("%d\n",B[i]); return 0; }
相关文章推荐
- [hihocoder]#1619“共同富裕” && #1620股票价格3
- zoj 3363 线段树维护最大值 Alice's present
- [树状数组套权值线段树 || 分块] BZOJ 2120 数颜色 & BZOJ 2453 维护队列
- 【维护区间最长连续子序列 && 线段树 && 区间归并】HDU - 1540 Tunnel Warfare
- 【BZOJ 2957】楼房重建&&Codechef COT5 Count on a Treap&&【NOIP模拟赛】Weed 线段树的分治维护
- hihocoder 股票价格
- POJ 2886 Who Gets the Most Candies? (高合成数&用线段树维护约瑟夫环)
- 【HIHOCODER 1576】 子树中的最小权值(线段树维护DFS序)
- poj(2828)——Buy Tickets(线段树维护点&点更新)
- hihocoder 股票价格
- 【线段树维护区间编号 && 区间更新】HDU - 4614 Vases and Flowers
- bzoj 1798 && 5039: [Jsoi2014]序列维护(线段树)
- poj 2528 Mayor's posters(扫描线+堆维护||离散化+线段树)
- Screensaver<用线段树维护函数系数>
- BZOJ3673 & BZOJ3674 可持续化并查集 【可持续化线段树维护可持续化数组】
- [hihoCoder] #1306 : 股票价格 解题报告
- UVa 11235 / POJ 3368 Frequent values (想法题&RMQ线段树维护最长区间长度)
- hihocoder 1080 线段树:区间加法&赋值
- BZOJ3673 & BZOJ3674 可持续化并查集 【可持续化线段树维护可持续化数组】
- 【Hihocoder 1167】 高等理论计算机科学 (树链的交,线段树或树状数组维护区间和)