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