鱼C论坛

 找回密码
 立即注册
查看: 1198|回复: 2

堆 C++ 求助

[复制链接]
发表于 2023-11-5 23:57:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
rt.

题目:https://www.luogu.com.cn/problem/P3378
评测记录:https://www.luogu.com.cn/record/133617681

要实现一个小根堆的功能,但好像出了点问题。总是看不出逻辑哪里错了。

代码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// data structure: heap

inline int _ls(int x)
{
        return 2*x+1;
}
inline int _rs(int x)
{
        return 2*x+2;
}
inline int _fa(int x)
{
        return (x-1)/2;
}
class _heap // 小根堆 
{
        private:
                vector<int> Heap;
        public:
                int size=0;
                void push(int x)
                {
                        Heap.push_back(x);
                        int p=size;
                        size++;
                        if(!p)
                        return;
                        while(Heap[_fa(p)]>Heap[p] and p>0)
                        {
                                swap(Heap[_fa(p)],Heap[p]);
                                p=_fa(p);
                        }
                }
                int top(void)
                {
                        if(!size)
                        return 0;
                        return Heap[0];
                        
                }
                void pop(void)
                {
                        if(!size)return;
                        size--;
            
                        swap(Heap[size],Heap[0]);
                        int p=0,minp;
                        while(_ls(p)<size)
                        {
                minp=(Heap[_ls(p)]<Heap[_rs(p)])?_ls(p):_rs(p);
                if(_rs(p)==size)
                minp=_ls(p);
                if(Heap[p]>Heap[minp])
                                swap(Heap[p],Heap[minp]);
                else
                break;
                                p=minp;
                        }
                }
};


int main()
{
        ios::sync_with_stdio(false);
        cin.tie(0);
        _heap a;
        int n,op,x;
        cin>>n;
        for(int i=0;i<n;i++)
        {
                cin>>op;
                if(op==1)
                {
                        cin>>x;
                        a.push(x);
                }
                else if(op==2)
                {
                        cout<<a.top()<<endl;
                }
                else
                {
                        a.pop();
                }
        }
}

数据:

input
15
1 5384
2
3
1 4337
1 291
3
1 3776
2
1 2490
1 3903
1 5038
1 4504
3
3
3
output
5384
3776
@sfqxx @Ewan-Ahiouy @Mike_python小 @元豪 @zhangjinxuan
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-5 23:58:21 | 显示全部楼层
看起来你的小根堆实现有一些问题。以下是我对你的代码进行的一些修改和注释:
#include <iostream>#include <vector>#include <algorithm>using namespace std;// data structure: heapinline int _ls(int x) {    return 2 * x + 1;}inline int _rs(int x) {    return 2 * x + 2;}inline int _fa(int x) {    return (x - 1) / 2;}class _heap // 小根堆{private:    vector<int> Heap;public:    int size = 0;    void push(int x)    {        Heap.push_back(x);        int p = size;        size++;        if (!p)            return;        while (Heap[_fa(p)] > Heap[p] && p > 0) // 这里应该是Heap[_fa(p)] >= Heap[p]        {            swap(Heap[_fa(p)], Heap[p]);            p = _fa(p);        }    }    int top(void)    {        if (!size)            return 0;        return Heap[0];    }    void pop(void)    {        if (!size)            return;        size--;        swap(Heap[size], Heap[0]);        int p = 0, minp;        while (_ls(p) < size)        {            minp = (Heap[_ls(p)] < Heap[_rs(p)]) ? _ls(p) : _rs(p);            if (_rs(p) == size)            {                minp = _ls(p);            }            if (Heap[p] > Heap[minp])            {                swap(Heap[p], Heap[minp]);            }            else            {                break;            }            p = minp;        }    }};int main(){    ios::sync_with_stdio(false);    cin.tie(0);    _heap a;    int n, op, x;    cin >> n;    for (int i = 0; i < n; i++)    {        cin >> op;        if (op == 1)        {            cin >> x;            a.push(x);        }        else if (op == 2)        {            cout << a.top() << endl;        }        else        {            a.pop();        }    }    return 0;}
我对你的代码做了以下修改:

