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 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.

题意:给你一个容器,每次执行add或者remove操作,当容器不为空时,输出容器中元素的中位数;如果为空,输出Empty!或者Wrong!

分析:比赛时也想到了用multiset,但是删除时会把所有相同的元素一起删掉,没想到用指针指向要删除的元素。这题还可以用树状数组或者线段树来做。

#include<stdio.h>
#include<string.h>
#include<set>
#include<algorithm>
using namespace std;

multiset<int> ms;
multiset<int>::iterator it; //指向要删除的元素的位置
multiset<int>::iterator it1; //记录中位数的位置
multiset<int>::iterator it2; //辅助迭代器

int main()
{
    int t, n, i;
    char op[20];
    int a;
    scanf("%d",&t);
    while(t--)
    {
        ms.clear();
        scanf("%d",&n);
        for(i = 0; i < n; i++)
        {
            scanf("%s%d",op, &a);
            if(!strcmp(op, "add"))
            {
                ms.insert(a);
                if(ms.size() == 1)
                    it1 = ms.begin();
                else
                {
                    if((ms.size()&1) && a >= *it1) it1++;
                    else if((ms.size() % 2 == 0 && a < *it1)) it1--;
                }
            }
            else
            {
                it = ms.find(a);
                if(it == ms.end())
                {
                    printf("Wrong!\n");
                    continue;
                }
                else
                {
                    if(*it1 == a)
                    {
                        it = it1;
                        if(ms.size() % 2 == 0)
                            it1++;
                        else
                            it1--;
                    }
                    else
                    {
                        if((ms.size()&1) && a >= *it1) it1--;
                        else if(ms.size() % 2 == 0 && a < *it1) it1++;
                    }
                    ms.erase(it);
                }
            }
            int s = ms.size();
            if(s == 0) printf("Empty!\n");
            else
            {
                if(s&1)
                    printf("%d\n",*it1);
                else
                {
                    it2 = it1;
                    it2++;
                    long long ans = (long long)*it1 + (long long)*it2;
                    if(ans % 2 == 0)
                        printf("%lld\n",ans/2);
                    else
                        printf("%.1lf\n",ans/2.0);
                }
            }
        }
    }
    return 0;
}

ZOJ 3612 Median (multiset),布布扣,bubuko.com

时间: 2024-08-07 08:23:52

ZOJ 3612 Median (multiset)的相关文章

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[

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 fir

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不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大堆存放前半部分较小的元素,最小堆存放后半部分较大的元素,并且最大堆的所有元素小于最小堆的所有元素:保持最大堆最多比最小堆多一个元素.每次插入元素的时候都先插入到最大堆,如果发现最大堆比最小堆多了两个个,那么就从最大堆里面拿出最大的放到最小堆里面:如果发现最大堆里面新插入的元素破坏了最大堆所有元素小于

ZOJ3612 Median treap

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4736 解题思路:在treap里面加上一个num域,用来表示这个节点重复数的个数就行!WA了很多次,发现自己在处理负数的时候有点问题了.然后拿cxlove 的SBT代码比对了一下,时间上差不多,但是空间差距太大了,是他的六倍左右,然后想到应该优化内存,删除节点,最后变成他的 1/3 左右. 解题代码: 1 // File Name: 4736.cpp 2 // Aut