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 ;
}

最新文章

  1. Git 命令速查图
  2. iOS高效调试
  3. centos6.6编译安装lnmp系列之PHP
  4. python中字符串连接的三种方式
  5. asp.net php asp jsp 301重定向的代码
  6. 【 .NET 面向对象程序设计进阶》】【 《.NET 面向对象编程基础》】【《正则表达式助手》】
  7. canvas 3D雪花效果
  8. Linear Regression with Scikit Learn
  9. Ubuntu16设置Redis开机自启动
  10. 新建一个express项目的流程
  11. 阿里云Ubuntu 18.04安装图形界面
  12. 从零开始学spring cloud(五) -------- 将服务注册到Eureka上
  13. day 7-21 pymysql模块
  14. 第十二节,TensorFlow读取数据的几种方法以及队列的使用
  15. 线上问题定位--OOM
  16. 冲刺Two之站立会议5
  17. Let it crash philosophy for distributed systems
  18. python mock的简单使用
  19. comboBox绑定字典Dictionary 获取value中的值
  20. 问题:Oracle出发器;结果:1、Oracle触发器详解,2、Oracle触发器示例

热门文章

  1. php二分式查找
  2. java正则表达式 非捕获组详解
  3. 转ORA-28002: the password will expire within 7 days 解决方法
  4. JavaScript学习记录总结(七)——dom对象应用之用户简单管理
  5. JavaScript学习记录总结(四)——js函数的特殊性
  6. URAL-1989 Subpalindromes(单点更新+hash)
  7. poj1984 带权并查集
  8. 回调函数的实现 &amp; 结构体的继承
  9. [hdu 3605]Escape
  10. 26 个 jQuery使用技巧