ITON - 3 论queue()

本文介绍了一种使用优先队列(queue)维护动态整数序列中位数的方法,并通过对比不同实现方式,探讨了将队列定义为全局变量的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于标题

I(Interesting)T(Test)O(Of)N(Noip)
P.S.此类文章纯属娱乐,还有WYW不要又引用我的题!!!


番外(论queue( ))

一道OJ的题:

Middle number

题目描述
有一个整数序列,我们现在有两个操作:

1.add a:意味着在序列的结尾添加一个整数 a ,形成一个长度为 n+1 的序列。
2.mid:输出当前序列的中位数。

所谓中位数,就是这个序列按升序排列后中间位置的数(如果序列的长度是偶数,那么中位数就是此序列中间两个数)

示例1:序列为 1 2 13 14 15 16,则中位数为 13。
示例2:序列为 1 3 5 7 10 11 17,则中位数为 7 。
示例3:序列为 1 1 1 2 3,则中位数为 1 。

输入格式
第一行输入一个整数 T(T≤10) ,表示测试数据的组数。
对每组数据:
第一行是一个整数 N(1≤N≤100000),表示序列长度;
接下来一行是 N 个整数,表示这个序列;
接下来一行是一个整数 M(0≤M≤10000),表示有M个操作;
接下来有 M 行,每行是由 add 或 mid 开头表示一个操作。

输出格式
对于每组的每个 mid 操作,输出此时序列中符合题意的中位数。

样例数据 1
输入  [复制]

1
6
1 2 13 14 15 16
5
add 5
add 3
mid
add 20
mid
输出

5
13


本来是一道不是特别难的专门用来给queue(优先队列)装逼的。。
然而。。

我一开始是把queue定义在while里面的。。

WTF.CPP

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
int t,n,m,d;
char c[4];

inline int read()
{
    int w=1,x=0;char ch;
    while(ch< '0'||ch >'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*w;
}

int main()
{    
    t = read();
    while(t--)
    {
        priority_queue<int >qu1;
        priority_queue<int >qu2;
        int a;
        n = read();
        for(int i=1;i<=n;i++){a = read();qu1.push(a);}
        for(int i=1;i<=n/2;i++){qu2.push(-qu1.top());qu1.pop();}
        m = read();
        for(int i=1;i<=m;i++)
        {
            cin >> c;
            if(c[0]=='a')
            {
                cin>>a;
                if(-qu2.top()<a)qu2.push(-a);
                else    qu1.push(a);
                while(qu2.size()>qu1.size()-1)qu1.push(-qu2.top()),qu2.pop();
                while(qu2.size()<qu1.size()-1)qu2.push(-qu1.top()),qu1.pop();               
            }
            else cout<<qu1.top()<<endl;         
        }
    }
}

样例啊极限数据什么的都过的是行云流水这是要 AC 的节奏啊A_A。。。
然后。。。

这里写图片描述

??????

??????

??????

这里写图片描述
然后在懵逼状态中把queue换成了全局变量。。

STD.CPP

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
int t,n,m,d;
char c[4];

inline int read()
{
    int w=1,x=0;char ch;
    while(ch< '0'||ch >'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*w;
}
priority_queue<int >qu1;
priority_queue<int >qu2;
int main()
{    
    t = read();
    while(t--)
    {
        while(!qu1.empty())qu1.pop();
        while(!qu2.empty())qu2.pop();       
        int a;
        n = read();
        for(int i=1;i<=n;i++){a = read();qu1.push(a);}
        for(int i=1;i<=n/2;i++){qu2.push(-qu1.top());qu1.pop();}
        m = read();
        for(int i=1;i<=m;i++)
        {
            cin >> c;
            if(c[0]=='a')
            {
                cin>>a;
                if(-qu2.top()<a)qu2.push(-a);
                else    qu1.push(a);
                while(qu2.size()>qu1.size()-1)qu1.push(-qu2.top()),qu2.pop();
                while(qu2.size()<qu1.size()-1)qu2.push(-qu1.top()),qu1.pop();               
            }
            else cout<<qu1.top()<<endl;         
        }
    }
}

秒过。。。。。
这里写图片描述
??????

??????

??????

这里写图片描述

别人都是把queue放在while里面随便过啊???

这里写图片描述
这里写图片描述

后来经过某大佬的讲解后。。发现是因为queue相当于一个大数组。。所以说最好不要把queue放在函数利用而是弄成全局变量再清空(虽然慢一些)。。。
至于为什么别人都没事就我挂了。。。
大佬也不知道。。。
这里写图片描述

感想

queue不要放主函数。。。。。。orz

### 解决方案 当遇到 `python3 venv` 创建虚拟环境失败并返回错误状态码 1 的情况时,通常是因为依赖项未正确安装或 Python 环境配置存在问题。以下是详细的解决方案: #### 1. 检查并修复依赖关系 如果系统缺少必要的依赖包,则可能导致 `python3-venv` 安装不完全或运行异常。可以通过重新安装相关依赖来解决问题。例如,在基于 Debian 或 Ubuntu 的系统中,可以执行以下命令以确保所需组件已安装[^3]: ```bash sudo apt-get update sudo apt-get install libopenblas-dev libblas-dev m4 cmake cython python3-dev python3-yaml python3-setuptools python3-wheel python3-pillow python3-numpy ``` #### 2. 重新安装 `python3-venv` 即使已经安装过 `python3-venv`,也可能因为某些原因导致其功能损坏。建议卸载后再重新安装该模块[^1]: ```bash sudo apt remove python3.10-venv sudo apt install python3.10-venv ``` #### 3. 使用正确的 pip 版本 确认所使用的 `pip` 是针对目标解释器的版本。特别是在 Windows 平台下创建虚拟环境时,需验证路径中的 `pip` 是否匹配指定的 Python 解释器位置[^2]。可通过如下方式更新 `pip` 和关联工具至最新版: ```bash python -m ensurepip --upgrade python -m pip install --upgrade pip setuptools wheel ``` #### 4. 手动排除冲突文件 有时旧残留数据会干扰新虚拟环境初始化过程。尝试删除可能引起冲突的相关目录再重试操作。 #### 示例脚本 下面提供一段用于自动化上述部分流程的小型脚本作为参考: ```python import subprocess def setup_venv(env_path, python_exec=&#39;python3&#39;): try: subprocess.run([python_exec, &#39;-m&#39;, &#39;ensurepip&#39;, &#39;--upgrade&#39;], check=True) subprocess.run([python_exec, &#39;-m&#39;, &#39;pip&#39;, &#39;install&#39;, &#39;--upgrade&#39;, &#39;pip&#39;, &#39;setuptools&#39;, &#39;wheel&#39;], check=True) subprocess.run([python_exec, &#39;-m&#39;, &#39;venv&#39;, env_path], check=True) print(f"Virtual environment created successfully at {env_path}.") except Exception as e: print(f"An error occurred during virtual environment creation: {e}") if __name__ == "__main__": setup_venv(&#39;my_env&#39;) ``` 通过以上措施应该能够有效缓解乃至彻底消除因 `ensurepip` 导致的退出状态非零问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值