POJ 1442(treap || 优先队列)

treap套用模板即可:

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxn 30000+5
int m,n;
int a[maxn],b[maxn];
int val[maxn],ch[maxn][2],r[maxn],size[maxn],root,cnt,counts[maxn];
inline void pushup(int rt)
{
  size[rt] = size[ch[rt][0]] + size[ch[rt][1]] + counts[rt];
}
void rotate(int &x,int kind)
{
  int y = ch[x][kind^1];
  ch[x][kind^1] = ch[y][kind];
  ch[y][kind] = x;
  pushup(x);
  pushup(y);
  x = y;
}
void newnode(int &rt, int v)
{
   rt = ++ cnt;
   val[rt] = v;
   ch[rt][0] = ch[rt][1] = 0;
   size[rt] = counts[rt] = 1;
   r[rt] = rand();
}
void insert(int &rt, int v)
{
  if(rt == 0)
  {
    newnode(rt, v);
    return;
  }
  if(val[rt] == v)
  counts[rt] ++;
  else
  {
	int kind = (val[rt] < v);
	insert(ch[rt][kind], v);
	if(r[ch[rt][kind]] < r[rt])
	rotate(rt, kind^1);
  }
  pushup(rt);
}
int select(int rt,int k)
{
  if(size[ch[rt][0]] >= k) return select(ch[rt][0], k);
  if(size[ch[rt][0]] + counts[rt] >= k) return val[rt];
  return select(ch[rt][1], k - size[ch[rt][0]] - counts[rt]);
}
int main()
{
     root = 0;
	 cnt = 0;
	 while(scanf("%d%d",&n,&m) != EOF)
	 {
	 for(int i = 1;i <= n;i ++)
	 scanf("%d", &a[i]);
	 for(int j = 1;j <= m;j ++)
	 scanf("%d", &b[j]);
	 int k = 1;
	 for(int i = 1;i <= n;i ++)
	 {
	   insert(root, a[i]);
	   if(k > n) break;
	   while(b[k] == i)
	   {
         printf("%d\n",select(root, k));
		 k ++;
	   }
	 }
	 }
	 return 0;
}

也可以使用优先队列来做,维护两个优先队列~

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
#define maxn 30000 + 5
int main()
{
    int n, m;
    priority_queue<int, vector<int>, greater<int> > q1;
    priority_queue<int, vector<int>, less<int> > q2;
    int a[maxn],b[maxn];
    while(scanf("%d%d",&n, &m) != EOF)
    {
      while(!q1.empty())
      q1.pop();
      while(!q2.empty())
      q2.pop();
      for(int i = 1;i <= n;i ++)
      {
        scanf("%d", &a[i]);
      }
      for(int j = 1;j <= m;j ++)
      {
        scanf("%d", &b[j]);
      }
      int k = 1;
      for(int i = 1;i <= n;i ++)
      {
        if(!q1.empty())
        {
          if(a[i] < q1.top())
          q2.push(a[i]);
          else
          {
           q2.push(q1.top());
           q1.pop();
           q1.push(a[i]);
          }
        }
        else q2.push(a[i]);
        while(b[k] == i)
        {
          if(q2.size() == k)
          {
            printf("%d\n",q2.top());
          }
          else if(q2.size() > k)
          {
            while(q2.size() != k)
            {
              q1.push(q2.top());
              q2.pop();
            }
            printf("%d\n",q2.top());
          }
          else if(q2.size() < k)
          {
            while(q2.size() != k)
            {
              q2.push(q1.top());
              q1.pop();
            }
            printf("%d\n",q2.top());
          }
          k++;
        }
      }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-27 19:07:53

POJ 1442(treap || 优先队列)的相关文章

Black Box(POJ 1442&#183;TREAP实现)

传送门:http://poj.org/problem?id=1442 Black Box Time Limit: 1000MS   Memory Limit: 10000K       Description Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empt

POJ 1442 treap

裸treap. 只需增加一个size记录其儿子个数便可找到第k大数. #include <cstdio> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <cstdlib> #include <cmath> #include <utility> #include <vector>

poj 1442 Treap实现名次树

Treap的入门题目,每个结点多维护一个size表示以它为根的子树的结点数,然后查kth的时候一层一层向下即可. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <ctime> 6 using namespace std; 7 8 struct Node 9 { 10 Node * ch[2]; 11 in

[ACM] POJ 1442 Black Box (堆,优先队列)

Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7099   Accepted: 2888 Description Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empt

POJ 1442 Black Box treap求区间第k大

题目来源:POJ 1442 Black Box 题意:输入xi 输出前xi个数的第i大的数 思路:试了下自己的treap模版 #include <cstdio> #include <cstring> #include <cstdlib> #include <ctime> using namespace std; struct Node { Node *ch[2]; int r; int v; int s; Node(){} Node(int v): v(v)

POJ 1442 Black Box(优先队列)

题目地址:POJ 1442 这题是用了两个优先队列,其中一个是较大优先,另一个是较小优先.让较大优先的队列保持k个.每次输出较大优先队列的队头. 每次取出一个数之后,都要先进行判断,如果这个数比较大优先的队列的队头要小,就让它加入这个队列,队列头移到较小优先的队列中.然后当较大优先的数不足k个的时候,就让较小优先的队列的队头移到较大优先的队头中. 代码如下: #include <iostream> #include <cstdio> #include <string>

POJ 1442 优先队列 模板

/* poj 1442 题意:给定M个数,每次可以插入序列一个数:再给N个数,表示在插入第几个数时输出一个数, 第一次输出序列中最小的,第二次输出序列中第二小的……以此类推,直到输出N个数. 优先队列的使用: 本题思路是建立一个小顶堆p和一个大顶堆q, q保存前k个小的数,且保证p的值都比q的大, 最后输出q的顶 */ #include <iostream> #include <cstdio> #include <algorithm> #include <queu

poj 1442 Black Box (treap树入门题)

1 /**************************************************** 2 题目: Black Box(poj 1442) 3 链接: http://poj.org/problem?id=1442 4 题意: 给n个数,m个询问,对第i数个询问前Xi个数中第 5 i小的是那个数. 6 算法: treap树 7 *****************************************************/ 8 #include<iostream

连接(应该可以这么说吧)优先队列——POJ 1442

对应POJ题目:点击打开链接 Black Box Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial m