zoukankan      html  css  js  c++  java
  • 洛谷 [TJOI2010]中位数

    题目链接

    题解

    比较水。。

    常见套路,维护两个堆

    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
    ", p.top());
    	}
        return 0;
    }
    
    
  • 相关阅读:
    只能输入数字的文本框
    Ajax
    Crtl+Enter提交留言
    onkeydown onkeyup键盘事件
    面向对象基础
    JS鼠标拖拽
    博客收藏
    不错的按钮
    如何在你的java程序中注册系统级热键
    开源GIS系统
  • 原文地址:https://www.cnblogs.com/zzy2005/p/9892150.html
Copyright © 2011-2022 走看看