C++ TreeMap+Queue
\nclass MKAverage {\npublic:\n    map<int,int>record;\n    long long total;\n    queue<int>q;\n    int m;\n    int k;\n    MKAverage(int m, int k) {\n        th
```\nclass MKAverage {\npublic:\n    map<int,int>record;\n    long long total;\n    queue<int>q;\n    int m;\n    int k;\n    MKAverage(int m, int k) {\n        this->m=m;\n        this->k=k;\n        total=0;\n    }\n    \n    void addElement(int num) {\n        q.push(num);\n        total+=(long long)num;\n        record[num]++;\n        if(q.size()>m)\n        {\n            int nums=q.front();\n            q.pop();\n            total-=nums;\n            record[nums]--;\n            if(record[nums]==0)record.erase(nums);\n        }\n        return;\n    }\n    \n    int calculateMKAverage() {\n        if(q.size()<m)return -1;\n        long long sum=total;\n        cout << sum << endl;\n        int count=k;\n        for(auto &x:record)\n        {\n            int nums=x.first;\n            int times=x.second;\n            if(times<=count)\n            {\n                count-=times;\n                sum-=(long long)nums*times;\n            }else\n            {\n                sum-=(long long)nums*count;\n                count=0;\n            }\n            if(count==0)break;\n        }\n        count=k;\n        for(auto x=record.rbegin();x!=record.rend();x++)\n        {\n            int nums=x->first;\n            int times=x->second;\n            if(x->second<=count)\n            {\n                count-=x->second;\n                sum-=(long long)nums*x->second;\n            }else\n            {\n                sum-=(long long)nums*count;\n                count=0;\n            }\n            if(count==0)break;\n        }\n        return sum/(long long)(m-k*2);\n    }\n};\n\n/**\n * Your MKAverage object will be instantiated and called as such:\n * MKAverage* obj = new MKAverage(m, k);\n * obj->addElement(num);\n * int param_2 = obj->calculateMKAverage();\n */\n```
['Tree', 'Queue']