无序数组的中位数(set+deque)hdu5249
KPI
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 901 Accepted Submission(s): 398
每组数据第一行有一个n(1≤n≤10000),代表服务记录数。
接下来有n行,每一行有3种形式
"in x": 代表重要值为x(0≤x≤109)的请求被推进管道。
"out": 代表服务拉取了管道头部的请求。
"query: 代表我想知道当前管道内请求重要值的中间值. 那就是说,如果当前管道内有m条请求, 我想知道,升序排序后第floor(m/2)+1th 条请求的重要值.
为了让题目简单,所有的x都不同,并且如果管道内没有值,就不会有"out"和"query"操作。
Case #i:
然后每一次"query",输出当前管道内重要值的中间值。
分析:题目中说有值的加入和值的取出,首先可以用双端队列维护一个队列请求,即先进先出,用q.front()和q.pop_front()读取和删除队列头元素,用q.back()和q.push_back()读取和加入队尾元素,而维护队列的有序可以用set容器,分别建立两个容器sa和sb,无论是删除和加入元素的时候保证size(sa)==size(sb)或者size(sb)+1==size(sb);则sb.begin() 就是中位数的值
程序:
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"algorithm"
#include"queue"
#include"math.h"
#include"iostream"
#include"vector"
#define M 100009
#define inf 0x3f3f3f3f
#define eps 1e-9
#define PI acos(-1.0)
#include"map"
#include"vector"
#include"set"
#include"string"
using namespace std;
int main()
{
int n,kk=;
while(scanf("%d",&n)!=-)
{
set<int>sa,sb;
sa.insert(-);
sb.insert();
deque<int>q;
printf("Case #%d:\n",kk++);
while(n--)
{
char str[];
scanf("%s",str);
if(strcmp(str,"in")==)
{
int k;
scanf("%d",&k);
q.push_back(k);
sa.insert(k);
int la=sa.size();
int lb=sb.size();
int u=*(sa.rbegin());
int v=*(sb.begin());
if(la>lb)
{
sa.erase(u);
sb.insert(u);
}
u=*(sa.rbegin());
v=*(sb.begin());
if(u>v)
{
sb.erase(v);
sa.insert(v);
sa.erase(u);
sb.insert(u);
}
}
else if(strcmp(str,"out")==)
{ int u=q.front();
q.pop_front();
sa.erase(u);
sb.erase(u); int la=sa.size();
int lb=sb.size();
if(la+==lb)
{
sa.insert(*(sb.begin()));
sb.erase(*(sb.begin()));
}
else if(la==lb+)
{
sb.insert(*(sa.rbegin()));
sa.erase(*(sa.rbegin()));
}
}
else
{
printf("%d\n",*(sb.begin()));
}
}
}
return ;
}
/* 40
in 11
in 22
in 33
in 44
in 55
in 66
query */
无序数组的中位数(set+deque)hdu5249的更多相关文章
- 1005E1 Median on Segments (Permutations Edition) 【思维+无序数组求中位数】
题目:戳这里 百度之星初赛原题:戳这里 题意:n个不同的数,求中位数为m的区间有多少个. 解题思路: 此题的中位数就是个数为奇数的数组中,小于m的数和大于m的数一样多,个数为偶数的数组中,小于m的数比 ...
- 两个无序数组分别叫A和B,长度分别是m和n,求中位数,要求时间复杂度O(m+n),空间复杂度O(1) 。
#include <iostream> using namespace std; /*函数作用:取待排序序列中low.mid.high三个位置上数据,选取他们中间的那个数据作为枢轴*/ i ...
- 从0打卡leetcode之day 5 ---两个排序数组的中位数
前言 我靠,才坚持了四天,就差点不想坚持了.不行啊,我得把leetcode上的题给刷完,不然怕是不好进入bat的大门. 题目描述 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . ...
- 无序数组求第K大的数
问题描述 无序数组求第K大的数,其中K从1开始算. 例如:[0,3,1,8,5,2]这个数组,第2大的数是5 OJ可参考:LeetCode_0215_KthLargestElementInAnArra ...
- [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 ...
- 有1,2,3一直到n的无序数组,排序
题目:有1,2,3,..n 的无序整数数组,求排序算法.要求时间复杂度 O(n), 空间复杂度O(1). 分析:对于一般数组的排序显然 O(n) 是无法完成的. 既然题目这样要求,肯定原先的数组有一定 ...
- 快速查找无序数组中的第K大数?
1.题目分析: 查找无序数组中的第K大数,直观感觉便是先排好序再找到下标为K-1的元素,时间复杂度O(NlgN).在此,我们想探索是否存在时间复杂度 < O(NlgN),而且近似等于O(N)的高 ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)
转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...
随机推荐
- Java中测试对象的等价性
Java中用于测试对象的等价性有三个操作符:== , != 和 Equals() 对于基本类型即int,boolean, byte 等等来说,==和 != 比较的是 基本类型的内容,这和c.c++是一 ...
- OmniThreadLibrary 3.03b发布了
虽然版本号升的不大,但这也是一个重要的版本.作者发现了一个长期存在的bug,就是建立一个线程,如果不指定线程的优先级则默认设置为idle.(正确的应是Normal) 看一下具体的改动情况: 新功能: ...
- hbase基本命令
基本命令 建表scores 具有两个列族grad 和courese create 'scores','grade', 'course' 查看当前HBase中具有哪些表 list 查看表结构 des ...
- 地图API使用文档-以腾讯地图为例
目录 腾讯地图API 2 1.API概览... 2 1.1 WebService API(官网注明是beta版本,可能不稳定,慎用):... 2 1.2 URL API:... 2 1.3 静态图AP ...
- 【转】MySQL USE NAMES 'UTF8'
先说MySQL的字符集问题.Windows下可通过修改my.ini内的 # CLIENT SECTION [mysql] default-character-set=utf8 # SERVER SEC ...
- MVC 中的 ViewModel
此文章总结自:http://rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp.net-mvc-applica ...
- Magento white screen or how XML can break your site?
Magento white screen or how XML can break your site? by SANDO on 02. OCT, 2012 in MAGENTO, SMALL TIP ...
- Marriage Match IV---hdu3416(spfa + Dinic)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3416 有一个有向图,n个点,m条边,给一个起点和终点,求出从起点到终点的最短路共有几条,每 ...
- Stakeholder Risk Management
In this article we'll address the people swirling around your project: stakeholders. You'll find som ...
- POJ 1041问题描述
Description Little Johnny has got a new car. He decided to drive around the town to visit his friend ...