poj 1442 单调队列

题意:求第k小的值
解法:两个堆栈进行维护,大顶堆的top小于小顶堆的top,那么小顶堆的所有值都小于,每次维护足够的小顶堆数量就好了。

priority_queue< int , vector< int >, greater< int > > q;//小顶堆
priority_queue<int, vector<int>, less<int> > q;//大顶堆

 
 
  1. /************************************************************************* 
  2.     > File Name: poj1442.cpp 
  3.     > Author: cy 
  4.     > Mail: 1002@qq.com  
  5.     > Created Time: 14/11/9 21:43:21 
  6.  ************************************************************************/ 
  7.  
  8. #include<iostream> 
  9. #include<cstring> 
  10. #include <algorithm> 
  11. #include<cstdlib> 
  12. #include<vector> 
  13. #include<cmath> 
  14. #include<stdlib.h> 
  15. #include<iomanip> 
  16. #include<list> 
  17. #include<deque> 
  18. #include<map> 
  19. #include <stdio.h> 
  20. #include <queue> 
  21.  
  22. const int maxn=30000+5
  23.  
  24. #define inf 0x3f3f3f3f 
  25.   #define INF 0x3FFFFFFFFFFFFFFFLL 
  26. #define rep(i,n) for(i=0;i<n;i++) 
  27.  #define reP(i,n) for(i=1;i<=n;i++) 
  28.  
  29. #define ull unsigned long long 
  30.  #define ll long long 
  31.  
  32. #define cle(a) memset(a,0,sizeof(a)) 
  33.  
  34. using namespace std; 
  35. int val[maxn]; 
  36. int n,m; 
  37. int main() 
  38. #ifndef ONLINE_JUDGE 
  39.      freopen("in.txt","r",stdin); 
  40.      //freopen("out.txt","w",stdout); 
  41. #endif 
  42.     while(cin>>n>>m) 
  43.     { 
  44.         int i; 
  45.         priority_queue<int , vector<int>, greater<int> >up;   
  46.         priority_queue<int , vector<int>, less <int> >down;  
  47.         rep(i,n) 
  48.         { 
  49.             scanf("%d",&val[i]); 
  50.         } 
  51.         int now=0
  52.         rep(i,m) 
  53.         { 
  54.             int type; 
  55.             scanf("%d",&type); 
  56.             while(type>now) 
  57.             { 
  58.                 up.push(val[now++]); 
  59.             } 
  60.             while(!down.empty()&&down.top()>up.top()) 
  61.             { 
  62.                 int temp=up.top();up.pop(); 
  63.                 up.push(down.top());down.pop(); 
  64.                 down.push(temp); 
  65.             } 
  66.             printf("%d\\n",up.top()); 
  67.             int temp=up.top();up.pop();down.push(temp); 
  68.         } 
  69.     } 
  70.     return 0
  71. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值