GGBGGBGGB000 2022-08-04 11:23 采纳率: 0%
浏览 24

为什么用堆过不了,谁能告诉我原因 ?

洛谷 P1880 [NOI1995] 石子合并

题目链接

我是用大根堆和小根堆来做的,只过了 2 个点,中间三个点 WA 了。

谁能告诉我为什么 ?

本人代码 :

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
priority_queue<LL, vector<LL>, greater<LL> > q;
priority_queue<LL> p;
//struct node
//{
//    int num;
//    bool operator < (const node&a) const
//    {
//        return num < a.num;
//    }
//};

LL n;

int main()
{
    scanf("%lld", &n);
    for(int i = 1; i <= n; i ++)
    {
        LL k;
        scanf("%lld", &k);
        q.push(k);
        p.push(k);
    }
    LL ans = 0, t = 0;
    LL sum = 0, e = 0;
    while(q.size() >= 2)
    {
        ans += q.top();
        t += q.top();
        q.pop();
        ans += q.top();
        t += q.top();
        q.pop();
        q.push(t);
        t = 0;
    }
    while(p.size() >= 2)
    {
        sum += p.top();
        e += p.top();
        p.pop();
        sum += p.top();
        e += p.top();
        p.pop();
        p.push(e);
        e = 0;
    }
    printf("%lld\n%lld\n", ans, sum); 
    return 0;
}
  • 写回答

1条回答 默认 最新

  • [PE]经典八炮 2022-08-04 19:21
    关注

    只能合并相邻的两堆啊。。。这个题要用dp

    评论

报告相同问题?

问题事件

  • 修改了问题 8月4日
  • 创建了问题 8月4日