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. 使用c# 实现冒泡排序

    冒泡排序是一个经典的案例 实现原理就数与数前后两两比较,如果前面比后面大则交换位置.最终达到从小到大的顺序,这样的排序方式就是冒泡排序. //冒泡排序 ;//定义一个中间变量,用来交换值 , , , ...

  2. Ef-Code-First 使用实体类映射出数据库

    最近面试时很多面试官都问到了EF框架 好记性不如烂笔头 赶紧记下来 code-first是EF框架中的一种,是使用实体类来进行数据库表的映射,所以实体类中的字段要规范(我认为) 比如: 如果有外键的话 ...

  3. 解决linux下source /etc/profile关闭终端失效问题

    本来想配置环境变量的,看网上和博客上很多说改/etc/profile,然后source /etc/profile之后就可以永久保存使环境变量生效,但是终端一关闭,就环境变量就失效了,其他终端也用不了. ...

  4. js中cookie,localStorage(sessionStorage)的存取

    一.cookie (原生的不好用,自己简单封装) 1. 存cookie的方法: function setCookie(c_name,value,expiredays) { var exdate=new ...

  5. Office 2010激活 NO KMS products detected问题

    今天用office2010激活工具Office 2010 Toolkit激活安装的office2010时悲剧的遇到了这个问题,如下图: (这张图是从网上找的,不过和我遇到的问题是一样的). 然后上网搜 ...

  6. POJ 2664

    #include<iostream> #include<stdio.h> #include<algorithm> #define MAXN 105 using na ...

  7. 解决白屏(vue) - webpace es6转es5

    1.npm安装 npm install babel-polyfillnpm install es6-promise package.json中会出现 "babel-polyfill" ...

  8. 十分钟内在Ubuntu系统上搭建Mono开发环境(Mono软件Ubuntu系统国内镜像源、Mono国内镜像源)

    Mono软件Ubuntu系统国内镜像源.Mono国内镜像源 http://download.githall.cn/repo 替换为国内源(非官方)有利于加快mono的安装速度,一般情况下,完成mono ...

  9. python获取动态网站上面的动态加载的数据(初级)

    我们在处理一些网站数据的时候,有时候我们需要的数据很多都是动态加载的,而不都是静态的,以下以一个实例来介绍简单的获取动态数据,首先申明本人小白,还在学习python中,这个方法还是比较笨拙的,但是对于 ...

  10. hao643.com劫持(修改快捷方式跳转至hao123.com)

    >症状:所有浏览器快捷方式,都被加上尾巴,例如IE的:"C:\Program Files\Internet Explorer\iexplore.exe" http://hao ...