UESTC OJ 1830 Cookies Test

http://www.cnblogs.com/easymath3/archive/2013/05/01/3052944.html

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <iostream>
#include <algorithm>
using namespace std;

priority_queue <int, vector<int>, less<int> > lHeap;
priority_queue <int, vector<int>, greater<int> > rHeap;

bool isQuery(char s[], int &d)
{
    if (s[0] == '#') return true;
    d = 0; int len = strlen(s);
    for (int i = 0; i < len; i++)
    {
        d *= 10;
        d += s[i] - '0';
    }
    return false;
}

int main(void)
{
   // freopen("input.txt", "r", stdin);

    char s[50]; int d;
    while (scanf("%s", s) !=EOF)
    {
        if (isQuery(s, d) == true)
        {
            printf("%d\n", rHeap.top());
            rHeap.pop();
        }
        else
        {
            if (rHeap.empty() || d <= rHeap.top()) lHeap.push(d);
            else if (d > rHeap.top()) rHeap.push(d);
        }
        while (lHeap.size() > rHeap.size())
        {
            rHeap.push(lHeap.top());
            lHeap.pop();
        }
        while (lHeap.size() + 1 < rHeap.size())
        {
            lHeap.push(rHeap.top());
            rHeap.pop();
        }
    }
    return 0;
}


 

你可能感兴趣的:(C++,C语言)