股票价格
时间限制:20000ms
单点时限:2000ms
内存限制:256MB
描述
小Hi最近在分析一支股票的价格走势,他需要一个程序来辅助分析。这个程序会接收3种消息(指令):
价格信息,格式是P timestamp price:表示这支股票在 timestamp 时刻价格是 price。
删除价格指令,格式是R timestamp:随着时间推移,小Hi会积累越来越多的价格数据。一些老旧的数据会变得不重要。这个指定会删除 timestamp以前(包括 timestamp 时刻)的价格数据。
价格查询指令,格式是Q:小Hi希望程序返回这只股票最高、最低和最近的价格。注意已经被删除的价格不应该被统计。
给定一个包含以上3种信息(指令)的序列,你能否帮助小Hi完成这个程序呢?
输入
第1行包含一个整数 N (1 ≤ N ≤ 500000),表示消息(指令)序列的长度。
第2 - N+1行,每行包含一条消息或指令。
输入保证价格信息是按照 timestamp 升序排列的,并且出现的 timestamp 和价格小于100000000。
输出
对于输入中每一条价格查询指令,输出当时最高、最低和最近的价格。
- 样例输入
-
10 P 1 77 P 2 73 P 5 70 P 7 74 Q R 4 Q P 8 78 R 5 Q
- 样例输出
-
77 70 74 74 70 74 78 74 78
思路:利用map存答案,优先队列进行操作;#include<bits/stdc++.h> using namespace std; #define ll __int64 #define inf 1e18 struct is { int num; int pos; friend bool operator <(is a,is b) { return a.pos>b.pos; } }; char a[10]; priority_queue<is>q; map<int,int>m; int main() { int x,y,z,i,t; int st=0,en=1000000001,pre; scanf("%d",&x); while(x--) { scanf("%s",a); if(a[0]=='P') { is gg; scanf("%d%d",&gg.pos,&gg.num); pre=gg.num; q.push(gg); m[gg.num]++; } else if(a[0]=='R') { scanf("%d",&z); while(!q.empty()) { if(q.top().pos<=z) { int v=q.top().num; m[v]--; if(m[v]==0) m.erase(v); q.pop(); } else break; } } else { int ansmax=(--m.end())->first; int ansmin=m.lower_bound(st)->first; printf("%d %d %d ",ansmax,ansmin,pre); } } return 0; }