对顶堆0分求调!样例能过,测试点全WA!
查看原帖
对顶堆0分求调!样例能过,测试点全WA!
470966
Ding_wd_Peter146_楼主2024/1/16 12:48

对顶堆0分求调!样例能过,测试点全WA!

# include <bits/stdc++.h>
using namespace std;

priority_queue <int> maxhp, minhp;
const int MAXN = 1e5 + 7;
int a[MAXN];
int k;

void mthp()
{
	while (maxhp.size() > k)
	{
		minhp.push(-maxhp.top() );
		maxhp.pop();
	}
	while (maxhp.size() < k)
	{
		maxhp.push( -minhp.top() );
		minhp.pop();
	}
}

int main()
{
    int n;
    cin >> n;
    int x;
    cin >> x;
    maxhp.push(x);
    for (int i = 2; i <= n; i++)
    {
    	cin >> x;
    	if (x <= maxhp.top())
    		maxhp.push(x);
    	else
    		minhp.push(-x);
	}
	int m;
	cin >> m;
    for (int i = 1; i <= m; i++)
	{
    	string s;
    	cin >> s;
    	if (s == "add")
    	{
    		k = (i + 1 + n) / 2;
    		cin >> x;
    		if (x < maxhp.top())
    			maxhp.push(x);
    		else
    			minhp.push(-x);
    		mthp();
		}
		if (s == "mid")
		{
		    mthp();
			cout << maxhp.top() << endl;
		}
	}
    return 0;
}

2024/1/16 12:48
加载中...