Codeforces Round #357 (Div. 2)C. Heap Operations

用单调队列(从小到大),模拟一下就好了,主要是getMin比较麻烦,算了,都是模拟。。。。也没什么好说的。。

#include<cstdio>
#include<map>
#include<queue>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;

priority_queue<int, vector<int>, greater<int> >qq;

struct asd{
    char s[10];
    int k;
};
asd q[500000];

int main()
{
    int n;
    scanf("%d",&n);
    int x;
    char ss[10];
    int num=0;
    for(int i=0;i<n;i++)
    {
        scanf("%s",&ss);

        if(ss[0]=='i')
        {
            scanf("%d",&x);
            strcpy(q[num].s,"insert");
            q[num].k=x;
            num++;
            qq.push(x);
        }
        else if(ss[0]=='r')
        {
            int s1,s2;
            if(!qq.empty())
            {
                s1=qq.top();
                strcpy(q[num].s,"removeMin");
                q[num].k=s1;
                num++;
                qq.pop();
            }
            else
            {
                strcpy(q[num].s,"insert");
                q[num].k=0;
                num++;

                strcpy(q[num].s,"removeMin");
                q[num].k=0;
                num++;
            }
        }
        else if(ss[0]=='g')
        {
            scanf("%d",&x);
            int s1;
            if(!qq.empty())
            {
                s1=qq.top();
                if(s1==x)
                {
                    strcpy(q[num].s,"getMin");
                    q[num].k=x;
                    num++;
                }
                else if(s1>x)
                {
                    strcpy(q[num].s,"insert");
                    q[num].k=x;
                    num++;
                    qq.push(x);

                    strcpy(q[num].s,"getMin");
                    q[num].k=x;
                    num++;
                }
                else if(s1<x)
                {
                    int flag=0;
                    while(!qq.empty())
                    {
                        int nn=qq.top();
                        if(nn==x)
                        {
                            strcpy(q[num].s,"getMin");
                            q[num].k=x;
                            num++;
                            break;
                        }
                        else if(nn>x)
                        {
                            strcpy(q[num].s,"insert");
                            q[num].k=x;
                            num++;
                            qq.push(x);

                            strcpy(q[num].s,"getMin");
                            q[num].k=x;
                            flag=1;
                            num++;
                            break;
                        }
                        else if(nn<x)
                        {
                            strcpy(q[num].s,"removeMin");
                            q[num].k=nn;
                            num++;
                            qq.pop();
                        }
                    }
                }
            }
            if(qq.empty())
            {
                strcpy(q[num].s,"insert");
                q[num].k=x;
                num++;
                qq.push(x);

                strcpy(q[num].s,"getMin");
                q[num].k=x;
                num++;
            }
        }
    }
    printf("%d\n",num);
    for(int i=0;i<num;i++)
    {
        if(q[i].s[0]=='r')
        {
            printf("%s\n",q[i].s);
        }
        else
        {
            printf("%s %d\n",q[i].s,q[i].k);
        }
    }
    return 0;
}

/*
2
insert
getMin 4


8
insert 1
insert 1
insert 1
removeMin
removeMin
removeMin
getMin 2
getMin 3
13
insert -851839419
removeMin
removeMin
g




/*
2
insert
getMin 4


8
insert 1
insert 1
insert 1
removeMin
removeMin
removeMin
getMin 2
getMin 3
13
insert -851839419
removeMin
removeMin
getMin -624068418
insert 119194510
removeMin
removeMin
removeMin
getMin -179673992
getMin 520596304
insert -265233646
getMin -222234500
removeMin


6
insert 1
insert 1
insert 1
removeMin
getMin 6
getMin 3

4
getMin 4
getMin 3
getMin 2
getMin 1

4
getMin 1
getMin 2
getMin 3
getMin 4

*/

posted @ 2016-06-15 10:09  see_you_later  阅读(108)  评论(0编辑  收藏  举报