• 华为OD机考:固定窗口的最大和、简易内存池的分配


    0032求固定窗口的最大和

    题目描述

    有一个N个整数的数组 和一个长度为M的窗口 窗口从数组内的第一个数开始滑动 直到窗口不能滑动为止 每次滑动产生一个窗口 和窗口内所有数的和 求窗口滑动产生的所有窗口和的最大值

    输入描述

    第一行输入一个正整数N 表示整数个数 0

    输出描述

    窗口滑动产生所有窗口和的最大值

    样例

    输入
    6
    12 10 20 30 15 23
    3
    
    输出
    68
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    c++ 代码

    //
    // Created by HANWENKE on 2022-09-19.
    //
    #include 
    #include 
    #include 
    #include 
    using namespace  std;
    int main()
    {
        int n,m;
        cin>>n;
        vector<int>arr(n);
        string s;
        getchar();
        getline(cin,s);
        istringstream stemp(s);
        int temp;
        int i=0;
        while(stemp>>temp){
            arr[i++]=temp;
        }
    /*    for(auto &x:arr){
            cout<
        cin>>m;
        /*左右双指针大法*/
        int left=0,right=0;
        int mmax=INT32_MIN;
        int sum=0,k=0;
        while(right<n){
            //每次获取窗口大小当中的和
            sum+=arr[right];
            k++;
            if(k==m){
                //记录和的最大值
                mmax=max(sum,mmax);
                //将窗口最左边的值减去
                sum-=arr[left];
                //同时将窗口的元素个数减一
                k--;
                //将窗口向右边移动一个位置
                left++;
            }
            right++;
        }
        cout<<mmax;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    0033-简易内存池的分配

    题目描述

    有一个简易内存池,内存按照大小粒度分类

    每个粒度有若干个可用内存资源用户会进行一系列内存申请 需要按需分配内存池中的资源返回申请结果成功失败列表

    分配规则如下

    1.分配的内存要大于等于内存的申请量存在满足需求的内存就必须分配 优先分配粒度小的,但内存不能拆分使用

    2.需要按申请顺序分配 先申请的先分配,有可用内存分配则申请结果为true 没有可用则返回false

    注释:不考虑内存释放

    输入描述

    输入为两行字符串

    第一行为内存池资源列表

    包含内存粒度数据信息,粒度数据间用逗号分割

    一个粒度信息内用冒号分割

    冒号前为内存粒度大小,冒号后为数量

    资源列表不大于1024 每个粒度的数量不大于4096

    第二行为申请列表 申请的内存大小间用逗号分割,申请列表不大于100000

    64:2,128:1,32:4,1:128

    50,36,64,128,127

    输出描述

    输出为内存池分配结果

    如true,true,true,false,false

    输入样例

    64:2,128:1,32:4,1:128
    50,36,64,128,127
    
    • 1
    • 2

    输出样例

    true,true,true,false,false
    
    • 1

    C++ 代码

    //
    // Created by HANWENKE on 2022-09-19.
    //
    #include 
    //先申请先分配--队列-先进先出
    #include 
    #include 
    #include 
    using namespace  std;
    int main(){
        string ss;
        map<int ,int>tmap;
        vector<pair<string,string>>arr;
        getline(cin,ss);
        int k=0;
        {//这段代码可以简化--直接将字符串转化成整数放到map中--有点懒-不改了
            while (k < ss.length()) {
                string s1;
                while (ss[k] != ',' && k < ss.length() && ss[k] != ':') {
                    s1 += ss[k++];
                }
                k++;
                string s2;
                while (ss[k] != ',' && k < ss.length() && ss[k] != ':') {
                    s2 += ss[k++];
                }
                k++;
                pair<string, string> stemp(s1, s2);
                arr.push_back(stemp);
            }
            for (int i = 0; i < arr.size(); i++) {
                int temp1 = stoi(arr[i].first);
                int temp2 = stoi(arr[i].second);
                tmap[temp1] = temp2;
            }
        }
        //重置cin输入状体
        cin.clear();
        //清除cin缓冲区未读取信息
        cin.sync();
        ss.clear();
        getline(cin,ss);
        k=0;
        queue<int>q;
        while(k<ss.length()){
            string stemp;
            while(k<ss.length()&&ss[k]!=','){
                stemp+=ss[k++];
            }
            k++;
            int temp=stoi(stemp, nullptr,10);
            q.push(temp);
        }
        //保持最后的结果
        vector<string>res;
        while(!q.empty()){
            int temp=q.front();
            q.pop();
           auto x= tmap.lower_bound(temp);
           if(x!=tmap.end()&&x->second>0){
               //当一个内存粒度的数量为0的时候,删除这个粒度
               res.push_back("true");
               x->second--;
               if(x->second==0){
                   tmap.erase(x);
               }
           }else{
               res.push_back("false");
           }
        }
    
        for(int i=0;i<res.size()-1;++i){
            cout<<res[i]<<",";
        }
        cout<<res[res.size()-1];
        return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
  • 相关阅读:
    【牛客刷题】链表的奇偶重排
    【PyQT5教程】-02-UI组件
    PostgreSQL数据库统计信息——compute_scalar_stats计算统计数据
    Map和Set的详解
    Vagrant + VirtualBox + CentOS7 + WindTerm 5分钟搭建本地linux开发环境
    css 左右滚轮无缝衔接
    罗丹明 110,CAS号:13558-31-1
    一种改进盲解卷积算法在旋转机械故障诊断中的应用(MATLAB)
    【结构型】桥接模式(Bridge)
    Artstudio Pro Mac(绘图和图片编辑)
  • 原文地址:https://blog.csdn.net/weixin_41694574/article/details/126930595