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()) > )
{
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 + - size_r) > )
{
int tmp = *lmax.rbegin();
rmin.insert(tmp);
removeOnly1(lmax, tmp);
}
}
else
{
rmin.insert(v);
if((size_r + )> 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 = ; 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() >)
{
long long f1 = (long long)(*lmax.rbegin());
long long f2 = (long long)(*rmin.begin());
if ((f1 + f2) % == ) {
printf("%.0lf\n",(f1*.+f2)/.);
}
else {
printf("%.1lf\n",(f1*.+f2)/.);
}
}
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 = ; i < N; i++){
cin >> temp >> tempint;
s.push_back(temp);
X.push_back(tempint);
} median(s,X);
return ;
}

HackerRank "Median Updates"的更多相关文章

  1. 【HackerRank】Median

    题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...

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

    In the Quicksort challenges, you sorted an entire array. Sometimes, you just need specific informati ...

  3. Failure to find xxx in xxx was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced @ xxx

    问题: 在linux服务器上使用maven编译war时报错: 16:41:35 [FATAL] Non-resolvable parent POM for ***: Failure to find * ...

  4. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  5. [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 ...

  6. [LeetCode] 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 ...

  7. Applying vector median filter on RGB image based on matlab

    前言: 最近想看看矢量中值滤波(Vector median filter, VMF)在GRB图像上的滤波效果,意外的是找了一大圈却发现网上没有现成的code,所以通过matab亲自实现了一个,需要学习 ...

  8. 【leetcode】Median of Two Sorted Arrays

    题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...

  9. Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing

    B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. T ...

随机推荐

  1. Spark读取HDFS文件,文件格式为GB2312,转换为UTF-8

    package iie.udps.example.operator.spark; import scala.Tuple2; import org.apache.hadoop.conf.Configur ...

  2. 扩展KVM镜像的虚拟磁盘大小

    当我们需要扩展模板镜像的虚拟磁盘大小时,比如原来的虚拟磁盘大小为20G,现在我们想将其扩展到30G,那么我们可以根据如下步骤来操作. 整个流程可以分为三个阶段: 1.扩展KVM镜像磁盘文件大小到30G ...

  3. 三步搞定ISO/GHO安装系统 - imsoft.cnblogs

    高清互动安装系统附件:重装系统视频教程.7z

  4. Codeforces Round #375 (Div. 2) A B C 水 模拟 贪心

    A. The New Year: Meeting Friends time limit per test 1 second memory limit per test 256 megabytes in ...

  5. smartUpload组件批量下载

    public class BatchDownloadServlet extends HttpServlet { public void doGet(HttpServletRequest request ...

  6. html部分---样式表,选择器;

    <1.内联样式,优点:控制精确,缺点:代码重用性差,页面代码乱.> <div style="background-color:#0F0"></div& ...

  7. hihoCoder #1078 : 线段树的区间修改

    题目大意及分析: 线段树成段更新裸题. 代码如下: # include<iostream> # include<cstdio> # include<cstring> ...

  8. VS2013远程调试功能

    我跪了,我真的跪了. 实际证明这是不可行的. [开发机] Win10,64bits,VS2013 [目标机] Winxp VS2013的debug tool不能在xp上安装运行,卒! 如果有win7, ...

  9. Android Studio + gradle多渠道打包

    通过工具栏的Build->Build Apk 好像只能打包第一个Module(eclipse里面是Project的概念),怎么多渠道打包呢?目前好像只能一个一个的打 首先在清单文件里设置个变量: ...

  10. tomcat集群待整理

    对于tomcat的集群有两种方式,这个主要是针对 session而言的.一种就是sticky模式,即黏性会话模式:另外一种就是session复制模式了.所谓sticky模式就是说同一个用户的访问 请求 ...