zoukankan      html  css  js  c++  java
  • 优先队列

    C++STL——优先队列

     

    一、相关定义

    优先队列容器与队列一样,只能从队尾插入元素,从队首删除元素。但是它有一个特性,就是队列中最大的元素总是位于队首,所以出队时,并非按照先进先出的原则进行,而是将当前队列中最大的元素出队。这点类似于给队列里的元素进行了由大到小的顺序排序。元素的比较规则默认按元素值由大到小排序,可以重载“<”操作符来重新定义比较规则。

    优先级队列可以用向量(vector)或双向队列(deque)来实现(注意list container不能用来实现queue,因为list的迭代器不是任意存取iterator,而pop中用到堆排序时是要求randomaccess iterator 的!):
    priority_queue<vector<int>, less<int> > pq1;     // 使用递增less<int>函数对象排序
    priority_queue<deque<int>, greater<int> > pq2;   // 使用递减greater<int>函数对象排序
    其成员函数有“判空(empty)” 、“尺寸(Size)” 、“栈顶元素(top)” 、“压栈(push)” 、“弹栈(pop)”等。

    二、priority_queue

    基本操作:

    empty()      如果队列为空,则返回真

    pop()    删除对顶元素,删除第一个元素

    push()        加入一个元素

    size()      返回优先队列中拥有的元素个数

    top()     返回优先队列对顶元素,返回优先队列中有最高优先级的元素

    在默认的优先队列中,优先级高的先出队。在默认的int型中先出队的为较大的数。

    头文件:

    #include <queue>

    声明方式:

    1、普通方法:

    priority_queue<int> q;                 //通过操作,按照元素从大到小的顺序出队
    priority_queue<int,vector<int>, greater<int> > q;    //通过操作,按照元素从小到大的顺序出队

    2、自定义优先级:

    struct cmp {     
      operator bool ()(int x, int y)     
      {        
         return x > y;   // x小的优先级高       //也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高
      }
    };
    priority_queue<int, vector<int>, cmp> q;    //定义方法
    //其中,第二个参数为容器类型。第三个参数为比较函数。

    3、结构体声明方式:

    struct node {     
      int x, y;     
      friend bool operator < (node a, node b)     
      {         
        return a.x > b.x;    //结构体中,x小的优先级高     
      }
    };
    priority_queue<node>q;   //定义方法
    //在该结构中,y为值, x为优先级。
    //通过自定义operator<操作符来比较元素中的优先级。
    //在重载”<”时,最好不要重载”>”,可能会发生编译错误
     
    题:
    6 2
    10 3 12 15 12 18
    6 1
    10 3 12 15 12 18
    5 5
    1 2 3 4 5
    Sample Output
    37 25 15
    题意:
    有6个点,分别是第二行的六个数,代表房间的价格;你每k(第一个样例中的2)就会有一天可以免费,问怎么样能使花的钱最少,必须依次住,不能跳着住。
     
    思路1:
    第一个样例:
    前两天一定要选,所以可以删除一个,从第k+1天到能删第二个数中,找一个最大的数放在队列(不一定删除,因为可能后面有比他大的数,因为队列空所以不用替换),之后每一个i%(k+1)!=0的数,都要和队列中最小的数比较,如果这个数大于队列中的最小的,就替换掉(其实第一次也一样,只不过第一次的时候队列为空),如果i%(k+1)==0,就先把他放进队列,然后就进入上面的情况中。
    代码如下:
    #include <iostream>
    #include<algorithm>
    #include<math.h>
    #include<cstring>
    #include<cstdlib>
    #include<queue>
    using namespace std;
    typedef long long ll;
    int a[100010];
    int main()
    {
        ll n,k,sum=0;;
        priority_queue<int,vector<int>,greater<int> >q;
        while(~scanf("%lld",&n))
        {
            sum=0;
            scanf("%lld",&k);
            for(int i=1; i<=n; i++)
            {
                 scanf("%d",&a[i]);
                 sum+=a[i];
            }
    
            for(int i=k+1;i<=n;i++)
            {
                if(i%(k+1)!=0)
                {
                    if(q.top()<a[i])
                    {
                        q.pop();
                        q.push(a[i]);
                    }
                }
                else 
                {
                    q.push(a[i]);
                }
            }
            while(q.size()!=0)
            {
                sum-=q.top();
                q.pop();
            }
            printf("%lld
    ",sum);
    
        }
        return 0;
    }
    思路2:
    这个思路差不多,从后面开始入队列,如果i%(k+1)==0,找队列中的最大的删掉。
    #include <iostream>
    #include<algorithm>
    #include<math.h>
    #include<cstring>
    #include<cstdlib>
    #include<queue>
    using namespace std;
    typedef long long ll;
    int a[100010];
    int main()
    {
        ll n,k,sum=0;;
        priority_queue<int,vector<int>,less<int> >q;
        while(~scanf("%lld",&n))
        {
            sum=0;
             scanf("%lld",&k);
             for(int i=1;i<=n;i++)
                scanf("%d",&a[i]);
            while(q.size()!=0)
            {
                q.pop();
            }
             for(int i=n;i>=1;i--)
             {
                 q.push(a[i]);
                 if(i%(k+1)==0)
                 {
                    q.pop();
                 }
             }
             while(q.size()!=0)
             {
                 sum+=q.top();
                 q.pop();
             }
             printf("%lld
    ",sum);
        }
        return 0;
    }
  • 相关阅读:
    PAT甲级——A1091 Acute Stroke【30】
    PAT甲级——A1090 Highest Price in Supply Chain
    PAT甲级——A1089 Insert or Merge
    PAT甲级——A1088 Rational Arithmetic
    PAT甲级——A1087 All Roads Lead to Rome【30】
    【php中的curl】php中curl的详细解说
    【php中的curl】使用curl完成POST数据给飞信接口
    【php中的curl】php中curl的使用
    【socket】php实现socket
    【socket】用PHP的socket实现客户端到服务端的通信
  • 原文地址:https://www.cnblogs.com/bhd123/p/9885898.html
Copyright © 2011-2022 走看看