HDU 5349 MZL's simple problem(队列)

题目地址:点击打开链接

思路:刚开始还以为每次输出最大值还要删除呢,调了半天没有过,看别人的代码,没看到把最大值弹出,每次只保留最大值,以为是数据的BUG,结果又一看题query的意思是查询.。。。把自己的代码改了一下,没过,直接参考写了个用优先队列和队列写的代码,有时间改一下,还有就是注意有多组测试数据,看到有人用multiset也A了,有时间学一下

错误代码1:

#include <iostream>
#include <queue>
#include <cstdio>

using namespace std;

struct num
{
    int x;
    bool operator < (const num &a) const
    {
        return x < a.x;//最大值优先
    }
};

int main()
{
    priority_queue<num> pq;
    num out,in;
    int n,a,b,sum;
    while(scanf("%d",&n) != EOF)
    {
        sum = 0;
        while(n--)
        {
            while(!pq.empty())
                pq.pop();
            scanf("%d",&a);
            if(a == 1)
            {
                scanf("%d",&b);
                in.x = b;
                pq.push(in);
            }
            else if(a == 2)
            {
                if(!pq.empty())
                    sum++;
                else
                    sum = 0;
            }
            else
            {
                if(sum < pq.size() && !pq.empty())
                {
                    out = pq.top();
                    pq.pop();
                    printf("%d\n",out.x);
                }
                else
                {
                    printf("0\n");
                }
            }
        }
    }
    return 0;
}

错误代码2:

#include <iostream>
#include <queue>
#include <cstdio>

using namespace std;

struct num
{
    int x;
    bool operator < (const num &a) const
    {
        return x < a.x;//最大值优先
    }
};

int main()
{
    priority_queue<num> pq;
    num out,in;
    int n,a,b,sum;
    while(scanf("%d",&n) != EOF)
    {
        sum = 0;
        while(n--)
        {
            while(!pq.empty())
                pq.pop();
            scanf("%d",&a);
            if(a == 1)
            {
                scanf("%d",&b);
                in.x = b;
                pq.push(in);
            }
            else if(a == 2)
            {
                if(!pq.empty())
                    sum++;
            }
            else
            {
                if(sum < pq.size())
                {
                    out = pq.top();
                    printf("%d\n",out.x);
                }
                else
                {
                    printf("0\n");
                }
            }
        }
    }
    return 0;
}

AC代码:

#include <iostream>
#include <queue>
#include <cstdio>

using namespace std;

const int inf = 0x3f3f3f3f;

int main()
{
    int n,a,b,max1;
    while(scanf("%d",&n) != EOF)
    {
        queue<int> q;//在里面定义省得清空
        max1 = -inf;
        while(n--)
        {
            scanf("%d",&a);
            if(a == 1)
            {
                scanf("%d",&b);
                q.push(b);
                max1 = max(max1,b);
            }
            else if(a == 2)
            {
                if(!q.empty())
                {
                    q.pop();
                }
                if(q.empty())
                    max1  = -inf;
            }
            else
            {
                if(q.empty())
                {
                    printf("0\n");
                }
                else
                {
                    printf("%d\n",max1);
                }
            }
        }
    }
    return 0;
}

AC代码:

#include <iostream>
#include <queue>
#include <cstdio>

using namespace std;

const int inf = 0x3f3f3f3f;

struct num//不用重新定义,因为默认排序是升序,
{
    int x;
    bool operator < (const num &a) const
    {
        return x < a.x;//最大值优先
    }
};

int main()
{
    num in;
    int n,a,b,max1;
    while(scanf("%d",&n) != EOF)
    {
        priority_queue<num> pq;//在里面定义省得清空
        max1 = -inf;
        while(n--)
        {
            scanf("%d",&a);
            if(a == 1)
            {
                scanf("%d",&b);
                in.x = b;
                pq.push(in);
                max1 = max(max1,b);
            }
            else if(a == 2)
            {
                /*if(!pq.empty())
                {
                    pq.pop();不能这样搞,会导致队列没值,最大值没变
                }
                else
                    max1  = -inf;*/
                if(!pq.empty())
                {
                    pq.pop();
                }
                if(pq.empty())
                    max1  = -inf;
            }
            else
            {
                if(pq.empty())
                {
                    printf("0\n");
                }
                else
                {
                    printf("%d\n",max1);
                }
            }
        }
    }
    return 0;
}

大神地址: 点击打开链接

你可能感兴趣的:(HDU 5349 MZL's simple problem(队列))