算法设计与复杂性分析 第二次上机 Dynamic Median

来源:互联网 发布:淘宝看图购在哪里发布 编辑:程序博客网 时间:2024/06/05 06:23

总时间限制: 
3000ms 
内存限制: 
65536kB
描述

设计一个数据结构,初始为空,支持以下操作:

(1)增加一个元素,要求在log(n)时间内完成,其中n是该数据结构中当前元素的个数。注意:数据结构中允许有重复的元素。

(2)返回当前元素集合的中位数,要求在常数时间内完成。如果当前元素的个数为偶数,那么返回下中位数(即两个中位数中较小的一个)。

(3)删除中位数,要求在log(n)时间内完成。


输入
输入的第一行是一个自然数T,代表测试数据的组数((1 ≤ T ≤ 600))。每组测试数据的第一行是个自然数N,代表操作的次数,1<=N<=10000。后面的N行中的每行代表一个操作,每次操作首先输入一个单字符代表操作的类型:

I表示插入,后面跟着输入一个正整数(这是唯一带有输入数值的操作)。
Q表示查询,输出当前的中位数(这是唯一产生输出的操作)。
D表示删除当前的中位数。

输入保证是正确的:查询时集合保证不为空(即中位数是存在的),删除时保证集合中有足够可供删除的元素。
输出
每次查询操作Q时输出的中位数,每次输出单独占一行。
样例输入
18I 4I 3I 5Q  DI 10I 2Q
样例输出
43
提示
123


由于查找中位数需要在常数时间内操作,所以需要维护两个优先队列,一个用于存储比中位数小或者中位数,一个用户存储比中位数大的。前者从大到小,后者从小到大。



#include <stdio.h>#include <iostream>#include <queue>using namespace std;//维护两个优先队列,一个从大到小,存储比中位数小的数据,一个从小到大,存储比中位数大的数据。int main(){    int t;    scanf("%d", &t);    while(t > 0)    {        priority_queue<int> les;        priority_queue<int, vector<int>, greater<int> > gre;        int n;        scanf("%d", &n);        while(n > 0)        {            char ch;            scanf("%c", &ch);            scanf("%c", &ch);            switch(ch)            {            case 'I':                int m;                scanf("%d", &m);                if(les.size() == gre.size())                {                    if(!gre.empty() && m > gre.top())                    {                        int tmp = gre.top();                        gre.pop();                        gre.push(m);                        les.push(tmp);                    }                    else                        les.push(m);                }                else                {                    if(!les.empty() && m < les.top())                    {                        int tmp = les.top();                        les.pop();                        gre.push(tmp);                        les.push(m);                    }                    else                        gre.push(m);                }                //scanf("%c", &ch);                break;            case 'Q':                printf("%d\n", les.top());                //scanf("%c", &ch);                break;            case 'D':                les.pop();                if(les.size() < gre.size())                {                    int tmp = gre.top();                    gre.pop();                    les.push(tmp);                }                //scanf("%c", &ch);                break;            }            n--;            //printf("%d\n", n);        }        t--;    }    return 0;}


使用自定义cmp的实现方式为:

#include <stdio.h>#include <iostream>#include <queue>using namespace std;//维护两个优先队列,一个从大到小,存储比中位数小的数据,一个从小到大,存储比中位数大的数据。struct cmp{    bool operator() (int a, int b)    {        if(a > b)            return true;        return false;    }};int main(){    int t;    scanf("%d", &t);    while(t > 0)    {        priority_queue<int> les;        priority_queue<int, vector<int>, cmp> gre;        int n;        scanf("%d", &n);        while(n > 0)        {            char ch;            scanf("%c", &ch);            scanf("%c", &ch);            switch(ch)            {            case 'I':                int m;                scanf("%d", &m);                if(les.size() == gre.size())                {                    if(!gre.empty() && m > gre.top())                    {                        int tmp = gre.top();                        gre.pop();                        gre.push(m);                        les.push(tmp);                    }                    else                        les.push(m);                }                else                {                    if(!les.empty() && m < les.top())                    {                        int tmp = les.top();                        les.pop();                        gre.push(tmp);                        les.push(m);                    }                    else                        gre.push(m);                }                //scanf("%c", &ch);                break;            case 'Q':                printf("%d\n", les.top());                //scanf("%c", &ch);                break;            case 'D':                les.pop();                if(les.size() < gre.size())                {                    int tmp = gre.top();                    gre.pop();                    les.push(tmp);                }                //scanf("%c", &ch);                break;            }            n--;            //printf("%d\n", n);        }        t--;    }    return 0;}


原创粉丝点击