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 if m is even or the average number of the middle 2 numbers if m is odd. You have an empty number list at first. Then you can add or remove some number from the list.

For each add or remove operation, output the median of the number in the list please.

Input

This problem has several test cases. The first line of the input is an integer T (0<T<=100) indicates the number of test cases. The first line of each test case is an integer n (0<n<=10000) indicates the number of operations. Each of the next n lines is either "add x" or "remove x"(-231<=x<231) indicates the operation.

Output

For each operation of one test case: If the operation is add output the median after adding x in a single line. If the operation is remove and the number x is not in the list, output "Wrong!" in a single line. If the operation is remove and the number x is in the list, output the median after deleting x in a single line, however the list is empty output "Empty!".

Sample Input

2
7
remove 1
add 1
add 2
add 1
remove 1
remove 2
remove 1
3
add -2
remove -2
add -1

Sample Output

Wrong!
1
1.5
1
1.5
1
Empty!
-2
Empty!
-1

Hint

if the result is an integer DO NOT output decimal point. And if the result is a double number , DO NOT output trailing 0s.

题意:找中位数,由于数据比较大,用到了vector容器,

我也不会用,比赛前看过一点,可是不知道怎么用,so。。。比赛完,借大神的代码来观摩下,哈哈。我发现这个真的很好用。我一定要把他学会!!!

ps:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4736

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
vector<int>vet;
int deal(int x)
{
int left=,right=vet.size()-;
while(left<=right)
{
int mid=(left+right)/;
if(vet[mid]==x)
{
return mid;
}
else if(vet[mid]>x)
{
right=mid-;
}
else
{
left=mid+;
}
}
return -;
}
int erfen(int x)
{
if(vet.size()==) return ;
if(x<vet[]) return ;
if(x>vet[vet.size()-]) return vet.size();
int left=,right=vet.size()-,k=-;
while(left<=right)
{
int mid=(left+right)/;
if(vet[mid]>=x&&vet[mid-]<=x)
{
return mid;
}
else if(vet[mid]<=x&&vet[mid+]>=x)
{
return mid+;
}
else if(vet[mid]>x)
{
right=mid-;
}
else left=mid+;
}
}
int main()
{
int text;
scanf("%d",&text);
while(text--)
{
int n;
scanf("%d",&n);
vet.clear();
vector<int>::iterator it=vet.begin();
while(n--)
{ char ch[];
int x;
scanf("%s%d",ch,&x);
if(ch[]=='r')
{
if(vet.size()==)
{
printf("Wrong!\n");
continue;
}
else
{
int tmp=deal(x);
if(tmp==-)
{
printf("Wrong!\n");
continue;
}
else
{
it=vet.begin();
vet.erase((it+tmp));
int len=vet.size();
if(len==)
{
printf("Empty!\n");
continue;
}
else if(len%==)
cout<<vet[len/]<<endl;
else
{
long long ans=(long long)vet[len/]+(long long)vet[len/-];
if(ans%==)
printf("%lld\n",ans/);
else
{
double sum=ans/2.0;
printf("%.1lf\n",sum);
}
} }
}
}
else
{
int tmp=erfen(x);
it=vet.begin();
vet.insert((it+tmp),x);
int len=vet.size();
if(len%==)
printf("%d\n",vet[len/]);
else
{
long long ans=(long long)vet[len/]+(long long)vet[len/-];
if(ans%==)
printf("%lld\n",ans/);
else
{
double sum=ans/2.0;
printf("%.1lf\n",sum);
}
}
}
}
}
return ;
}

加油!!!加油!!!

