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;
}

大神地址: 点击打开链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值