题意:求第k小的值
解法:两个堆栈进行维护,大顶堆的top小于小顶堆的top,那么小顶堆的所有值都小于,每次维护足够的小顶堆数量就好了。
priority_queue<
int
, vector<
int
>, greater<
int
> > q;//小顶堆
priority_queue<
int
, vector<
int
>, less<
int
> > q;//大顶堆
-
-
-
-
-
-
-
- #include<iostream>
- #include<cstring>
- #include <algorithm>
- #include<cstdlib>
- #include<vector>
- #include<cmath>
- #include<stdlib.h>
- #include<iomanip>
- #include<list>
- #include<deque>
- #include<map>
- #include <stdio.h>
- #include <queue>
-
- const int maxn=30000+5;
-
- #define inf 0x3f3f3f3f
- #define INF 0x3FFFFFFFFFFFFFFFLL
- #define rep(i,n) for(i=0;i<n;i++)
- #define reP(i,n) for(i=1;i<=n;i++)
-
- #define ull unsigned long long
- #define ll long long
-
- #define cle(a) memset(a,0,sizeof(a))
-
- using namespace std;
- int val[maxn];
- int n,m;
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("in.txt","r",stdin);
-
- #endif
- while(cin>>n>>m)
- {
- int i;
- priority_queue<int , vector<int>, greater<int> >up;
- priority_queue<int , vector<int>, less <int> >down;
- rep(i,n)
- {
- scanf("%d",&val[i]);
- }
- int now=0;
- rep(i,m)
- {
- int type;
- scanf("%d",&type);
- while(type>now)
- {
- up.push(val[now++]);
- }
- while(!down.empty()&&down.top()>up.top())
- {
- int temp=up.top();up.pop();
- up.push(down.top());down.pop();
- down.push(temp);
- }
- printf("%d\\n",up.top());
- int temp=up.top();up.pop();down.push(temp);
- }
- }
- return 0;
- }