Median(vector+二分)的更多相关文章

  1. 【POJ - 3579 】Median(二分)

    Median Descriptions 给N数字, X1, X2, ... , XN,我们计算每对数字之间的差值:∣Xi - Xj∣ (1 ≤ i < j ≤N). 我们能得到 C(N,2) 个 ...

  2. BZOJ 2083: [Poi2010]Intelligence test [vector+二分]

    2083: [Poi2010]Intelligence test Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 469  Solved: 227[Su ...

  3. POJ 3579 Median(二分答案)

    Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11599 Accepted: 4112 Description G ...

  4. Gunner II--hdu5233(map&vector/二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5233 题意:有n颗树,第 i 棵树的高度为 h[i],树上有鸟,现在这个人要打m次枪,每次打的高度是 ...

  5. luogu P5826 【模板】子序列自动机 主席树 vector 二分

    LINK:子序列自动机 想了一些很有趣的做法. dp 容易看出 f[i][j]表示前i个数匹配了j个数的dp 不过复杂度很高. 贪心 容易想到匹配的时候每个数字尽量往前匹配 这样显然是最优的 复杂度Q ...

  6. POJ 3579 Median(二分答案+Two pointers)

    [题目链接] http://poj.org/problem?id=3579 [题目大意] 给出一个数列,求两两差值绝对值的中位数. [题解] 因为如果直接计算中位数的话,数量过于庞大,难以有效计算, ...

  7. POJ 3579 Median 【二分答案】

    <题目链接> 题目大意: 给出 N个数,对于存有每两个数的差值的序列求中位数,如果这个序列长度为偶数个元素,就取中间偏小的作为中位数. 解题分析: 由于本题n达到了1e5,所以将这些数之间 ...

  8. HackerRank "Median Updates"

    Same as LintCode "Sliding Window Median", but requires more care on details - no trailing ...

  9. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

随机推荐

  1. 剑指offer编程题Java实现——面试题4替换空格

    题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. package Solution; ...

  2. 【BZOJ2882】 工艺(SAM)

    传送门 BZOJCH 洛谷 Solution 这个东西要求的不就是最小表示法吗? 把原串复制一遍然后都加到后缀自动机里面去. 用个map跑一下,这样子可以保证每一次选的是最小字典序的. 然后跑\(n\ ...

  3. ADB 命令介绍

    Android adb shell am 命令介绍 am这个指令是 activity manager的缩写.这个命令可以启动Activity.打开或关闭进程.发送广播等操作. am命令格式如下 adb ...

  4. jQuery基础(2)

    一.jQuery的属性操作 jQuery的属性操作分为四部分: html标签属性操作:是对html文档中的标签属性进行读取,设置和移除操作.比如attr().removeAttr(): DOM属性操作 ...

  5. .NET Core 从1.1升级到2.0记录(Cookie中间件踩坑)

    .NET Core 2.0 新时代 万众瞩目的.NET Core 2.0终于发布了,原定于9.19的dotnetconf大会的发布时间大大提前了1个月,.NET Core 2.0/.NET Stand ...

  6. cmd下【java监视和管理控制台】

    不需要安装插件,只要jmeter的运行环境配置好就可以了:打开这个小工具的步骤很简单,如果你已经配置好了Jmeter运行的环境,那么你也就不用去做其他的配置,直接 点击:开始——>运行——> ...

  7. python3模块: requests

    Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务. 发送G ...

  8. CI框架入门笔记

    当前(2019-03-22)CodeIgniter 框架的最新版本是 3.1.5,于2017年6月发布,距今快两年了也没有更新,这与 Laravel 的更新速度相比差距太大了.因为确实,它是一个很古老 ...

  9. 阿里语音识别(语音转文字)java调用全程手把手详解-适合中小学生快速上手

    阿里语音识别服务java调用全程手把手详解-适合中小学生快速上手 阿里语音识别与百度语音识别的调用对比: 用例:1分30秒的录音文件    百度用时:3秒    阿里用时:30秒    识别准确率来看 ...

  10. Service Fabric学习-从helloworld开始(无状态服务)

    原先做服务器程序, 都是部署在xx云上, 也没理解云是个啥, 不就是个服务器(虚拟机)租赁商吗? 好吧, 其实这个是IaaS, 而接下来要学习的ServiceFabric(以下简称SF)是PaaS. ...