zoukankan      html  css  js  c++  java
  • POJ 1442 Black Box(优先队列)

    题目地址:POJ 1442

    这题是用了两个优先队列,当中一个是较大优先。还有一个是较小优先。

    让较大优先的队列保持k个。每次输出较大优先队列的队头。

    每次取出一个数之后,都要先进行推断,假设这个数比較大优先的队列的队头要小,就让它增加这个队列。队列头移到较小优先的队列中。然后当较大优先的数不足k个的时候,就让较小优先的队列的队头移到较大优先的队头中。

    代码例如以下。

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <stdlib.h>
    #include <math.h>
    #include <ctype.h>
    #include <queue>
    #include <map>
    #include <set>
    #include <algorithm>
    
    using namespace std;
    int a[40000], b[40000];
    struct xiao
    {
        int x;
        bool operator < (const xiao &a) const
        {
            return x > a.x;
        }
    };
    struct da
    {
        int x;
        bool operator < (const da &a) const
        {
            return x<a.x;
        }
    };
    int main()
    {
        int n, m, i, j, cnt, x, k;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            priority_queue<xiao>q1;
            priority_queue<da>q2;
            xiao f1;
            da f2;
            for(i=0; i<n; i++)
            {
                scanf("%d",&a[i]);
            }
            for(i=0; i<m; i++)
            {
                scanf("%d",&b[i]);
            }
            j=0;
            for(i=0; i<m; i++)
            {
                while(j<b[i])
                {
                    if(q2.empty()||a[j]>=q2.top().x)
                    {
                        f1.x=a[j];
                        q1.push(f1);
                    }
                    else
                    {
                        f1.x=q2.top().x;
                        q1.push(f1);
                        q2.pop();
                        f2.x=a[j];
                        q2.push(f2);
                    }
                    j++;
                }
                while(q2.size()<=i)
                {
                    f1=q1.top();
                    f2.x=f1.x;
                    q1.pop();
                    q2.push(f2);
                }
                printf("%d
    ",q2.top());
            }
        }
        return 0;
    }
    


  • 相关阅读:
    算法笔记 --- Selection Sort
    算法笔记 --- Radix Sort
    算法笔记 --- Quick Sort
    算法笔记 --- Merge Sort
    算法笔记 --- Insertion Sort
    算法笔记 --- Heap Sort
    算法笔记 --- Counting Sort
    算法笔记 --- Bubble Sort
    算法笔记 --- Tree Travers
    javaweb_JDBC
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7094837.html
Copyright © 2011-2022 走看看