【SP15376】RMID-Running Median

题目描述

You will be given some integers in non decreasing order and each time the median is queried you have to report and remove it. Take the smaller element as median in case of even elements.

传送门

输入输出格式

输入格式:

The input contains many test cases. Read until End Of File.

Each test case contains \(n\)

For a query, print the current median on a single line and remove it from the list.

Each test case ends with 0 on a single line, and two test cases will be separated by an empty line. All integers are guaranteed to fit in a signed 32-bit container. A query can only occur if the list is non-empty.

输出格式:

For each test case output m lines containing the answers to the corresponding queries. Print an empty line after each test case.

输入输出样例

输入样例#1:
1
2
3
4
-1
-1
5
6
7
-1
0

2
3
-1
0
输出样例#1:
2
3
5

2

分析

不知为何,洛谷上被UE了无数次,每次都显示Failed to get recod id。。。。。于是机智的在vjudge上提交并通过了。

其实这道题用对顶堆会很好写,但输入数据是递增的所以直接放在数组后也行。

代码

#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
int a[100001];
bool vis[100001];
int main(){
    int k,num=0,tot=0,mid=0;
    bool flag=0;
    while(scanf("%d",&k)!=EOF){
        if(k==0) putchar('\n'),flag=1;
        if(flag){
            flag=0,tot=0,num=0,mid=0;
            memset(a,0,sizeof(a));
            memset(vis,0,sizeof(vis));
        }
        if(k==-1){
            printf("%d\n",a[mid]);
            num--,vis[mid]=1;
            if(num%2) while(vis[++mid]);
            else while(vis[--mid]);
        }
        else if(k!=0){
            a[++tot]=k;
            num++;
            if(num%2) while(vis[++mid]);
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/hlw1/p/11150754.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值