• STL 优先队列


    题目来源:ACM-ICPC 2015 Changchun Preliminary Contest      

    题意:    巴拉巴拉

    思路:    优先队列内库的直接使用              (优先队列又名单调队列)

    优先队列:   是一种队列, 只不过他的储存规则存在自动排序等操作,里面可以存入结构体,可以按照结构体的某个元素进行排序。(即需要自己设置元素优先级)

    struct s{
        string str;
        int val,id;
        friend bool operator < (s a, s b)
        {
            if (a.val!=b.val)
                return a.val<b.val;   //先按val  从大到小排序 
            return a.id>b.id;          //    如果val相等,则按id从小到排序   
        }
    }mp[MAX];

    内库基本操作  增删改查    (按优先级进行排序)

    empty()  //  如果队列为空,则返回真
    
    pop()   // 删除对顶元素,删除第一个元素
    
    push()   //  加入一个元素
    
    size()   // 返回优先队列中拥有的元素个数
    
    top()   // 返回优先队列对顶元素,返回优先队列中有最高优先级的元素

    AC代码:

    #include<iostream>
    #include<cstdio>
    #include<ctime>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<map> 
    #include<algorithm>
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    #define Mem0(x) memset(x,0,sizeof(x))
    #define Mem1(x) memset(x,-1,sizeof(x))
    #define MemX(x) memset(x,0x3f,sizeof(x))
    #include<functional>
    using namespace std;
    typedef long long ll;
    const int inf=0x3f3f3f;
    const double pi=acos(-1.0);
    
    int T,k,m,qq;
    const int MAX=250010;
    struct s{
        string str;
        int val,id;
        friend bool operator < (s a, s b)
        {
            if (a.val!=b.val)
                return a.val<b.val;  
            return a.id>b.id;          
        }
    }mp[MAX];
    
    struct ss{
        int x,y;
    }door[MAX];
    
    bool cmp (ss a,ss b)
    {
        return a.x<b.x;
    }
    priority_queue <s> q;
    
    string ans[MAX];
    
    int main()
    {
        cin>>T;
        while(T--){
            while (!q.empty())
                q.pop();
            cin>>k>>m>>qq;
            for (int i=1;i<=k;i++){
                cin>>mp[i].str>>mp[i].val;
                mp[i].id=i;
            }
            for (int i=1;i<=m;i++){
                cin>>door[i].x>>door[i].y;
            }
            sort(door+1,door+m+1,cmp);
            
            int now=1,anss=0,tmp;
            for (int res=1;res<=k;res++){
                q.push(mp[res]);
                while (res==door[now].x&&now<=m){
                    for (int j=1;j<=door[now].y&&!q.empty();j++){
                        ans[++anss]=q.top().str;
                        q.pop();
                    }
                    ++now;
                }
            }
            while (!q.empty()){
                ans[++anss]=q.top().str;
                q.pop();
            }
            for (int i=0;i<qq;i++){
                int x;
                cin>>x;
                if (i==0)
                    cout<<ans[x];
                else
                    cout<<" "<<ans[x];
            }
            cout<<endl;
        }
        return 0;
    }
        
  • 相关阅读:
    asp.net页面生命周期追踪
    asp.net Forums 之配置,缓存,多数据访问
    沪江技术部程序员招聘试题,大家一起讨论一下。
    httpd does not appear to be running and proxying cobbler, or SELinux is in the way.
    网络知识OSI七层网络与TCP/IP五层网络架构及二层/三层网络
    python中用psutil模块,yagmail模块监控CPU、硬盘、内存使用,阈值后发送邮件
    Linux中访问Apache报403错误处理方法
    centos7的启动流程
    pycharm介绍
    监测NGINX服务的shell脚本
  • 原文地址:https://www.cnblogs.com/q1204675546/p/11250516.html
Copyright © 2020-2023  润新知