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\n",(f1*1.+f2)/2.);
                }
                else {
                    printf("%.1lf\n",(f1*1.+f2)/2.);
                }
            }
            else
                cout << "Wrong!" << endl;
        }
        else
        {
            printf("%d\n",*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;
}

时间: 2024-10-24 22:20:32

HackerRank "Median Updates"的相关文章

【HackerRank】Median

题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大堆存放前半部分较小的元素,最小堆存放后半部分较大的元素,并且最大堆的所有元素小于最小堆的所有元素:保持最大堆最多比最小堆多一个元素.每次插入元素的时候都先插入到最大堆,如果发现最大堆比最小堆多了两个个,那么就从最大堆里面拿出最大的放到最小堆里面:如果发现最大堆里面新插入的元素破坏了最大堆所有元素小于

【HackerRank】Find the Median(Partition找到数组中位数)

In the Quicksort challenges, you sorted an entire array. Sometimes, you just need specific information about a list of numbers, and doing a full sort would be unnecessary. Can you figure out a way to use your partition code to find the median in an a

LeetCode OJ 4. Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

leetcode笔记:Find Median from Data Stream

一. 题目描述 Median 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 value. Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 De

zoj Median (multiset)

Median Time Limit: 5 Seconds      Memory Limit: 65536 KB The median of m numbers is after sorting them in order, the middle one number of them ifm is even or the average number of the middle 2 numbers if m is odd. You have an empty number list at fir

4. Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

LeetCode-刷题 Median of Two Sorted Arrays

之前一直是写C++的,想着开始学学用JAVA写Code会有什么不同,是否会更加简洁?于是开始学着用JAVA去刷LEETCODE 习题如下: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example

Median Weight Bead_floyd

Description There are N beads which of the same shape and size, but with different weights. N is an odd number and the beads are labeled as 1, 2, ..., N. Your task is to find the bead whose weight is median (the ((N+1)/2)th among all beads). The foll

Could not find JSON in http://updates.jenkins-ci.org/update-center.json?id=default&amp;version=2.7.4

14-Sep-2016 21:43:58.241 INFO [Download metadata thread] hudson.model.AsyncPeriodicWork$1.run Finished Download metadata. 2,388 ms 14-Sep-2016 21:43:59.070 WARNING [Jenkins initialization thread] hudson.model.UpdateCenter.updateDefaultSite Upgrading