• 对顶栈


    问题 A: K优先队列

    时间限制: 1 Sec  内存限制: 128 MB
    [提交] [状态]

    题目描述

    你需要维护一个队列,支持以下两种操作:
    1.加入一个非负整数x;
    2.取出当前队列中第k大的数字。
    保证进行第二种操作时,队列中至少有k个数字。
    部分数据经过加密,你需要依次处理每个操作才能获得正确的下一个操作。

    输入

    第一行包括三个非负整数n,k,p,分别表示操作次数,参数k以及数据是否进行过加密。
    接下来n行,每行先给出一个数opt,表示操作类型。若opt=1,接下来还会有一个非负整数x,若p=0,表示往队列中加入x,若p=1,表示往队列中加入x异或上前一次出队操作取出的数字后得到的结果,如果还未进行过出队操作,把前一次取出的数字看作0。若opt=2,表示要求取出并输出当前队列中第k大的数字。

    输出

    对于每一个出队操作,输出一个正整数表示答案。

    样例输入 Copy

    5 2 1
    1 2
    1 3
    2
    1 3
    2
    

    样例输出 Copy

    2
    1
    

    提示

    对于100%的数据,1≤k≤n≤2∗105,0≤x≤109
    本来输出的是第一个,现在把他改为输出第二个就对了,不知道为啥
    #include<iostream>
    #include<algorithm>
    #include<map>
    #include<string> 
    #include <math.h> 
    #include<memory.h>
    #include<cstring>
    #include<bits/stdc++.h> 
    using namespace std;
    typedef long long ll; 
    inline int read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    const int INF=0x3f3f3f3f;
    const int maxn=5e5+10;
    priority_queue<int> q1; //大根堆大到小 
    priority_queue<int, vector<int>, greater<int>> q2; //小根堆小到大 
    int n,k,p;
    //void  insert11(int x){
    //    if(!q2.size()||x>q2.top()){
    //        q2.push(x);
    //    }
    //    else{
    //        q1.push(x);
    //    }
    //    if(q2.size()>=k){
    //        q1.push(q2.top());
    //        q2.pop();
    //    }
    //    while(q2.size()<k&&q1.size()!=0){
    //        q2.push(q1.top());
    //        q1.pop();
    //    }
    //}
    void insert11(int x){
        q2.push(x);
        if(q2.size()==k){
            q1.push(q2.top());
            q2.pop(); 
        } 
    }
    int main(){
        cin>>n>>k>>p;
        int z=0;
        int x,y;
        while(n--){
            scanf("%d",&x); 
            if(x==1){
                scanf("%d",&y);
                if(p==1){
                    y=y^z;
                }
                insert11(y);    
            }
            else if(x==2){
                z=q1.top();
                printf("%d
    ",z);
                q1.pop();
            }
        }
    }
    #include <map>
    #include <queue>
    #include <string>
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include <algorithm>
    #include <math.h>
    typedef long long ll;
    using namespace std;
    const int maxn=2e5+1010;
    #define inf 0x3f3f3f3f
    #define sf scanf
    #define pf printf
    const int mod=998244353;
    const int MOD=10007;
    
    inline int read() {
        int x=0;
        bool t=false;
        char ch=getchar();
        while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
        if(ch=='-')t=true,ch=getchar();
        while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
        return t?-x:x;
    }
    
    priority_queue<ll , vector<ll> , greater<ll> > mn;//上  小根堆         小到大
    priority_queue<ll , vector<ll> , less<ll> > mx;//下       大根堆      大到小
    //map<ll,ll>mp;
    
    ll n,m,t,l,r,p;
    ll sum,ans,res,cnt,flag;
    
    int main() {
        sf("%lld%lld%lld",&n,&m,&p);
        if(p==0) {
            while(n--) {
                ll opt,x;
                sf("%lld",&opt);
                if(opt==1){
                    sf("%lld",&x);
                    mn.push(x);
                    if(mn.size()==m){
                        mx.push(mn.top());
                        mn.pop();
                    }
                }else {
                    printf("%lld
    ",mx.top());
                    mx.pop();
                }
            }
        } 
        
        
        
        else {
            ll y=0;
            while(n--) {
                ll opt,x;
                sf("%lld",&opt);
                if(opt==1){
                    sf("%lld",&x);
                    x=x^y;
                //    cout<<"    "<<x<<endl;
                    mn.push(x);
                    if(mn.size()==m){
                        mx.push(mn.top());
                        mn.pop();
                    }
                }else {
                    y=mx.top();
                    printf("%lld
    ",mx.top());
                    //cout<<mx.top()<<endl;
                    mx.pop();
                }
            }
    
        }
        return 0;
    }
  • 相关阅读:
    linux截图工具
    Git理论知识补充
    Git基本操作(add,commit的理解)
    VS2017 error CS0234: 命名空间“Microsoft”中不存在类型或命名空间名“Office”问题的一种解决方案
    MFC CFileDialog DoModal()无法弹出窗口,直接返回IDCANCEL
    VS2015 、VS2017 MFC输出日志到控制台窗口
    win10 VMware 关闭虚拟机失败导致再打开时显示连接不上虚拟机的一种解决方法
    c语言之位段
    Adobe Acrobat DC 制作多级书签
    MFC基于对画框工程笔记->更改窗口图标以及生成的.exe图标
  • 原文地址:https://www.cnblogs.com/lipu123/p/12926789.html
Copyright © 2020-2023  润新知