Median(vector+二分)
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+二分)的更多相关文章
- 【POJ - 3579 】Median(二分)
Median Descriptions 给N数字, X1, X2, ... , XN,我们计算每对数字之间的差值:∣Xi - Xj∣ (1 ≤ i < j ≤N). 我们能得到 C(N,2) 个 ...
- BZOJ 2083: [Poi2010]Intelligence test [vector+二分]
2083: [Poi2010]Intelligence test Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 469 Solved: 227[Su ...
- POJ 3579 Median(二分答案)
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11599 Accepted: 4112 Description G ...
- Gunner II--hdu5233(map&vector/二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5233 题意:有n颗树,第 i 棵树的高度为 h[i],树上有鸟,现在这个人要打m次枪,每次打的高度是 ...
- luogu P5826 【模板】子序列自动机 主席树 vector 二分
LINK:子序列自动机 想了一些很有趣的做法. dp 容易看出 f[i][j]表示前i个数匹配了j个数的dp 不过复杂度很高. 贪心 容易想到匹配的时候每个数字尽量往前匹配 这样显然是最优的 复杂度Q ...
- POJ 3579 Median(二分答案+Two pointers)
[题目链接] http://poj.org/problem?id=3579 [题目大意] 给出一个数列,求两两差值绝对值的中位数. [题解] 因为如果直接计算中位数的话,数量过于庞大,难以有效计算, ...
- POJ 3579 Median 【二分答案】
<题目链接> 题目大意: 给出 N个数,对于存有每两个数的差值的序列求中位数,如果这个序列长度为偶数个元素,就取中间偏小的作为中位数. 解题分析: 由于本题n达到了1e5,所以将这些数之间 ...
- HackerRank "Median Updates"
Same as LintCode "Sliding Window Median", but requires more care on details - no trailing ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
随机推荐
- DDD Code First 迁移数据实现EF CORE的软删除,值对象迁移配置
感谢Jeffcky大佬的博客: EntityFramework Core 2.0全局过滤 (HasQueryFilter) https://www.cnblogs.com/CreateMyself/p ...
- UWP Button添加圆角阴影(二)
原文:UWP Button添加圆角阴影(二) 阴影 对于阴影呢,WindowsCommunityToolkit中已经有封装好的DropShadowPanel啦,只要引用Microsoft.Toolki ...
- Flask系列09--Flask中WTForms插件,及自定义验证器
一.概述 django中的forms组件非常的方便,在flask中有WTForms的组件实现的也是类似的功能, 安装这个插件 二.简单使用 文档地址https://wtforms.readthedoc ...
- 201621123018《Java程序设计》第5周学习报告
1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 接口.interface.implements.Comparable.Comparator. 1.2 尝试使用思维导图将这些关键 ...
- 网络编程-socket(三)(TCP长连接和UDP短连接、时间服务器)
详解地址:https://www.cnblogs.com/mys6/p/10587673.html TCP server端 import socketsk = socket.socket() # 创建 ...
- Python MySQL - 创建/查询/删除数据库
#coding=utf-8 import mysql.connector import importlib import sys #连接数据库的信息 mydb = mysql.connector.co ...
- cisco 的ACL
搞网络好几年了,怎么说呢,水平一直停留在NA-NP之间,系统的学完NA后,做了不少实验,后来也维护了企业的网络,各种网络设备都玩过(在商汤用的Juniper srx 550 我认为在企业环境,非IDC ...
- BitArray源码解析
BitArray是C# System.Collections内置的集合,用于帮助进行位运算. BitArray的使用示例 // 创建两个大小为 8 的点阵列 BitArray ba1 = new Bi ...
- C# 泛型类在使用中约束
首先看一下泛型的基本语法 访问修饰符 返回类型 泛型方法名 <T>(T 参数) 1):无法在泛型方法内部给任何 T 类型创建实例的对象,因为在泛型方法内部不知道传进来的对象有哪些构造函 ...
- oc中的枚举
如果一个变量只有几种可能的值,比如星期有几天,一年有几个季节等.这个时候可以用枚举变量. 先定义类型再定义变量,如:enum siji{chun,xia,qiu,dong} 也可以定义匿名:enum{ ...