其他分享
首页 > 其他分享> > 【SP15376】RMID-Running Median

【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;
}

标签:case,RMID,++,Median,mid,vis,SP15376,num,test
来源: https://www.cnblogs.com/hlw1/p/11150754.html