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()) > )
{
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"的更多相关文章
- 【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 informati ...
- 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 * ...
- 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 ...
- [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 ...
- [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 ...
- Applying vector median filter on RGB image based on matlab
前言: 最近想看看矢量中值滤波(Vector median filter, VMF)在GRB图像上的滤波效果,意外的是找了一大圈却发现网上没有现成的code,所以通过matab亲自实现了一个,需要学习 ...
- 【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 ...
- 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 ...
随机推荐
- js实现简单易用的上下无缝滚动效果
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 简单的将内容加入到drupal的主页面
首先要管理员用户 然后进入结构目录 进入菜单项 在main行 选择 列出list 选择添加链接 完善信息 保存即可 eg: http://peach.fafu.edu.cn/ 将papaya的jbro ...
- haar-like特征(转载)
浅析人脸检测之Haar分类器方法 [补充] 这是我时隔差不多两年后, 回来编辑这篇文章加的这段补充, 说实话看到这么多评论很是惊讶, 有很多评论不是我不想回复, 真的是时间久了, 很多细节我都忘记了 ...
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) J dp 背包
J. Bottles time limit per test 2 seconds memory limit per test 512 megabytes input standard input ou ...
- java的加减乘除
//编写一个程序,用户输入两个数,求出其加减乘除,并用消息框显示计算结果.//MengYao,2015,10,6 import javax.swing.JOptionPane;public class ...
- Mybatis学习 —— 包括所有 mybatis官网
http://www.mybatis.org/mybatis-3/zh/configuration.html#typeAliases
- hdu3033 I love sneakers! 分组背包变形
分组背包要求每一组里面只能选一个,这个题目要求每一组里面至少选一个物品. dp[i, j] 表示前 i 组里面在每组至少放进一个物品的情况下,当花费 j 的时候,所得到的的最大价值.这个状态可以由三个 ...
- Android多线程入门学习
(1)进程间通信交换信息的一种方式--使用handler: (2)在主线程中new一个Handler对象,并重写他的handlerMessage(Message msg)方法: (3)Message中 ...
- Python中变量的作用域(variable scope)
http://www.crifan.com/summary_python_variable_effective_scope/ 解释python中变量的作用域 示例: 1.代码版 #!/usr/bin/ ...
- 使用Myeclipse 2015 进行 Hdp 4 windows 开发
在本地环境下进行开发,使用cygwin安装 Hdp那就是一个呵呵岂能概括. 所以啊,还是用Hdp windows进行开发测试吧.这样感觉省心点.具体 Hdp windows的安装参看前面的文章或自行G ...