• HackerRank "Median Updates"


    Same as LintCode "Sliding Window Median", but requires more care on details - no trailing zeroes.

    #include <map>
    #include <set>
    #include <list>
    #include <cmath>
    #include <ctime>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <bitset>
    #include <cstdio>
    #include <limits>
    #include <vector>
    #include <cstdlib>
    #include <numeric>
    #include <sstream>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    /* Head ends here */
    multiset<int> lmax, rmin;
    void removeOnly1(multiset<int> &ms, int v)
    {
        auto pr = ms.equal_range(v);
        ms.erase(pr.first);
    }
    
    void remove(multiset<int> &lmax, multiset<int> &rmin, int v)
    {
        if(v <= *lmax.rbegin())
        {
            removeOnly1(lmax, v);
            if(lmax.size() < rmin.size())
            {
                int tmp = *rmin.begin();
                lmax.insert(tmp);
                removeOnly1(rmin, tmp);
            }
        }
        else if(v >= *rmin.begin())
        {
            removeOnly1(rmin, v);
            if((lmax.size() - rmin.size()) > 1)
            {
                int tmp = *lmax.rbegin();
                removeOnly1(lmax, tmp);
                rmin.insert(tmp);
            }
        }
    }
    
    void addin(multiset<int> &lmax, multiset<int> &rmin, int v)
    {
        if(lmax.empty())
        {
            lmax.insert(v);
            return;
        }
        int lmax_v = *lmax.rbegin();
        int size_l = lmax.size(), size_r = rmin.size();
        if(v <= lmax_v) // to add left
        {
            lmax.insert(v);
            if((size_l + 1 - size_r) > 1)
            {
                int tmp = *lmax.rbegin();
                rmin.insert(tmp);
                removeOnly1(lmax, tmp);
            }
        }
        else
        {
            rmin.insert(v);
            if((size_r  + 1)> size_l)
            {
                int tmp = *rmin.begin();
                removeOnly1(rmin, tmp);
                lmax.insert(tmp);
            }
        }
    }
    
    void median(vector<char> s,vector<int> X) {
        int n = s.size();
        multiset<int> ms;
        for(int i = 0; i < n; i ++)
        {
            if(s[i] == 'r')
            {
                if(!lmax.count(X[i]) && !rmin.count(X[i]))
                {
                    cout << "Wrong!" << endl;                
                    continue;
                }
                else
                {
                    remove(lmax, rmin, X[i]);                
                }
            }
            else
            {
                addin(lmax, rmin, X[i]);            
            }
            if(lmax.size() == rmin.size())
            {
                if(lmax.size() >0)
                {
                    long long f1 = (long long)(*lmax.rbegin());
                    long long f2 = (long long)(*rmin.begin());                
                    if ((f1 + f2) % 2 == 0) {
                        printf("%.0lf
    ",(f1*1.+f2)/2.);
                    }
                    else {
                        printf("%.1lf
    ",(f1*1.+f2)/2.);
                    }
                }
                else
                    cout << "Wrong!" << endl;
            }
            else
            {            
                printf("%d
    ",*lmax.rbegin());
            }
                
        }
        
    }
    int main(void){
    
    //Helpers for input and output
    
       int N;
       cin >> N;
       
       vector<char> s;
        vector<int> X;
       char temp;
        int tempint;
       for(int i = 0; i < N; i++){
          cin >> temp >> tempint;
            s.push_back(temp);
            X.push_back(tempint);
       }
       
       median(s,X);
       return 0;
    }
    View Code
  • 相关阅读:
    QT 信号与槽 中传递自定义数据类型
    keil5 仿真时发现函数不能进入到main函数
    QT 报错分析 一
    QT 简易时间处理
    QT 编译出现 Custom Executable 对话框 问题
    win10 下装ubuntu双系统
    Vmware linux下不能联网
    Vmware Workstation
    免费在线作图,实时协作
    SVN常用问题汇总
  • 原文地址:https://www.cnblogs.com/tonix/p/4983719.html
Copyright © 2020-2023  润新知