额外减小 发表于 2023-11-5 23:57:22

堆 C++ 求助

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>Heap and p>0)
                        {
                                swap(Heap,Heap);
                                p=_fa(p);
                        }
                }
                int top(void)
                {
                        if(!size)
                        return 0;
                        return Heap;
                       
                }
                void pop(void)
                {
                        if(!size)return;
                        size--;
            
                        swap(Heap,Heap);
                        int p=0,minp;
                        while(_ls(p)<size)
                        {
                minp=(Heap<Heap)?_ls(p):_rs(p);
                if(_rs(p)==size)
                minp=_ls(p);
                if(Heap>Heap)
                                swap(Heap,Heap);
                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

Mike_python小 发表于 2023-11-5 23:58:21

看起来你的小根堆实现有一些问题。以下是我对你的代码进行的一些修改和注释:


#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 > Heap && p > 0) // 这里应该是Heap >= Heap
      {
            swap(Heap, Heap);
            p = _fa(p);
      }
    }

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

    void pop(void)
    {
      if (!size)
            return;
      size--;
      swap(Heap, Heap);
      int p = 0, minp;

      while (_ls(p) < size)
      {
            minp = (Heap < Heap) ? _ls(p) : _rs(p);

            if (_rs(p) == size)
            {
                minp = _ls(p);
            }

            if (Heap > Heap)
            {
                swap(Heap, Heap);
            }
            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 >= Heap,而不是Heap > Heap。小根堆要求父节点的值小于或等于子节点的值。
2. 在pop函数中,当右子节点不存在时,minp应该等于左子节点,否则会导致数组越界错误。
3. 添加了返回语句return 0;来结束main函数。

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

球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:}

isdkz 发表于 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 > Heap && p > 0)
      {
            swap(Heap, Heap);
            p = _fa(p);
      }
    }

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

    void pop(void)
    {
      if (!size) // 添加判断条件
            return;
      size--;
      swap(Heap, Heap);
      int p = 0, minp;
      while (_ls(p) < size)
      {
            minp = (Heap < Heap) ? _ls(p) : _rs(p);
            if (_rs(p) == size)
                minp = _ls(p);
            if (Heap > Heap)
                swap(Heap, Heap);
            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;
}


经过以上修正,代码应该能够正确地实现小根堆的功能了。希望对你有帮助!如果还有其他问题,请随时提问。
页: [1]
查看完整版本: 堆 C++ 求助