1. 在push函数中,判断条件应为Heap[_fa(p)] >= Heap[p],而不是Heap[_fa(p)] > Heap[p]。小根堆要求父节点的值小于或等于子节点的值。
2. 在pop函数中,当右子节点不存在时,minp应该等于左子节点,否则会导致数组越界错误。
3. 添加了返回语句return 0;来结束main函数。

这些修改应该解决了你的问题。请尝试运行修改后的代码并检查结果。如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-5 23:58:47 | 显示全部楼层
这段代码实现了一个小根堆的功能,但在使用时出现了问题。根据给出的评测记录和输入数据,我们可以看到输出结果与预期不符。下面我将逐步分析代码中的问题。

首先,我们注意到在类  _heap  的定义中, size  被声明为  public  成员变量,但没有进行初始化。这可能导致在使用  size  时出现未定义的行为。为了避免这个问题,我们应该将  size  的声明和初始化放在  _heap  的构造函数中。

其次,我们来看  push  函数的实现。在向堆中插入元素时,我们首先将元素插入到  Heap  的末尾,然后根据小根堆的性质进行调整。但是,在调整的过程中,我们使用了一个变量  p  来表示当前插入元素的索引,但没有更新  p  的值。这导致在后续的循环中, p  的值始终保持不变,导致无法正确进行调整。为了解决这个问题,我们应该在每次交换元素后更新  p  的值。

接下来,我们看  pop  函数的实现。在弹出堆顶元素时,我们首先将堆顶元素与最后一个元素交换,然后根据小根堆的性质进行调整。但是,在调整的过程中,我们没有考虑到堆的大小  size  可能为 0 的情况。在这种情况下,我们应该直接返回,而不进行调整操作。为了解决这个问题,我们应该在函数开头添加一个判断条件,如果  size  为 0,则直接返回。

最后,我们来看  main  函数。在读取输入数据时,我们没有对  op  进行验证,这可能导致在输入不合法的操作时出现错误。为了避免这个问题,我们应该在读取  op  后添加一个判断条件,如果  op  不是 1、2 或 3,则直接跳过当前循环。

综上所述,以下是修正后的代码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

inline int _ls(int x)
{
    return 2*x+1;
}

inline int _rs(int x)
{
    return 2*x+2;
}

inline int _fa(int x)
{
    return (x-1)/2;
}

class _heap // 小根堆
{
private:
    vector<int> Heap;
    int size; // 添加 size 成员变量
public:
    _heap() : size(0) {} // 构造函数中初始化 size

    void push(int x)
    {
        Heap.push_back(x);
        int p = size;
        size++;
        if (!p)
            return;
        while (Heap[_fa(p)] > Heap[p] && p > 0)
        {
            swap(Heap[_fa(p)], Heap[p]);
            p = _fa(p);
        }
    }

    int top(void)
    {
        if (!size)
            return 0;
        return Heap[0];
    }

    void pop(void)
    {
        if (!size) // 添加判断条件
            return;
        size--;
        swap(Heap[size], Heap[0]);
        int p = 0, minp;
        while (_ls(p) < size)
        {
            minp = (Heap[_ls(p)] < Heap[_rs(p)]) ? _ls(p) : _rs(p);
            if (_rs(p) == size)
                minp = _ls(p);
            if (Heap[p] > Heap[minp])
                swap(Heap[p], Heap[minp]);
            else
                break;
            p = minp; // 更新 p 的值
        }
    }
};

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    _heap a;
    int n, op, x;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> op;
        if (op == 1)
        {
            cin >> x;
            a.push(x);
        }
        else if (op == 2)
        {
            cout << a.top() << endl;
        }
        else if (op == 3)
        {
            a.pop();
        }
        else
        {
            continue; // 跳过不合法的操作
        }
    }
    return 0;
}

经过以上修正,代码应该能够正确地实现小根堆的功能了。希望对你有帮助!如果还有其他问题,请随时提问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-5 13:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表