首页 > 其他 > 详细

洛谷 [TJOI2010]中位数

时间:2018-11-01 20:22:32      阅读:189      评论:0      收藏:0      [点我收藏+]

题目链接

题解

比较水。。

常见套路,维护两个堆

Code

#include<bits/stdc++.h>
#define LL long long
#define RG register
using namespace std;

inline int gi() {
    int f = 1, s = 0;
    char c = getchar();
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    if (c == '-') f = -1, c = getchar();
    while (c >= '0' && c <= '9') s = s*10+c-'0', c = getchar();
    return f == 1 ? s : -s;
}

priority_queue<int> p;
priority_queue<int, vector<int>, greater<int> > q;
const int N = 100010;
int a[N];
char s[10];
int main() {
    int n = gi();
    for (int i = 1; i <= n; i++) a[i] = gi();
    sort(a+1, a+1+n);
    int t = gi(), s1 = (n+1)/2, s2 = n-s1;
    for (int i = 1; i <= s1; i++)
        p.push(a[i]);
    for (int i = s1+1; i <= n; i++)
        q.push(a[i]);
    while (t--) {
        cin >> s;
        if (s[0] == 'a') {
            q.push(gi());
            s2++;
            if (q.top() < p.top()) {
                int tmp = q.top(); q.pop();
                q.push(p.top()); p.pop();
                p.push(tmp);
            }
            if (s1 < s2) {
                s2--;
                s1++;
                p.push(q.top());
                q.pop();
            }
        }
        else printf("%d\n", p.top());
    }
    return 0;
}

洛谷 [TJOI2010]中位数

原文:https://www.cnblogs.com/zzy2005/p/9892150.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!