zoj Median (multiset)

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 ifm 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 integerT (0<T<=100) indicates the number of test cases. The first line of each test case is an integern (0<n<=10000) indicates the number of operations.
Each of the nextn 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 addingx 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 deletingx 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.

1.要维护it1迭代器一直指向中位数

2.代码:

#include<cstdio>
#include<set>
#include<cstring>
#define LL long long
using namespace std;

multiset<LL> mst;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        multiset<LL>::iterator it;
        multiset<LL>::iterator it1;
        mst.clear();
        int n;
        scanf("%d",&n);
        it1=mst.begin();
        for(int i=1; i<=n; i++)
        {
            char s[20];
            LL x;
            scanf("%s%lld",s,&x);
            if(strcmp("add",s)==0)
            {
                mst.insert(x);
                if(mst.size()==1)
                {
                    it1=mst.begin();
                }
                else if(mst.size()%2==1&&x>=*it1)
                {
                    it1++;
                }
                else if(mst.size()%2==0&&x<*it1)
                {
                    it1--;
                }
            }
            else
            {
                it=mst.find(x);
                if(it==mst.end())
                {
                    printf("Wrong!\n");
                    continue;
                }
                else
                {
                    if(mst.size()==1)
                    {
                        mst.erase(it);
                        printf("Empty!\n");
                        continue;
                    }
                    else if(*it1 == x)
                    {
                        it = it1;
                        if(mst.size() % 2 == 0)
                            it1++;
                        else
                            it1--;
                    }
                    else if(mst.size()%2&&x>=*it1)
                    {
                        it1--;
                    }
                    else if(mst.size()%2==0&&x<*it1)
                    {
                        it1++;
                    }
                    mst.erase(it);
                }
            }
            multiset<LL>::iterator it2;
            if(mst.size()%2)
            {
                printf("%lld\n",*it1);
            }
            else
            {
                it2=it1;
                it2++;
                if((*it1+*it2)%2)
                {
                    printf("%.1lf\n",(*it1+*it2)/2.0);
                }
                else
                {
                    printf("%lld\n",(*it1+*it2)/2);
                }
            }
        }
    }
    return 0;
}
时间: 2024-12-23 11:48:59

zoj Median (multiset)的相关文章

ZOJ 3612 Median (multiset)

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 fi

zoj 3612 Median (splay)

题目大意: 添加和删除一个数,然后输出中位数. 简单的Splay   维护Splay上有多少个节点就可以了 #include <cstdio> #include <iostream> #define inf 1LL<<60 #define maxn 222222 #define keyTree (ch[ch[root][1]][0]) using namespace std; typedef long long LL; int S[maxn],que[maxn],ch[

Twelves Monkeys (multiset解法 141 - ZOJ Monthly, July 2015 - H)

Twelves Monkeys Time Limit: 5 Seconds      Memory Limit: 32768 KB James Cole is a convicted criminal living beneath a post-apocalyptic Philadelphia. Many years ago, the Earth's surface had been contaminated by a virus so deadly that it forced the sur

ZOJ 3963 Heap Partition(multiset + stl自带二分 + 贪心)题解

题意:给你n个数字s1~sn,要你把它们组成一棵棵二叉树,对这棵二叉树来说,所有节点来自S,并且父节点si<=子节点sj,并且i<j,问你树最少几棵二叉数.树 思路:贪心.我们往multiset加还能加子节点的节点,二分查找一个个大于等于当前插入节点的节点,然后插入,若找不到则重新建一棵树. 没想到set自带lower_bound(),第一次迭代器遍历TLE就想着手动写二分...然后发现自带二分... 代码: #include<iostream> #include<stdio

L - Median(ZOJ 4124)

Time Limit : 1 Second      Memory Limit : 65536 KB Source : 第十届山东省ACM省赛 Problem Link : ZOJ 4124 Author : Houge Date : 2019-5-19 题目大意: 给你n个数(不知道谁大谁小)和m个关系(ai,bi),每个关系代表ai严格大于bi.问你能否通过已知推出可能的中间项k的位置.(题目保证n一定是奇数) 分析: 比赛时想用拓扑排序,结果因为菜没写出来,赛后听了学长用floyd的思路,

[LeetCode] Find Median from Data Stream

Find Median from Data Stream Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examples: [2,3,4] , the median is 3 [2,3], the median is

zoj 月赛

141 - ZOJ Monthly, July 2015 - H Twelves Monkeys Time Limit: 5 Seconds      Memory Limit: 32768 KB James Cole is a convicted criminal living beneath a post-apocalyptic Philadelphia. Many years ago, the Earth's surface had been contaminated by a virus

【HackerRank】Median

题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大堆存放前半部分较小的元素,最小堆存放后半部分较大的元素,并且最大堆的所有元素小于最小堆的所有元素:保持最大堆最多比最小堆多一个元素.每次插入元素的时候都先插入到最大堆,如果发现最大堆比最小堆多了两个个,那么就从最大堆里面拿出最大的放到最小堆里面:如果发现最大堆里面新插入的元素破坏了最大堆所有元素小于

解题报告:295. Find Median from Data Stream

题目描述: Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 Desi