<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p>

<ul>
	<li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li>
	<li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li>
</ul>

<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p>

<p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000]
<strong>Explanation:</strong> 
Window position                Median
---------------                -----
[<strong>1  3  -1</strong>] -3  5  3  6  7        1
 1 [<strong>3  -1  -3</strong>] 5  3  6  7       -1
 1  3 [<strong>-1  -3  5</strong>] 3  6  7       -1
 1  3  -1 [<strong>-3  5  3</strong>] 6  7        3
 1  3  -1  -3 [<strong>5  3  6</strong>] 7        5
 1  3  -1  -3  5 [<strong>3  6  7</strong>]       6
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3
<strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

class Solution {
public:
    vector<double> medianSlidingWindow(vector<int>& nums, int k) {
        if (k == 1)
        {
            vector<double> res;
            for (auto it : nums)
            {
                res.push_back(it * 1.0);
            }

            return res;
        }
        vector<int> arr;
        for (int i = 0; i < k; i++)
        {
            arr.push_back(nums[i]);
        }

        sort(arr.begin(), arr.end());

        multiset<int> s1;
        multiset<int> s2;
        for (int i = 0; i < k/2; i++)
        {
            s1.insert(arr[i]);
        }

        for (int i = k/2; i < k; i++)
        {
            s2.insert(arr[i]);
        }

        vector<double> res;

        if (k % 2)
        {
            auto it = s2.begin();
            res.push_back(*it);
        }
        else
        {
            auto it1 = s1.rbegin();
            auto it2 = s2.begin();
            res.push_back(((long) ((long) *it1 + (long) *it2)) / 2.0);
        }

        int n = nums.size();
        for (int i = k; i < n; i++)
        {
            auto it1 = s1.rbegin();
            auto it2 = s2.begin();

            int v1 = *it1;
            int v2 = *it2;

            bool delete1 = false;

            if (nums[i - k] <= v1)
            {
                int count = s1.count(nums[i-k]);
                s1.erase(nums[i - k]);
                for (int j = 0; j < count-1; j++)
                {
                    s1.insert(nums[i - k]);
                }
                delete1 = true;
            }
            else
            {
                int count = s2.count(nums[i-k]);
                s2.erase(nums[i - k]);
                for (int j = 0; j < count-1; j++)
                {
                    s2.insert(nums[i - k]);
                }
            }

            bool insert1 = false;
            if (nums[i] <= v1)
            {
                s1.insert(nums[i]);
                insert1 = true;
            }
            else
            {
                s2.insert(nums[i]);
            }

            if (delete1)
            {
                if (insert1)
                {
                    // do nothing
                }
                else
                {
                    auto it = s2.begin();
                    int val = *it;
                    int count = s2.count(val);
                    s2.erase(val);
                    s1.insert(val);
                    for (int j = 0; j < count-1; j++)
                    {
                        s2.insert(val);
                    }
                }
            }
            else
            {
                if (insert1)
                {
                     auto it = s1.rbegin();
                     int val = *it;
                     int count = s1.count(val);
                     s1.erase(val);
                     s2.insert(val);
                     for (int j = 0; j < count-1; j++)
                     {
                        s1.insert(val);
                     }
                }
                else
                {
                    //do nothing
                }
            }

            if (k % 2)
            {
                auto it = s2.begin();
                res.push_back(*it);
            }
            else
            {
                auto it1 = s1.rbegin();
                auto it2 = s2.begin();
                res.push_back(((long) ((long) *it1 + (long) *it2)) / 2.0);
            }
        }

        return res;
    }
};
48890